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 { setHtmlPageLang, setLoadLocalePool } from './helper';
  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. setHtmlPageLang(locale);
  15. setLoadLocalePool((loadLocalePool) => {
  16. loadLocalePool.push(locale);
  17. });
  18. return {
  19. legacy: false,
  20. locale,
  21. fallbackLocale: fallback,
  22. messages: {
  23. [locale]: message,
  24. },
  25. availableLocales: availableLocales,
  26. sync: true, //If you don’t want to inherit locale from global scope, you need to set sync of i18n component option to false.
  27. silentTranslationWarn: true, // true - warning off
  28. missingWarn: false,
  29. silentFallbackWarn: true,
  30. };
  31. }
  32. // setup i18n instance with glob
  33. export async function setupI18n(app: App) {
  34. const options = await createI18nOptions();
  35. i18n = createI18n(options) as I18n;
  36. app.use(i18n);
  37. }