Skip to content

The agent that grows with you

fix(desktop): refresh Kanban labels when the locale changes

brooklyn!4faa44267ee6
diff --git a/apps/desktop/src/contrib/kanban-i18n.test.tsx b/apps/desktop/src/contrib/kanban-i18n.test.tsx
new file mode 100644
index 00000000000..3062d916200
--- /dev/null
+++ b/apps/desktop/src/contrib/kanban-i18n.test.tsx
@@ -0,0 +1,104 @@
+import { act, cleanup, render, screen } from '@testing-library/react'
+import { afterEach, expect, it, vi } from 'vitest'
+
+import { PALETTE_AREA, usePaletteContributions } from '@/app/command-palette/contrib'
+import { ROUTES_AREA, SIDEBAR_NAV_AREA, type SidebarNavContribution } from '@/app/routes'
+import { createPluginContext } from '@/contrib/plugin'
+import { useContributions } from '@/contrib/react/use-contributions'
+import { registry } from '@/contrib/registry'
+import type { HermesConfigRecord } from '@/hermes'
+import { I18nProvider, useI18n } from '@/i18n'
+import type { I18nContextValue } from '@/i18n'
+import { setRuntimeI18nLocale } from '@/i18n/runtime'
+import { contributedKeybindHandler, keybindAction, KEYBINDS_AREA } from '@/lib/keybinds/actions'
+import { bindingsFor, resetBinding, setBinding } from '@/store/keybinds'
+
+import { KANBAN_LOCALES } from '../plugins/kanban/i18n'
+import plugin from '../plugins/kanban/plugin'
+
+// Keep the real registration and UI path without opening a board socket.
+vi.mock('../plugins/kanban/api', async () => ({ ...(await vi.importActual('../plugins/kanban/api')), bindApi: () => () => {} }))
+let i18n: I18nContextValue
+const disposers: Array<() => void> = []
+
+function Contributions() {
+ i18n = useI18n()
+ const nav = useContributions(SIDEBAR_NAV_AREA)
+ const palette = usePaletteContributions()
+
+ return (
+ <>
+ {nav.map(c => (
+ <a key={c.id}>{(c.data as SidebarNavContribution).label}</a>
+ ))}
+ {palette.map(c => (
+ <button key={c.id} type="button">
+ {c.label}
+ </button>
+ ))}
+ </>
+ )
+}
+
+function unload() {
+ // Match bundled-loader disposal order, including registrations made later.
+ disposers.splice(0).forEach(dispose => dispose())
+}
+
+afterEach(() => {
+ cleanup()
+ unload()
+ resetBinding('kanban.newTask')
+ setRuntimeI18nLocale('en')
+})
+
+it('relabels Kanban after delayed config load and locale switches without replacing the board or bindings', async () => {
+ setRuntimeI18nLocale('en')
+ plugin.register(createPluginContext('kanban', dispose => disposers.push(dispose)))
+ const route = registry.getArea(ROUTES_AREA).find(c => c.id === 'kanban:page')
+ const handler = contributedKeybindHandler('kanban.newTask')
+ setBinding('kanban.newTask', ['mod+alt+k'])
+ let resolveConfig!: (config: HermesConfigRecord) => void
+
+ const config = new Promise<HermesConfigRecord>(resolve => {
+ resolveConfig = resolve
+ })
+
+ const client = { getConfig: () => config, saveConfig: async () => ({ ok: true }) }
+ render(
+ <I18nProvider configClient={client} initialLocale="en">
+ <Contributions />
+ </I18nProvider>
+ )
+ expect(screen.getByText(KANBAN_LOCALES.en!.nav as string)).toBeTruthy()
+ await act(async () => {
+ resolveConfig({ display: { language: 'zh' } })
+ await config
+ })
+
+ for (const locale of ['zh', 'ja', 'zh-hant', 'en'] as const) {
+ if (locale !== 'zh') {
+ await act(() => i18n.setLocale(locale))
+ }
+ const messages = KANBAN_LOCALES[locale]!
+ expect(screen.getByText(messages.nav as string)).toBeTruthy()
+ expect(screen.getByRole('button', { name: messages.openBoard as string })).toBeTruthy()
+ expect(screen.getByRole('button', { name: messages.newTaskCommand as string })).toBeTruthy()
+ expect(keybindAction('kanban.newTask')?.label).toBe(messages.newTaskCommand)
+ expect(contributedKeybindHandler('kanban.newTask')).toBe(handler)
+ expect(bindingsFor('kanban.newTask')).toEqual(['mod+alt+k'])
+ expect(registry.getArea(ROUTES_AREA).find(c => c.id === 'kanban:page')).toBe(route)
+ expect(registry.getArea(SIDEBAR_NAV_AREA).filter(c => c.id === 'kanban:nav')).toHaveLength(1)
+ }
+
+ await act(() => unload())
+ await act(() => i18n.setLocale('zh'))
+
+ for (const area of [SIDEBAR_NAV_AREA, PALETTE_AREA, KEYBINDS_AREA, ROUTES_AREA]) {
+ expect(registry.getArea(area).some(c => c.source === 'plugin:kanban')).toBe(false)
+ }
+
+ await act(() => plugin.register(createPluginContext('kanban', dispose => disposers.push(dispose))))
+ expect(screen.getByText(KANBAN_LOCALES.zh!.nav as string)).toBeTruthy()
+ expect(bindingsFor('kanban.newTask')).toEqual(['mod+alt+k'])
+})
diff --git a/apps/desktop/src/i18n/plugin-i18n.test.tsx b/apps/desktop/src/i18n/plugin-i18n.test.tsx
index 44e99d588e4..bfb911377c2 100644
--- a/apps/desktop/src/i18n/plugin-i18n.test.tsx
+++ b/apps/desktop/src/i18n/plugin-i18n.test.tsx
@@ -55,6 +55,32 @@ describe('plugin locale registry', () => {
expect(translatePlugin('merge', 'en', 'a', [])).toBe('a')
})
+ it('locale listeners ignore no-op changes and unsubscribe both explicitly and on unload', () => {
+ const disposers: Array<() => void> = []
+
+ const i18n = createPluginI18n('listener-plugin', dispose => {
+ disposers.push(dispose)
+
+ return dispose
+ })
+
+ i18n.register({ en: { greet: 'hello' }, ja: { greet: 'こんにちは' } })
+ const calls: string[] = []
+ const unsubscribe = i18n.onLocaleChange(() => calls.push(i18n.t('greet')))
+ expect(calls).toEqual([])
+ setRuntimeI18nLocale('en')
+ expect(calls).toEqual([])
+ setRuntimeI18nLocale('ja')
+ expect(calls).toEqual(['こんにちは'])
+ unsubscribe()
+ setRuntimeI18nLocale('en')
+ expect(calls).toEqual(['こんにちは'])
+ i18n.onLocaleChange(() => calls.push(i18n.t('greet')))
+ disposers.forEach(dispose => dispose())
+ setRuntimeI18nLocale('ja')
+ expect(calls).toEqual(['こんにちは'])
+ })
+
it('ctx.i18n.t reads the app runtime locale', () => {
const i18n = createPluginI18n('runtime-plugin', noopTrack)
i18n.register({ en: { greet: 'hello' }, ja: { greet: 'こんにちは' } })
diff --git a/apps/desktop/src/i18n/plugin-i18n.ts b/apps/desktop/src/i18n/plugin-i18n.ts
index b5f81b1c54f..bdd6d2533cf 100644
--- a/apps/desktop/src/i18n/plugin-i18n.ts
+++ b/apps/desktop/src/i18n/plugin-i18n.ts
@@ -16,7 +16,7 @@ import { atom } from 'nanostores'
import { useCallback } from 'react'
import { useI18n } from './context'
-import { getRuntimeI18nLocale, translateFrom } from './runtime'
+import { getRuntimeI18nLocale, subscribeRuntimeI18nLocale, translateFrom } from './runtime'
import type { Locale } from './types'
/** A leaf message: a literal or an interpolator (`n => `${n} left``). */
@@ -43,6 +43,9 @@ export interface PluginI18n {
/** Module-level translator against the app's active locale (mirrors
* `translateNow`). Non-reactive — in React prefer `usePluginI18n`. */
t: PluginTranslate
+ /** Observe locale changes (not initial registration) to rebuild static labels.
+ * Returns an unsubscribe function; also disposed on plugin unload. */
+ onLocaleChange: (listener: () => void) => () => void
}
const registry = new Map<string, Map<Locale, PluginMessages>>()
@@ -96,6 +99,7 @@ export function translatePlugin(pluginId: string, locale: Locale, key: string, a
export function createPluginI18n(pluginId: string, track: (dispose: () => void) => () => void): PluginI18n {
return {
register: bundles => track(registerPluginLocales(pluginId, bundles)),
+ onLocaleChange: listener => track(subscribeRuntimeI18nLocale(listener)),
t: (key, ...args) => translatePlugin(pluginId, getRuntimeI18nLocale(), key, args)
}
}
diff --git a/apps/desktop/src/i18n/runtime.ts b/apps/desktop/src/i18n/runtime.ts
index 9dc463c0312..b89807d1402 100644
--- a/apps/desktop/src/i18n/runtime.ts
+++ b/apps/desktop/src/i18n/runtime.ts
@@ -1,10 +1,11 @@
import { isRecord } from '@hermes/shared/i18n'
+import { atom } from 'nanostores'
import { TRANSLATIONS } from './catalog'
import { DEFAULT_LOCALE } from './languages'
import type { Locale } from './types'
-let runtimeLocale: Locale = DEFAULT_LOCALE
+const $runtimeLocale = atom<Locale>(DEFAULT_LOCALE)
/** Walk a dot-path (`a.b.c`) into a nested message tree. */
function resolvePath(source: unknown, key: string): unknown {
@@ -50,15 +51,18 @@ export function translateFrom(
}
export function setRuntimeI18nLocale(locale: Locale) {
- runtimeLocale = locale
+ $runtimeLocale.set(locale)
}
+/** Observe changes to the locale used by non-React plugin contributions. */
+export const subscribeRuntimeI18nLocale = $runtimeLocale.listen
+
/** The locale module-level translators resolve against (the app's active
* `display.language`). Plugin `ctx.i18n.t` reads this too. */
export function getRuntimeI18nLocale(): Locale {
- return runtimeLocale
+ return $runtimeLocale.get()
}
export function translateNow(key: string, ...args: unknown[]): string {
- return translateFrom(locale => TRANSLATIONS[locale], runtimeLocale, key, args)
+ return translateFrom(locale => TRANSLATIONS[locale], $runtimeLocale.get(), key, args)
}
diff --git a/apps/desktop/src/plugins/kanban/plugin.tsx b/apps/desktop/src/plugins/kanban/plugin.tsx
index e819eaa5d67..5836071773c 100644
--- a/apps/desktop/src/plugins/kanban/plugin.tsx
+++ b/apps/desktop/src/plugins/kanban/plugin.tsx
@@ -111,51 +111,61 @@ const plugin: HermesPlugin = {
data: { path: '/kanban' } satisfies RouteContribution,
render: () => <KanbanBoardPage />
},
- {
- id: 'nav',
- area: SIDEBAR_NAV_AREA,
- order: 50,
- data: { codicon: 'project', label: 'Kanban', path: '/kanban' } satisfies SidebarNavContribution
- },
{
id: 'count',
area: STATUSBAR_AREAS.right,
order: 80,
render: () => <KanbanCount />
- },
- {
- id: 'open',
- area: PALETTE_AREA,
- data: {
- id: 'kanban.open',
- label: 'Kanban: Open board',
- keywords: ['kanban', 'board', 'tasks', 'agents'],
- run: () => host.navigate('/kanban')
- } satisfies PaletteContribution
- },
- {
- id: 'new-task',
- area: PALETTE_AREA,
- data: {
- id: 'kanban.newTask',
- action: 'kanban.newTask',
- label: ctx.i18n.t('newTaskCommand'),
- keywords: ['kanban', 'task', 'new', 'create', 'triage'],
- run: newTask
- } satisfies PaletteContribution
- },
- {
- id: 'new-task',
- area: KEYBINDS_AREA,
- data: {
- id: 'kanban.newTask',
- category: 'view',
- defaults: ['mod+alt+n'],
- label: ctx.i18n.t('newTaskCommand'),
- run: newTask
- } satisfies KeybindContribution
}
])
+
+ const registerLabels = () =>
+ ctx.registerMany([
+ {
+ id: 'nav',
+ area: SIDEBAR_NAV_AREA,
+ order: 50,
+ data: { codicon: 'project', label: ctx.i18n.t('nav'), path: '/kanban' } satisfies SidebarNavContribution
+ },
+ {
+ id: 'open',
+ area: PALETTE_AREA,
+ data: {
+ id: 'kanban.open',
+ label: ctx.i18n.t('openBoard'),
+ keywords: ['kanban', 'board', 'tasks', 'agents'],
+ run: () => host.navigate('/kanban')
+ } satisfies PaletteContribution
+ },
+ {
+ id: 'new-task',
+ area: PALETTE_AREA,
+ data: {
+ id: 'kanban.newTask',
+ action: 'kanban.newTask',
+ label: ctx.i18n.t('newTaskCommand'),
+ keywords: ['kanban', 'task', 'new', 'create', 'triage'],
+ run: newTask
+ } satisfies PaletteContribution
+ },
+ {
+ id: 'new-task',
+ area: KEYBINDS_AREA,
+ data: {
+ id: 'kanban.newTask',
+ category: 'view',
+ defaults: ['mod+alt+n'],
+ label: ctx.i18n.t('newTaskCommand'),
+ run: newTask
+ } satisfies KeybindContribution
+ }
+ ])
+
+ let disposeLabels = registerLabels()
+ ctx.i18n.onLocaleChange(() => {
+ disposeLabels()
+ disposeLabels = registerLabels()
+ })
}
}