🐛 fix(client): 注册 locale 字典,修掉设置页显示翻译键的问题

This commit is contained in:
pyh
2026-09-30 15:55:12 +08:00
parent 2f38b7ea3c
commit 2097aeaa76
3 changed files with 42 additions and 7 deletions
+33 -4
View File
@@ -375,10 +375,35 @@ window.__ModuleLoader__.load({
return preview(interaction.displayReason?.text ?? interaction.reason?.text ?? '')
}
/**
* Register this package's dictionaries with the locale service.
*
* Without this the service has no entry for the namespace and every
* `locale.bind(NS)` lookup answers with the key itself, so the settings row
* shows `settings.title` instead of its copy. The typed form takes every
* shipped locale in one call, and the older single-locale form stays
* supported as a fallback. Registration bumps the locale revision, so
* already-rendered entries pick the copy up without a reload.
* @param locale - the locale service, or undefined when this profile has none.
* @returns a disposer for whichever registrations were installed.
*/
function registerDictionaries(locale) {
if (locale === undefined || typeof locale.register !== 'function') return () => {}
const dictionaries = { zh, en }
try {
return locale.register(NS, dictionaries)
} catch {
const disposers = Object.entries(dictionaries).map(([id, dict]) => locale.register(NS, id, dict))
return () => {
for (const dispose of disposers) dispose?.()
}
}
}
/**
* Build a translate function for one locale service: the service's own bound
* namespace first, then the current language's dictionary, then the key
* itself, so a missing seat degrades copy instead of rendering.
* namespace first, then this package's dictionaries, then the key itself, so
* a missing seat degrades copy instead of rendering.
* @param locale - the locale service, or undefined when this profile has none.
* @returns a translate function with `{ name }` interpolation.
*/
@@ -387,7 +412,10 @@ window.__ModuleLoader__.load({
if (locale !== undefined && typeof locale.bind === 'function') {
try {
const bound = locale.bind(NS)
if (typeof bound === 'function') return bound(key, params)
if (typeof bound === 'function') {
const translated = bound(key, params)
if (typeof translated === 'string' && translated !== key) return translated
}
} catch {
/* a locale service without this namespace still renders the fallback */
}
@@ -395,7 +423,7 @@ window.__ModuleLoader__.load({
let language = ''
try {
const snapshot = locale?.getSnapshot?.()
language = String(snapshot?.language ?? snapshot?.locale ?? snapshot?.id ?? '')
language = String(snapshot?.active ?? '')
} catch {
language = ''
}
@@ -951,6 +979,7 @@ window.__ModuleLoader__.load({
}
}
ctx.effect(() => registerDictionaries(locale), 'session-notify: dictionaries')
ctx.effect(() => installBridges(), 'session-notify: window focus')
ctx.effect(() => () => {
disposed = true