setupI18n.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import type { App } from 'vue';
  2. import type { I18n, I18nOptions } from 'vue-i18n';
  3. import { createI18n } from 'vue-i18n';
  4. import { setLoadLocalePool } from './useLocale';
  5. import { localeSetting } from '/@/settings/localeSetting';
  6. import { useLocaleStoreWithOut } from '/@/store/modules/locale';
  7. const { fallback, availableLocales } = localeSetting;
  8. export let i18n: ReturnType<typeof createI18n>;
  9. async function createI18nOptions(): Promise<I18nOptions> {
  10. const localeStore = useLocaleStoreWithOut();
  11. const locale = localeStore.getLocale;
  12. const defaultLocal = await import(`./lang/${locale}.ts`);
  13. const message = defaultLocal.default?.message ?? {};
  14. setLoadLocalePool((loadLocalePool) => {
  15. loadLocalePool.push(locale);
  16. });
  17. return {
  18. legacy: false,
  19. locale,
  20. fallbackLocale: fallback,
  21. messages: {
  22. [locale]: message,
  23. },
  24. availableLocales: availableLocales,
  25. sync: true, //If you don’t want to inherit locale from global scope, you need to set sync of i18n component option to false.
  26. silentTranslationWarn: true, // true - warning off
  27. missingWarn: false,
  28. silentFallbackWarn: true,
  29. };
  30. }
  31. // setup i18n instance with glob
  32. export async function setupI18n(app: App) {
  33. const options = await createI18nOptions();
  34. i18n = createI18n(options) as I18n;
  35. app.use(i18n);
  36. }