fix(i18n): better-locale 接入改为 activation-order-safe
ctx.get('betterLocale') 是非响应式读取(cordis 只对声明的 inject
依赖重新求值)。如果 better-locale 在 better-sidebar 之后激活,
apply 顶层的 ctx.get 返回 undefined,attachBetterLocale(undefined)
使整个覆盖接入变成 no-op,即使用户后来在设置页选了日语,
better-sidebar 的 t() 也不查询 override store。
改为在 ctx.effect 中订阅 ctx.locale.subscribe:better-locale 在
激活时(若有持久化的 override)和用户切换 override 时都会 bump
locale revision,subscribe 回调触发 sync(),重新读取 ctx.get
('betterLocale')。store 可用后 attachBetterLocale(store) + register
ja dict;store 不可用时 detach。
这解决了插件激活顺序不确定的问题:无论 better-locale 先于还是后于
better-sidebar 激活,override 接入都能正确建立。
This commit is contained in:
1 parent
38dc807d15
commit
16a809594c
1 file changed
+37
-21
+37
-21
@@ -67,28 +67,44 @@ export function apply(ctx: Context): void {
|
|||||||
// directly) also switches to the override language. We also register
|
// directly) also switches to the override language. We also register
|
||||||
// the ja dict with the better-locale store so external callers of
|
// the ja dict with the better-locale store so external callers of
|
||||||
// ctx.locale.lookup('betterSidebar', key) get the override text too.
|
// ctx.locale.lookup('betterSidebar', key) get the override text too.
|
||||||
// Optional peer: ctx.get returns undefined when better-locale is absent,
|
//
|
||||||
// in which case the whole block is a no-op and the zh/en chain runs.
|
// Activation-order-safe: ctx.get('betterLocale') is a non-reactive read
|
||||||
const betterLocale = ctx.get('betterLocale') as
|
// (cordis only re-evaluates declared `inject` deps). If better-locale
|
||||||
| {
|
// activates after better-sidebar, the initial read returns undefined.
|
||||||
readonly active: string | undefined
|
// We subscribe to the locale revision — better-locale bumps it on
|
||||||
getOverride(dshActive: string, ns: string, key: string): string | undefined
|
// activation (when a persisted override exists) and on every override
|
||||||
isOverrideActive(dshActive: string): boolean
|
// switch — and re-check ctx.get on each bump, attaching + registering
|
||||||
register(ns: string, dicts: Record<string, Record<string, string>>): () => void
|
// the ja dict once the store becomes available.
|
||||||
subscribe(listener: () => void): () => void
|
ctx.effect(() => {
|
||||||
|
let dispose: (() => void) | undefined
|
||||||
|
const sync = (): void => {
|
||||||
|
dispose?.()
|
||||||
|
dispose = undefined
|
||||||
|
const store = ctx.get('betterLocale') as
|
||||||
|
| {
|
||||||
|
readonly active: string | undefined
|
||||||
|
getOverride(dshActive: string, ns: string, key: string): string | undefined
|
||||||
|
isOverrideActive(dshActive: string): boolean
|
||||||
|
register(ns: string, dicts: Record<string, Record<string, string>>): () => void
|
||||||
|
subscribe(listener: () => void): () => void
|
||||||
|
}
|
||||||
|
| undefined
|
||||||
|
attachBetterLocale(store)
|
||||||
|
if (store !== undefined) {
|
||||||
|
dispose = store.register(LOCALE_NS, { ja })
|
||||||
}
|
}
|
||||||
| undefined
|
}
|
||||||
attachBetterLocale(betterLocale)
|
// Initial check (picks up the store if better-locale activated first).
|
||||||
if (betterLocale !== undefined) {
|
sync()
|
||||||
ctx.effect(() => {
|
// Re-check on every locale revision bump (better-locale bumps when it
|
||||||
// Register the ja dict for the betterSidebar namespace. Better-locale's
|
// activates with a persisted override, and when the user switches).
|
||||||
// patched lookup consults this when the user has selected the ja
|
const unsubscribe = ctx.locale.subscribe(sync)
|
||||||
// override; the dict stays owned by better-sidebar (the namespace
|
return () => {
|
||||||
// owner), so a single source of truth covers both ctx.locale callers
|
unsubscribe()
|
||||||
// and the internal t().
|
dispose?.()
|
||||||
return betterLocale.register(LOCALE_NS, { ja })
|
attachBetterLocale(undefined)
|
||||||
}, 'dsh-better-sidebar: better-locale ja dict')
|
}
|
||||||
}
|
}, 'dsh-better-sidebar: better-locale lazy integration')
|
||||||
// One store instance per activation: production code creates it only here,
|
// One store instance per activation: production code creates it only here,
|
||||||
// then hands it to the mounted panel and closes over it in the slot
|
// then hands it to the mounted panel and closes over it in the slot
|
||||||
// registrations (the official createXXXStore() factory rule — no
|
// registrations (the official createXXXStore() factory rule — no
|
||||||
|
|||||||
Reference in new issue
Block a user