diff --git a/core/core-frontend/src/hooks/web/useLocale.ts b/core/core-frontend/src/hooks/web/useLocale.ts index a19b90576d..31c3b24c90 100644 --- a/core/core-frontend/src/hooks/web/useLocale.ts +++ b/core/core-frontend/src/hooks/web/useLocale.ts @@ -1,7 +1,7 @@ import { i18n } from '@/plugins/vue-i18n' import { useLocaleStoreWithOut } from '@/store/modules/locale' import { setHtmlPageLang } from '@/plugins/vue-i18n/helper' - +import { PATH_URL } from '@/config/axios/service' const setI18nLanguage = (locale: LocaleType) => { const localeStore = useLocaleStoreWithOut() @@ -16,13 +16,34 @@ const setI18nLanguage = (locale: LocaleType) => { setHtmlPageLang(locale) } +const loadRemoteI18n = async (option: any) => { + const name = option.lang.replace('-', '_') + const path = + PATH_URL.startsWith('./') && PATH_URL.length > 2 + ? window.location.pathname + PATH_URL.substring(2) + : PATH_URL + const url = `${path}/i18n/custom_${name}_front_${option.name}.js` + return await import(url) +} + export const useLocale = () => { // Switching the language will change the locale of useI18n // And submit to configuration modification const changeLocale = async (locale: LocaleType) => { const globalI18n = i18n.global - - const langModule = await import(`../../locales/${locale}.ts`) + let langModule = null + if (['zh-CN', 'en', 'tw'].includes(locale)) { + langModule = await import(`../../locales/${locale}.ts`) + } else { + const localeStore = useLocaleStoreWithOut() + const currentLocale = localeStore.getCurrentLocale + const localeMap = await localeStore.getLocaleMap + const cMap: any = localeMap.find(item => { + return item.lang === currentLocale.lang + }) + langModule = await loadRemoteI18n(cMap) + } + // const langModule = await import(`../../locales/${locale}.ts`) globalI18n.setLocaleMessage(locale, langModule.default) diff --git a/core/core-frontend/src/store/modules/locale.ts b/core/core-frontend/src/store/modules/locale.ts index b856fa87bf..54949d05f6 100644 --- a/core/core-frontend/src/store/modules/locale.ts +++ b/core/core-frontend/src/store/modules/locale.ts @@ -54,6 +54,7 @@ export const useLocaleStore = defineStore('locales', { const res = await request.get({ url: '/sysParameter/i18nOptions' }) this.customLoaded = true const customMap = res.data + let match = false for (const key in customMap) { const item = { lang: key, @@ -61,6 +62,17 @@ export const useLocaleStore = defineStore('locales', { custom: true } this.localeMap.push(item) + if (this.currentLocale?.lang === key) { + match = true + } + } + if (this.currentLocale?.lang && !match) { + const matchItem = this.localeMap.find(item => + item.lang.startsWith(this.currentLocale.lang) + ) + if (matchItem) { + this.currentLocale['lang'] = matchItem.lang + } } return this.localeMap } catch (error) {