tolgee-js icon indicating copy to clipboard operation
tolgee-js copied to clipboard

Feat: Provide support for static data directory and let tolgee handle importing files itself

Open paulwer opened this issue 5 months ago • 2 comments

Hello everyone, we have created a small script to handle import of static data (no ns and ns supported), as it is created by the cli automaticly into tolgee:

Object.fromEntries(
      Object.entries(import.meta.glob('./locales/**/*.json') as Record<string, () => Promise<{ default: Record<string, unknown> }>>)
        .map(([path, loader]) => {
          const m0 = path.match(/\.\/locales\/([a-z]{2})\.json$/);
          if (m0) {
            const lang = m0[1];
            return [lang, () => loader().then(m => m.default)];
          }
          const m1 = path.match(/\.\/locales\/([^\/]+)\/([a-z]{2})\.json$/);
          if (m1) {
            const [, ns, lang] = m1;
            return [`${lang}:${ns}`, () => loader().then(m => m.default)];
          }
          return null as any;
        })
        .filter((e): e is [string, () => Promise<Record<string, unknown>>] => Boolean(e))
) as TolgeeStaticDataProp

We would like to suggest to include this behavior into the cli itself, as it would be a huge benefit to just provide the dir name and the cli would manage importing files themself.

If this is not wanted behavior, feel free to close this issue :)

Edit: please move to tolgee-js. sorry :)

Kind regards & thank you paulwer

paulwer avatar Jul 07 '25 03:07 paulwer

Hi! Thanks for the suggestion! ^^

Let me check if I understand the idea correctly — the code is intended to automatically convert a folder of locale files into a staticData map, something like:

{
  en: () => import('./locales/en.json'),
  de: () => import('./locales/de.json'),
  fr: () => import('./locales/fr.json'),
}

The main issue is that import.meta.glob is specific to Vite. I believe Tolgee JS aims to stay build-tool agnostic, so adding a helper that depends on Vite's API wouldn't really fit — it wouldn't work in Webpack, Next.js, Astro, SvelteKit (non-Vite builds), and others.

We could consider mentioning this pattern in the docs for Vite users (perhaps in a more human-readable form).

Still, it's a good idea, and your example could definitely benefit Vite users — thanks for sharing! ^^

Anty0 avatar Nov 24 '25 15:11 Anty0

Hi, thanks for the response. The goal is to give some guideance or a unified way on how to import the different languages and namespaces, because thats a common problem, when starting to use the lib.

paulwer avatar Nov 25 '25 12:11 paulwer