web


git clone https://radroots.dev/git/web.git
Log | Files | Refs | Submodules | README | LICENSE

i18n.ts (1133B)


      1 import { i18n_conf } from '@radroots/apps-lib';
      2 import { iso639_1, locales_default, type Locales } from '@radroots/locales';
      3 
      4 export type Locale = Locales;
      5 
      6 const translations: Record<Locale, typeof iso639_1> = locales_default.reduce((acc, locale) => ({ ...acc, [locale as Locale]: iso639_1, }), {} as Record<Locale, typeof iso639_1>);
      7 const locale_keys = Object.keys(translations);
      8 
      9 const i18n = i18n_conf<Locale>({
     10     default_locale: `en`,
     11     translations,
     12     loaders: [
     13         ...locale_keys.map((locale) => [`common`, `countries`, `error`, `eula`, `icu`, `notification`, `products`, `units`].map(key => ({
     14             locale,
     15             key,
     16             loader: async () => (await import(`../../../../lib/locales/src/messages/${locale}/${key}.json`)).default
     17         }))),
     18     ].flat()
     19 });
     20 
     21 const {
     22     t: ls,
     23     loading: translations_loading,
     24     locales,
     25     locale,
     26     loadTranslations: load_translations
     27 } = i18n;
     28 
     29 translations_loading.subscribe(async (_loading) => {
     30     if (_loading) await translations_loading.toPromise();
     31 });
     32 
     33 export { load_translations, locale, locales, ls, translations, translations_loading };