From 23849a0c669de858f51cb9eefa4f168533051f0c Mon Sep 17 00:00:00 2001 From: sunmh Date: Tue, 11 Aug 2026 15:39:46 +0800 Subject: [PATCH] =?UTF-8?q?feat(i18n):=20=E5=89=8D=E7=AB=AF=E9=BB=98?= =?UTF-8?q?=E8=AE=A4=E8=AF=AD=E8=A8=80=E6=94=AF=E6=8C=81=E7=BB=8F=20DEFAUL?= =?UTF-8?q?T=5FLOCALE=20=E7=8E=AF=E5=A2=83=E5=8F=98=E9=87=8F=E8=BF=90?= =?UTF-8?q?=E8=A1=8C=E6=97=B6=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.example | 4 ++++ docker-compose.yml | 1 + frontend/docker-entrypoint.sh | 3 ++- frontend/src/i18n/index.ts | 18 ++++++++++++++++-- frontend/src/utils/index.ts | 1 + 5 files changed, 24 insertions(+), 3 deletions(-) diff --git a/.env.example b/.env.example index da5f273a..837b075f 100644 --- a/.env.example +++ b/.env.example @@ -55,6 +55,10 @@ TZ=Asia/Shanghai # 系统默认语言(BCP-47),用于 Prompt 中 {{language}} 占位符回退。 # 优先级:Accept-Language 请求头 > 此变量 > 内置默认值 (en-US)。 # WEKNORA_LANGUAGE=zh-CN +# 前端界面默认语言(BCP-47)。可选:zh-CN | en-US | ru-RU | ko-KR。 +# 优先级:用户已选语言(localStorage) > 此变量 > 内置默认 zh-CN。 +# 仅影响首次访问/未手动切换语言的用户;改完重启 frontend 容器即生效,无需重建镜像。 +# DEFAULT_LOCALE=en-US # 启动时自动执行数据库迁移;设 false 禁用(默认启用)。 AUTO_MIGRATE=true # 自动恢复脏数据迁移状态(默认 true)。 diff --git a/docker-compose.yml b/docker-compose.yml index be484437..c25547fb 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -9,6 +9,7 @@ services: - "${FRONTEND_PORT:-80}:80" environment: - MAX_FILE_SIZE_MB=${MAX_FILE_SIZE_MB:-50} + - DEFAULT_LOCALE=${DEFAULT_LOCALE:-} - APP_HOST=${APP_HOST:-app} # APP_BACKEND_PORT: the port NGINX proxies to (default 8080). # For local deployment this is the App container's listening port, independent of host-mapped APP_PORT. diff --git a/frontend/docker-entrypoint.sh b/frontend/docker-entrypoint.sh index f0a7bd41..1262bc26 100644 --- a/frontend/docker-entrypoint.sh +++ b/frontend/docker-entrypoint.sh @@ -3,7 +3,8 @@ # 生成运行时配置文件,注入环境变量到前端 cat > /usr/share/nginx/html/config.js << EOF window.__RUNTIME_CONFIG__ = { - MAX_FILE_SIZE_MB: ${MAX_FILE_SIZE_MB:-50} + MAX_FILE_SIZE_MB: ${MAX_FILE_SIZE_MB:-50}, + DEFAULT_LOCALE: "${DEFAULT_LOCALE:-}" }; EOF diff --git a/frontend/src/i18n/index.ts b/frontend/src/i18n/index.ts index da405b61..6028a7ab 100755 --- a/frontend/src/i18n/index.ts +++ b/frontend/src/i18n/index.ts @@ -11,8 +11,22 @@ const messages = { 'ko-KR': koKR } -// Получаем сохраненный язык из localStorage или используем китайский по умолчанию -const savedLocale = localStorage.getItem('locale') || 'zh-CN' +const SUPPORTED_LOCALES = Object.keys(messages) +const BUILT_IN_DEFAULT = 'zh-CN' + +// Resolve the deployment default locale. Mirrors the MAX_FILE_SIZE_MB convention: +// runtime config (.env -> docker-entrypoint -> window.__RUNTIME_CONFIG__) wins over +// build-time env (VITE_DEFAULT_LOCALE), then the built-in default. Unknown values +// (e.g. a typo in .env) fall back to the built-in default instead of a blank UI. +function resolveDefaultLocale(): string { + const candidate = window.__RUNTIME_CONFIG__?.DEFAULT_LOCALE + || import.meta.env.VITE_DEFAULT_LOCALE + || BUILT_IN_DEFAULT + return SUPPORTED_LOCALES.includes(candidate) ? candidate : BUILT_IN_DEFAULT +} + +// User's explicit past choice wins; otherwise use the deployment default. +const savedLocale = localStorage.getItem('locale') || resolveDefaultLocale() const i18n = createI18n({ legacy: false, diff --git a/frontend/src/utils/index.ts b/frontend/src/utils/index.ts index 235afda6..414609b6 100644 --- a/frontend/src/utils/index.ts +++ b/frontend/src/utils/index.ts @@ -7,6 +7,7 @@ declare global { interface Window { __RUNTIME_CONFIG__?: { MAX_FILE_SIZE_MB?: number; + DEFAULT_LOCALE?: string; }; } }