2021-01-09 23:28:52 +08:00
|
|
|
|
import type { App } from 'vue';
|
2023-10-08 17:30:19 +08:00
|
|
|
|
import type { I18nOptions } from 'vue-i18n';
|
2020-11-19 23:01:27 +08:00
|
|
|
|
|
|
|
|
|
|
import { createI18n } from 'vue-i18n';
|
2021-06-06 23:36:22 +08:00
|
|
|
|
import { setHtmlPageLang, setLoadLocalePool } from './helper';
|
2021-02-27 23:08:12 +08:00
|
|
|
|
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
|
|
|
|
|
2021-02-27 23:08:12 +08:00
|
|
|
|
const { fallback, availableLocales } = localeSetting;
|
2020-11-19 23:01:27 +08:00
|
|
|
|
|
2021-02-27 23:08:12 +08:00
|
|
|
|
export let i18n: ReturnType<typeof createI18n>;
|
2021-01-09 23:28:52 +08:00
|
|
|
|
|
2021-02-27 23:08:12 +08:00
|
|
|
|
async function createI18nOptions(): Promise<I18nOptions> {
|
2021-04-10 19:25:49 +08:00
|
|
|
|
const localeStore = useLocaleStoreWithOut();
|
2021-02-27 23:08:12 +08:00
|
|
|
|
const locale = localeStore.getLocale;
|
|
|
|
|
|
const defaultLocal = await import(`./lang/${locale}.ts`);
|
2021-03-02 20:54:15 +08:00
|
|
|
|
const message = defaultLocal.default?.message ?? {};
|
2021-06-06 23:36:22 +08:00
|
|
|
|
|
|
|
|
|
|
setHtmlPageLang(locale);
|
2021-05-11 09:24:03 +08:00
|
|
|
|
setLoadLocalePool((loadLocalePool) => {
|
|
|
|
|
|
loadLocalePool.push(locale);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2021-02-27 23:08:12 +08:00
|
|
|
|
return {
|
|
|
|
|
|
legacy: false,
|
|
|
|
|
|
locale,
|
|
|
|
|
|
fallbackLocale: fallback,
|
|
|
|
|
|
messages: {
|
|
|
|
|
|
[locale]: message,
|
|
|
|
|
|
},
|
|
|
|
|
|
availableLocales: availableLocales,
|
|
|
|
|
|
sync: true, //If you don’t 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,
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
2020-11-19 23:01:27 +08:00
|
|
|
|
|
|
|
|
|
|
// setup i18n instance with glob
|
2021-02-27 23:08:12 +08:00
|
|
|
|
export async function setupI18n(app: App) {
|
|
|
|
|
|
const options = await createI18nOptions();
|
2023-10-08 17:30:19 +08:00
|
|
|
|
i18n = createI18n(options);
|
2020-11-19 23:01:27 +08:00
|
|
|
|
app.use(i18n);
|
|
|
|
|
|
}
|