vue-vben-admin/src/locales/setupI18n.ts

45 lines
1.3 KiB
TypeScript
Raw Normal View History

2021-01-09 23:28:52 +08:00
import type { App } from 'vue';
import type { I18nOptions } from 'vue-i18n';
import { createI18n } from 'vue-i18n';
import { setHtmlPageLang, setLoadLocalePool } from './helper';
import { localeSetting } from '/@/settings/localeSetting';
2021-04-10 19:25:49 +08:00
import { useLocaleStoreWithOut } from '/@/store/modules/locale';
2021-01-09 23:28:52 +08:00
const { fallback, availableLocales } = localeSetting;
export let i18n: ReturnType<typeof createI18n>;
2021-01-09 23:28:52 +08:00
async function createI18nOptions(): Promise<I18nOptions> {
2021-04-10 19:25:49 +08:00
const localeStore = useLocaleStoreWithOut();
const locale = localeStore.getLocale;
const defaultLocal = await import(`./lang/${locale}.ts`);
2021-03-02 20:54:15 +08:00
const message = defaultLocal.default?.message ?? {};
setHtmlPageLang(locale);
setLoadLocalePool((loadLocalePool) => {
loadLocalePool.push(locale);
});
return {
legacy: false,
locale,
fallbackLocale: fallback,
messages: {
[locale]: message,
},
availableLocales: availableLocales,
sync: true, //If you dont want to inherit locale from global scope, you need to set sync of i18n component option to false.
silentTranslationWarn: true, // true - warning off
missingWarn: false,
silentFallbackWarn: true,
};
}
// setup i18n instance with glob
export async function setupI18n(app: App) {
const options = await createI18nOptions();
i18n = createI18n(options);
app.use(i18n);
}