From 00895f4da2b9fd7e0e66096815085528baa6299e Mon Sep 17 00:00:00 2001
From: admin <344137771@qq.com>
Date: Sun, 01 Feb 2026 14:49:18 +0800
Subject: [PATCH] 1
---
src/i18n/index.js | 74 +++++++++++++++----------------------
1 files changed, 30 insertions(+), 44 deletions(-)
diff --git a/src/i18n/index.js b/src/i18n/index.js
index ff5c263..558d7ba 100644
--- a/src/i18n/index.js
+++ b/src/i18n/index.js
@@ -1,5 +1,5 @@
import { createI18n } from 'vue-i18n'
-import { getStorage } from '@/utils/index'
+import { getStorage, getBrowserLang } from '@/utils/index'
import enLocale from './modules/en'
import cnLocale from './modules/CN'
import zhcnLocale from './modules/zh-CN'
@@ -13,56 +13,42 @@
import Italy from './modules/Italy'
import SpanishLocal from './modules/es'
import PortugueseLocal from './modules/pt'
-const lang = getStorage('lang') || 'en'
-const messages = {
- 'en': {
- ...enLocale
- },
- 'CN': {
- ...cnLocale
- },
- 'zh-CN': {
- ...zhcnLocale
- },
- 'Korean': {
- ...korcnLocale
- },
- 'Japanese': {
- ...japcnLocale
- },
- 'de': {
- ...Deutsch
- },
- 'fr': {
- ...fr
- },
- 'vi': {
- ...vi
- },
- 'th': {
- ...th
- },
- 'Italy': {
- ...Italy
- },
- 'es': {
- ...SpanishLocal
- },
- 'pt': {
- ...PortugueseLocal
- },
- 'gr': {
- ...gr
- }
+// 支持的 locale 列表,用于校验
+const SUPPORTED_LOCALES = ['en', 'CN', 'zh-CN', 'Korean', 'Japanese', 'de', 'fr', 'vi', 'th', 'Italy', 'es', 'pt', 'gr']
+
+// 获取初始语言:缓存 > 浏览器语言,并确保在支持列表中
+function getInitialLocale() {
+ const cached = getStorage('lang')
+ if (cached && SUPPORTED_LOCALES.includes(cached)) return cached
+ const browserLang = getBrowserLang()
+ return SUPPORTED_LOCALES.includes(browserLang) ? browserLang : 'en'
}
+const messages = {
+ en: { ...enLocale },
+ CN: { ...cnLocale },
+ 'zh-CN': { ...zhcnLocale },
+ Korean: { ...korcnLocale },
+ Japanese: { ...japcnLocale },
+ de: { ...Deutsch },
+ fr: { ...fr },
+ vi: { ...vi },
+ th: { ...th },
+ Italy: { ...Italy },
+ es: { ...SpanishLocal },
+ pt: { ...PortugueseLocal },
+ gr: { ...gr }
+}
const i18n = createI18n({
legacy: false,
- locale: lang, // 首先从缓存里拿,没有的话就用浏览器语言,
- fallbackLocale: 'en', // 设置备用语言
+ locale: getInitialLocale(),
+ fallbackLocale: ['zh-CN', 'en'], // 缺失 key 时先回退到简体中文,再回退到英文
messages,
+ missingWarn: import.meta.env.DEV, // 仅开发环境输出缺失 key 警告
+ fallbackWarn: import.meta.env.DEV,
+ silentTranslationWarn: !import.meta.env.DEV
})
export default i18n
\ No newline at end of file
--
Gitblit v1.9.3