github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/static_source/admin/src/plugins/vueI18n/index.ts (about) 1 import type { App } from 'vue' 2 import { createI18n } from 'vue-i18n' 3 import { useLocaleStoreWithOut } from '@/store/modules/locale' 4 import type { I18n, I18nOptions } from 'vue-i18n' 5 import { setHtmlPageLang } from './helper' 6 7 export let i18n: ReturnType<typeof createI18n> 8 9 const createI18nOptions = async (): Promise<I18nOptions> => { 10 const localeStore = useLocaleStoreWithOut() 11 const locale = localeStore.getCurrentLocale 12 const localeMap = localeStore.getLocaleMap 13 const defaultLocal = await import(`../../locales/${locale.lang}.ts`) 14 const message = defaultLocal.default ?? {} 15 16 setHtmlPageLang(locale.lang) 17 18 localeStore.setCurrentLocale({ 19 lang: locale.lang 20 // elLocale: elLocal 21 }) 22 23 return { 24 legacy: false, 25 locale: locale.lang, 26 fallbackLocale: locale.lang, 27 messages: { 28 [locale.lang]: message 29 }, 30 availableLocales: localeMap.map((v) => v.lang), 31 sync: true, 32 silentTranslationWarn: true, 33 missingWarn: false, 34 silentFallbackWarn: true 35 } 36 } 37 38 export const setupI18n = async (app: App<Element>) => { 39 const options = await createI18nOptions() 40 i18n = createI18n(options) as I18n 41 app.use(i18n) 42 }