setupI18n.ts 1.2 KB

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