Skip to content

The agent that grows with you

fix(desktop): apply a saved pack language at boot

kshitijk4poor944799abc0a8
diff --git a/apps/desktop/src/i18n/context.tsx b/apps/desktop/src/i18n/context.tsx
index 8d287a2038b..87d70663ffb 100644
--- a/apps/desktop/src/i18n/context.tsx
+++ b/apps/desktop/src/i18n/context.tsx
@@ -194,11 +194,12 @@ export function I18nProvider({
}
// Keep inference unsaved so OS language changes apply on the next boot
- // until the user explicitly picks a language.
+ // until the user explicitly picks a language. The saved ask is checked
+ // again after the probe: its pack may have arrived in the meantime.
const machineProfile = await window.hermesDesktop?.getMachineProfile?.().catch(() => null)
if (!cancelled && !userLocaleRef.current) {
- setLocaleState(resolveInitialLocale(undefined, machineProfile?.locale))
+ setLocaleState(resolveInitialLocale(requestedRef.current, machineProfile?.locale))
}
})
.catch(error => {
diff --git a/apps/desktop/src/i18n/profile-locale.test.tsx b/apps/desktop/src/i18n/profile-locale.test.tsx
index ef09bea74af..7504f79aa6b 100644
--- a/apps/desktop/src/i18n/profile-locale.test.tsx
+++ b/apps/desktop/src/i18n/profile-locale.test.tsx
@@ -4,11 +4,14 @@ import { afterEach, expect, it, vi } from 'vitest'
import { setApiRequestConnection } from '@/api/client'
import type { HermesApiRequest } from '@/global'
import type { HermesConfigRecord } from '@/hermes'
+import { $gateway } from '@/store/gateway'
import { $activeGatewayProfile } from '@/store/profile'
-import { $connection } from '@/store/session'
+import { $connection, $gatewayState } from '@/store/session'
import { useI18n } from './context'
import { ProfileI18nProvider as I18nProvider } from './profile-provider'
+import { resetAppLocaleRegistry } from './registry'
+import { $requestedLocale } from './runtime'
function Probe() {
const { locale, setLocale, saveError, isSavingLocale } = useI18n()
@@ -31,6 +34,10 @@ function route(profile: string, connectionId: string | null = null) {
afterEach(() => {
cleanup()
+ $gateway.set(null)
+ $gatewayState.set('idle')
+ resetAppLocaleRegistry()
+ $requestedLocale.set(null)
route('default')
Reflect.deleteProperty(window, 'hermesDesktop')
vi.restoreAllMocks()
@@ -145,3 +152,99 @@ it('isolates stale reads and failed saves when the same profile moves between co
act(() => $connection.set({ baseUrl: 'https://second.example' } as never))
await waitFor(() => expect(screen.getByTestId('locale').textContent).toBe('ar'))
})
+
+// A backend whose hermes-lang-ko pack carries Desktop strings.
+function koPackRequest() {
+ return vi.fn(async (method: string) =>
+ method === 'i18n.languages'
+ ? {
+ languages: [
+ {
+ id: 'ko',
+ endonym: '한국어',
+ rtl: false,
+ source: 'plugin:hermes-lang-ko',
+ surfaces: ['core', 'tui', 'desktop']
+ }
+ ]
+ }
+ : { lang: 'ko', surface: 'desktop', messages: { 'common.save': '저장' } }
+ )
+}
+
+// A saved `display.language: ko` with an OS-language probe the test resolves.
+function savedKoWithDeferredProbe() {
+ let resolveProfile: (profile: { locale: string }) => void = () => undefined
+
+ const machineProfile = new Promise<{ locale: string }>(resolve => {
+ resolveProfile = resolve
+ })
+
+ window.hermesDesktop = {
+ api: vi.fn(async () => ({ display: { language: 'ko' } })),
+ getMachineProfile: vi.fn(() => machineProfile)
+ } as never
+
+ return async (locale: string) => {
+ await act(async () => {
+ resolveProfile({ locale })
+ await machineProfile
+ })
+ await act(async () => undefined)
+ }
+}
+
+function mountWithOpenSocket() {
+ act(() => $gatewayState.set('open'))
+ render(
+ <I18nProvider>
+ <Probe />
+ </I18nProvider>
+ )
+}
+
+// Boot reports the socket `open` before the client is published in `$gateway`.
+// The backend-pack sync must still run once the client arrives, or a language
+// only a pack renders (ko via hermes-lang-ko) never leaves English.
+it('pulls the backend language pack once the gateway client is published after the socket opened', async () => {
+ window.hermesDesktop = { api: vi.fn(async () => ({ display: { language: 'ko' } })) } as never
+ const request = koPackRequest()
+
+ mountWithOpenSocket()
+ await waitFor(() => expect(window.hermesDesktop.api).toHaveBeenCalled())
+ expect(request).not.toHaveBeenCalled()
+
+ act(() => $gateway.set({ request } as never))
+ await waitFor(() => expect(screen.getByTestId('locale').textContent).toBe('ko'))
+ expect(request).toHaveBeenCalledWith('i18n.catalog', { lang: 'ko', surface: 'desktop' })
+})
+
+// The OS-language probe can finish after the pack sync promoted the saved
+// language; the probe's answer must not replace it.
+it('keeps a pack-rendered saved language when the OS-language probe resolves after the pack', async () => {
+ const resolveProbe = savedKoWithDeferredProbe()
+
+ mountWithOpenSocket()
+ await waitFor(() => expect(window.hermesDesktop.getMachineProfile).toHaveBeenCalled())
+
+ act(() => $gateway.set({ request: koPackRequest() } as never))
+ await waitFor(() => expect(screen.getByTestId('locale').textContent).toBe('ko'))
+
+ await resolveProbe('en-US')
+ expect(screen.getByTestId('locale').textContent).toBe('ko')
+})
+
+// The other order: the OS guess applies first, then the pack promotes the
+// saved language.
+it('promotes a saved pack language that arrives after the OS-language probe', async () => {
+ const resolveProbe = savedKoWithDeferredProbe()
+
+ mountWithOpenSocket()
+ await waitFor(() => expect(window.hermesDesktop.getMachineProfile).toHaveBeenCalled())
+
+ await resolveProbe('en-US')
+ expect(screen.getByTestId('locale').textContent).toBe('en')
+
+ act(() => $gateway.set({ request: koPackRequest() } as never))
+ await waitFor(() => expect(screen.getByTestId('locale').textContent).toBe('ko'))
+})
diff --git a/apps/desktop/src/i18n/profile-provider.tsx b/apps/desktop/src/i18n/profile-provider.tsx
index 68cf45e1dd0..21fbc0fa272 100644
--- a/apps/desktop/src/i18n/profile-provider.tsx
+++ b/apps/desktop/src/i18n/profile-provider.tsx
@@ -33,12 +33,15 @@ export function ProfileI18nProvider({ children }: { children: ReactNode }) {
/**
* Mirror the active backend's language packs into the app-locale registry:
* `i18n.languages` for the switcher, `i18n.catalog {surface:'desktop'}` for the
- * language `display.language` asks for. Re-syncs when the socket comes up,
- * when the requested language changes, and on a profile/connection switch
- * (whose first act is dropping the previous backend's layer — a soft
- * re-home must not leak one profile's strings into the next).
+ * language `display.language` asks for. Re-syncs when the socket comes up or
+ * its client is published (boot reports `open` before `$gateway` is set, so
+ * the client is a dependency, not a one-off read), when the requested
+ * language changes, and on a profile/connection switch (whose first act is
+ * dropping the previous backend's layer — a soft re-home must not leak one
+ * profile's strings into the next).
*/
function useBackendLocalePacks(scopeKey: string): void {
+ const gateway = useStore($gateway)
const gatewayState = useStore($gatewayState)
const requested = useStore($requestedLocale)
const open = gatewayState === 'open'
@@ -48,8 +51,6 @@ function useBackendLocalePacks(scopeKey: string): void {
}, [scopeKey])
useEffect(() => {
- const gateway = $gateway.get()
-
if (!open || !gateway) {
return
}
@@ -65,5 +66,5 @@ function useBackendLocalePacks(scopeKey: string): void {
return () => {
current = false
}
- }, [open, requested, scopeKey])
+ }, [gateway, open, requested, scopeKey])
}