Commit 0e1bfeb1 authored by Lukáš Gorazd Hrodek's avatar Lukáš Gorazd Hrodek
Browse files

feat: refactor i18n catalogs into grouped locale message files

parent 80434f8e
Loading
Loading
Loading
Loading
+46 −0
Original line number Diff line number Diff line
type MessageCatalog = Record<string, string>;

const chunkModules = import.meta.glob("./messages/*/*.json");

function getChunkPathsForLocale(locale: string): string[] {
  return Object.keys(chunkModules)
    .filter((path) => path.startsWith(`./messages/${locale}/`))
    .sort((a, b) => a.localeCompare(b));
}

async function loadLegacyLocaleMessages(locale: string): Promise<MessageCatalog> {
  const module = await import(`./messages/${locale}.json`);
  return module.default as MessageCatalog;
}

export async function loadMessages(locale: string): Promise<MessageCatalog> {
  const chunkPaths = getChunkPathsForLocale(locale);

  if (chunkPaths.length === 0) {
    return loadLegacyLocaleMessages(locale);
  }

  const mergedMessages: MessageCatalog = {};

  for (const chunkPath of chunkPaths) {
    const loader = chunkModules[chunkPath];
    if (!loader) {
      continue;
    }

    const module = (await loader()) as { default?: MessageCatalog };
    const chunkMessages = module.default ?? {};

    for (const [key, value] of Object.entries(chunkMessages)) {
      if (key in mergedMessages) {
        throw new Error(
          `Duplicate translation key '${key}' detected while loading '${chunkPath}'.`,
        );
      }

      mergedMessages[key] = value;
    }
  }

  return mergedMessages;
}
+338 −562

File changed.

Preview size limit exceeded, changes collapsed.

+16 −0
Original line number Diff line number Diff line
{
  "api.loading.retry": "Opakovat připojení",
  "api.loading.retry.hint": "Spusťte `pnpm dev` v adresáři projektu pro spuštění serveru",
  "api.loading.startup.brand": "DeckHand",
  "api.loading.startup.description": "Čekám na API a automaticky pokračuji, jakmile bude připravené.",
  "api.loading.startup.error.description": "Nepodařilo se navázat spojení do 3 minut. Zkontrolujte backend a zkuste to znovu.",
  "api.loading.startup.error.title": "API je stále offline",
  "api.loading.startup.progress": "Kontroluji API…",
  "api.loading.startup.remaining": "Zbývá {seconds}s",
  "api.loading.startup.title": "DeckHand se spouští",
  "api.status.mock": "Testovací data",
  "api.status.offline": "Offline",
  "api.status.online": "Živé API",
  "api.status.retry.short": "Opakovat",
  "api.status.retry.title": "Zkusit znovu připojení API"
}
+13 −0
Original line number Diff line number Diff line
{
  "app-sidebar.project.delete": "Smazat projekt",
  "app-sidebar.project.open": "Otevřít projekt",
  "app-sidebar.project.options": "Možnosti",
  "app-sidebar.project.pin": "",
  "app-sidebar.project.rename": "Přejmenovat projekt",
  "app-sidebar.project.unpin": "",
  "app-sidebar.projects": "Projekty",
  "app-sidebar.projects.empty": "Zatím žádné projekty",
  "app-sidebar.projects.toggle": "",
  "app-sidebar.projects.view-all": "",
  "app-sidebar.settings": "Nastavení"
}
+4 −0
Original line number Diff line number Diff line
{
  "auth.logout.success.description": "Byli jste úspěšně odhlášeni.",
  "auth.logout.success.title": "Úspěšné odhlášení"
}
Loading