|
|
@@ -2,39 +2,32 @@ import { createI18n } from 'vue-i18n';
|
|
|
|
|
|
import { localST_Locale } from './base/storage';
|
|
|
|
|
|
-import { header_zh, header_en } from './views/common/header-i18n';
|
|
|
-
|
|
|
import { LANGUAGE_ENGLISH, LANGUAGE_CHINESE } from './base/variables';
|
|
|
|
|
|
import { useLocaleStore } from './base/store/use-locale';
|
|
|
|
|
|
+import ajax from './base/ajax';
|
|
|
+
|
|
|
/**
|
|
|
* 多语言解决方案
|
|
|
* @see https://vue-i18n.intlify.dev/
|
|
|
*/
|
|
|
|
|
|
-/**
|
|
|
- * 配置多语言
|
|
|
- * @see https://vue-i18n.intlify.dev/guide/installation.html#package-managers
|
|
|
- */
|
|
|
-const i18n = createI18n({
|
|
|
- locale: LANGUAGE_CHINESE,
|
|
|
- fallbackLocale: LANGUAGE_ENGLISH,
|
|
|
- messages: {
|
|
|
- en: {
|
|
|
- message: {
|
|
|
- hello: 'hello world'
|
|
|
- },
|
|
|
- header: header_en
|
|
|
- },
|
|
|
- zh: {
|
|
|
- message: {
|
|
|
- hello: '欢迎光临'
|
|
|
- },
|
|
|
- header: header_zh
|
|
|
- }
|
|
|
+let i18n = null;
|
|
|
+
|
|
|
+// 创建一个函数来加载语言文件
|
|
|
+async function loadLocaleMessages() {
|
|
|
+ const messages = {};
|
|
|
+ const locales = [LANGUAGE_ENGLISH, LANGUAGE_CHINESE]; // 根据你的需求添加语言代码
|
|
|
+
|
|
|
+ for (const locale of locales) {
|
|
|
+ const url = `/i18n/lang-${locale}.json`;
|
|
|
+ const response = await ajax.get(url);
|
|
|
+ messages[locale] = response;
|
|
|
}
|
|
|
-});
|
|
|
+
|
|
|
+ return messages;
|
|
|
+}
|
|
|
|
|
|
/**
|
|
|
* 检查一个字符串是否是合法的语言名称
|
|
|
@@ -56,6 +49,10 @@ export function setI18nLanguage(locale) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
+ if (!i18n) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
i18n.global.locale = locale;
|
|
|
|
|
|
localST_Locale.setItem(locale);
|
|
|
@@ -73,4 +70,14 @@ export function setI18nLanguage(locale) {
|
|
|
store.updateLanguage(locale);
|
|
|
}
|
|
|
|
|
|
-export default i18n;
|
|
|
+export async function setupI18n() {
|
|
|
+ const messages = await loadLocaleMessages();
|
|
|
+
|
|
|
+ i18n = createI18n({
|
|
|
+ locale: LANGUAGE_CHINESE,
|
|
|
+ fallbackLocale: LANGUAGE_ENGLISH,
|
|
|
+ messages
|
|
|
+ });
|
|
|
+
|
|
|
+ return i18n;
|
|
|
+}
|