feat(i18n): 前端默认语言支持经 DEFAULT_LOCALE 环境变量运行时配置

main
sunmh 2026-08-11 15:39:46 +08:00 committed by lyingbug
parent 20ecaa402c
commit 23849a0c66
5 changed files with 24 additions and 3 deletions

View File

@ -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)。

View File

@ -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.

View File

@ -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

View File

@ -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,

View File

@ -7,6 +7,7 @@ declare global {
interface Window {
__RUNTIME_CONFIG__?: {
MAX_FILE_SIZE_MB?: number;
DEFAULT_LOCALE?: string;
};
}
}