Skip to content

Commit 94295e2

Browse files
authored
refactor(i18n): expose translation keys to static analysis (#865)
## Summary - Replace finite translation-key data flows with semantic values resolved through literal translation calls at presentation boundaries. - Remove stale and unreachable locale entries, including the non-actionable NONE remediation copy. - Make the i18n check fail on missing, incomplete, or unused translations without preservation patterns or a usage manifest. - Add mapping tests and document the translation-key architecture in repository guidance. ## Verification - npm run lint passes with zero errors and twelve pre-existing warnings. - npm run typecheck passes. - npm run test:i18n passes with 1,128 keys and zero unused keys. - npm run test:ci passes with 231 suites and 2,596 tests passing. - npm run test:scripts passes with 28 suites and 189 tests passing. - A temporary unused translation probe caused the strict i18n check to fail and was removed afterward.
1 parent a14fd57 commit 94295e2

67 files changed

Lines changed: 874 additions & 522 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎AGENTS.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Keep this file to repository-specific behavior and safeguards. Agent tooling is
77
- Standard checks are `npm run lint`, `npm run typecheck`, `npm run test:ci`, and `npm run test:scripts`.
88
- Before committing JavaScript or TypeScript, run `npx eslint` on the staged JS/TS files. Errors block; warnings do not.
99
- `i18next/no-literal-string` is an error. Put user-facing copy in translations. For technical literals, use a narrowly scoped disable with a reason; never exclude a mixed-copy file.
10+
- Keep finite translation choices statically visible: state, props, helpers, and option metadata carry semantic values, while the nearest presentation boundary maps them through literal `t('namespace:key')` calls. Do not pass translation keys as application data or hide them with `preservePatterns` or artificial usage manifests; `npm run test:i18n` uses `i18next-cli status` and `status --unused` to reject missing, incomplete, and stale keys.
1011

1112
## E2E testing
1213

‎CLAUDE.md‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,33 @@ One gap the rule still has: a **module-scope** `const` is not inspected, only fu
9494

9595
---
9696

97+
## Keep Translation Keys at the Presentation Boundary
98+
99+
For a finite domain, application state and data carry semantic values, not i18next keys. Resolve the semantic value with an exhaustive switch whose branches call literal `t('namespace:key')` expressions at the nearest presentation boundary.
100+
101+
```tsx
102+
// bad — the analyzer loses the real usage across metadata, state, and props
103+
const options = [{ value: 'newest', labelKey: 'servers:filter.newestFirst' }] as const
104+
const [selected] = useState(options[0])
105+
return <Text>{t(selected.labelKey)}</Text>
106+
107+
// good — state remains semantic and every real key is visible in the AST
108+
function getSortOrderLabel(order: 'newest' | 'oldest', t: TFunction<'servers'>) {
109+
switch (order) {
110+
case 'newest':
111+
return t('filter.newestFirst')
112+
case 'oldest':
113+
return t('filter.oldestFirst')
114+
}
115+
}
116+
```
117+
118+
Apply the same rule to helper return values, component props, arrays, and generic option records. Keep localization out of domain/business helpers; put the exhaustive translation resolver in the nearest UI utility or component and call it during render so language changes remain reactive.
119+
120+
Do not use `preservePatterns`, wildcard preservation, fake or unreachable `t()` calls, comment-only extraction, or a static-usage manifest to compensate for finite-key indirection. Those mechanisms can make stale keys look used. `npm run test:i18n` runs both `i18next-cli status` and `status --unused`, so missing, incomplete, and genuinely unused locale entries must continue to fail CI. See [`docs/followups/mobile/09-i18n-guardrails.md`](./docs/followups/mobile/09-i18n-guardrails.md).
121+
122+
---
123+
97124
## No `unknown` or `any` Without Explicit Permission
98125

99126
Never introduce `unknown` or `any` in new code without the user's explicit approval. If a type boundary genuinely requires one, stop and ask — don't default to it as a workaround for a type error.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import i18n from '@/test-utils/i18n-setup'
2+
import {
3+
getSlowLoadingTitle,
4+
type SlowLoadingTitleVariant,
5+
} from '@/components/conversation/slowLoadingTitle'
6+
7+
describe('getSlowLoadingTitle', () => {
8+
it.each<[SlowLoadingTitleVariant, string]>([
9+
[0, 'Untangling a long thread…'],
10+
[1, 'Messages are fashionably late…'],
11+
[2, 'Your messages hit some traffic…'],
12+
])('translates semantic variant %s', (variant, expected) => {
13+
expect(
14+
getSlowLoadingTitle(variant, i18n.getFixedT('en', ['conversation', 'common'])),
15+
).toBe(expected)
16+
})
17+
})

‎__tests__/unit/components/onboarding/OnboardingShell.test.tsx‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const renderShell = (direction: 1 | -1 | 0 = 0) => render(
1313
onNext={jest.fn()}
1414
onBack={jest.fn()}
1515
onSkip={jest.fn()}
16-
skipLabelKey="shell.pairLater"
16+
skipLabel="pairLater"
1717
>
1818
<Text>Step</Text>
1919
</OnboardingShell>,
@@ -27,6 +27,13 @@ function getEnteringAnimation(result: Awaited<ReturnType<typeof renderShell>>) {
2727
}
2828

2929
describe('OnboardingShell RTL navigation', () => {
30+
it('renders the semantic pair-later label', async () => {
31+
await i18n.changeLanguage('en')
32+
const { getByText } = await renderShell()
33+
34+
expect(getByText('Pair later')).toBeTruthy()
35+
})
36+
3037
it('keeps Back at the LTR start edge with a left-pointing arrow', async () => {
3138
await i18n.changeLanguage('en')
3239
const { getByTestId, queryByTestId } = await renderShell()
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import i18n from '@/test-utils/i18n-setup'
2+
import {
3+
getSessionsLayoutLabel,
4+
getSortByLabel,
5+
getSortOrderLabel,
6+
} from '@/components/servers/filterSortLabels'
7+
8+
describe('filter and sort labels', () => {
9+
const t = i18n.getFixedT('en', ['servers', 'settings', 'sessions'] as const)
10+
11+
it('translates every sessions layout semantic value', () => {
12+
expect(getSessionsLayoutLabel('tree', t)).toBe('Tree')
13+
expect(getSessionsLayoutLabel('hub', t)).toBe('Hub')
14+
expect(getSessionsLayoutLabel('classic', t)).toBe('Classic')
15+
})
16+
17+
it('translates every sort field semantic value', () => {
18+
expect(getSortByLabel('lastActivity', t)).toBe('Last message')
19+
expect(getSortByLabel('projectName', t)).toBe('Project name')
20+
expect(getSortByLabel('startedAt', t)).toBe('Created date')
21+
expect(getSortByLabel('status', t)).toBe('Status')
22+
})
23+
24+
it('translates every sort order semantic value', () => {
25+
expect(getSortOrderLabel('desc', t)).toBe('Newest first')
26+
expect(getSortOrderLabel('asc', t)).toBe('Oldest first')
27+
})
28+
})
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import i18n from '@/test-utils/i18n-setup'
2+
import {
3+
getHostPressureBannerLabel,
4+
getHostPressureDetectedLabel,
5+
getHostPressureWhatToDoLabel,
6+
getHostPressureWhyFineLabel,
7+
} from '@/components/servers/hostPressureLabels'
8+
9+
describe('host-pressure labels', () => {
10+
const t = i18n.getFixedT('en', 'servers')
11+
12+
it('translates semantic pressure levels and constraints', () => {
13+
expect(getHostPressureBannerLabel('elevated', 'memory', 'Studio', t)).toBe(
14+
'Studio is under memory pressure.',
15+
)
16+
expect(getHostPressureBannerLabel('critical', 'load', 'Studio', t)).toBe(
17+
'Studio is running at full load.',
18+
)
19+
expect(getHostPressureBannerLabel('elevated', undefined, 'Studio', t)).toBe(
20+
'Studio is under pressure.',
21+
)
22+
})
23+
24+
it('translates every resource reason in both explanation contexts', () => {
25+
expect(getHostPressureDetectedLabel('memory', t)).toBe('The computer is low on free RAM.')
26+
expect(getHostPressureDetectedLabel('event_loop', t)).toContain('Threadbase server')
27+
expect(getHostPressureDetectedLabel('load', t)).toBe('The CPU is busy.')
28+
expect(getHostPressureWhyFineLabel('memory', t)).toContain('swap RAM to disk')
29+
expect(getHostPressureWhyFineLabel('event_loop', t)).toContain('New ones wait')
30+
expect(getHostPressureWhyFineLabel('load', t)).toContain('Background compiles')
31+
})
32+
33+
it('translates OS-specific and generic advice', () => {
34+
expect(getHostPressureWhatToDoLabel('darwin', t)).toContain('On this Mac')
35+
expect(getHostPressureWhatToDoLabel('linux', t)).toContain('On this Linux machine')
36+
expect(getHostPressureWhatToDoLabel('win32', t)).toContain('On this Windows PC')
37+
expect(getHostPressureWhatToDoLabel(undefined, t)).toContain('On the computer')
38+
})
39+
})
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import i18n from '@/test-utils/i18n-setup'
2+
import { getSessionStatusLabel } from '@/components/sessions/sessionStatusLabel'
3+
import type { SessionStatusLabel } from '@/lib/sessionPresentation'
4+
5+
describe('getSessionStatusLabel', () => {
6+
it.each<[SessionStatusLabel, string]>([
7+
['running', 'Running'],
8+
['waiting', 'Waiting'],
9+
['idle', 'Idle'],
10+
['externalLive', 'External'],
11+
['historical', 'History'],
12+
['interrupted', 'Interrupted'],
13+
['interruptedWaiting', 'Was waiting'],
14+
['resumed', 'Resumed'],
15+
['onHold', 'On hold'],
16+
['completed', 'Completed'],
17+
['failed', 'Failed'],
18+
['unavailablePath', 'Unavailable'],
19+
['unavailableWorktree', 'Worktree gone'],
20+
['stale', 'Stale'],
21+
['starting', 'Starting up'],
22+
])('translates the %s semantic status', (status, expected) => {
23+
expect(getSessionStatusLabel(status, i18n.getFixedT('en', 'sessions'))).toBe(expected)
24+
})
25+
})

‎__tests__/unit/lib/locale.test.ts‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
import i18next from 'i18next'
22
import {
3+
getSupportedLocaleLabel,
34
isRTLLocale,
45
resolveSupportedLocale,
56
SUPPORTED_LOCALES,
67
} from '@/lib/locale'
78

89
describe('locale', () => {
10+
it('resolves every locale code to its translated display label', () => {
11+
const t = i18next.getFixedT('en', 'settings')
12+
expect(getSupportedLocaleLabel('en', t)).toBe('English')
13+
expect(getSupportedLocaleLabel('he', t)).toBe('עברית')
14+
expect(getSupportedLocaleLabel('ar', t)).toBe('العربية')
15+
expect(getSupportedLocaleLabel('ru', t)).toBe('Русский')
16+
})
17+
918
it('selects the first supported device preference', () => {
1019
expect(
1120
resolveSupportedLocale([
@@ -25,10 +34,10 @@ describe('locale', () => {
2534
// silently disagree with the one the UI actually renders from.
2635
it('lists every supported locale without duplicating its direction', () => {
2736
expect(SUPPORTED_LOCALES).toEqual([
28-
{ code: 'en', labelKey: 'language.english' },
29-
{ code: 'he', labelKey: 'language.hebrew' },
30-
{ code: 'ar', labelKey: 'language.arabic' },
31-
{ code: 'ru', labelKey: 'language.russian' },
37+
{ code: 'en' },
38+
{ code: 'he' },
39+
{ code: 'ar' },
40+
{ code: 'ru' },
3241
])
3342
})
3443

‎__tests__/unit/lib/sessionPresentation.test.ts‎

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ describe('deriveSessionPresentation', () => {
2222
live: true,
2323
capabilities: { canSendInput: true, canCancel: true, isObserveOnly: false },
2424
})
25-
expect(deriveSessionPresentation(base({ status: 'waiting_input' })).labelKey).toBe(
26-
'status.waiting',
25+
expect(deriveSessionPresentation(base({ status: 'waiting_input' })).statusLabel).toBe(
26+
'waiting',
2727
)
2828
})
2929

@@ -60,7 +60,7 @@ describe('deriveSessionPresentation', () => {
6060
it('marks on_hold and completed/failed from runtime status strings', () => {
6161
expect(deriveSessionPresentation(base({ status: 'on_hold' })).kind).toBe('on_hold')
6262
expect(deriveSessionPresentation(base({ status: 'completed' })).kind).toBe('completed')
63-
expect(deriveSessionPresentation(base({ status: 'failed' })).labelKey).toBe('status.failed')
63+
expect(deriveSessionPresentation(base({ status: 'failed' })).statusLabel).toBe('failed')
6464
})
6565

6666
it('marks gone external processes as stale', () => {
@@ -87,20 +87,20 @@ describe('deriveSessionPresentation', () => {
8787
),
8888
).toMatchObject({
8989
kind: 'historical',
90-
labelKey: 'status.historical',
90+
statusLabel: 'historical',
9191
capabilities: { canResume: true, isObserveOnly: true },
9292
})
9393
})
9494

9595
it('labels an interrupted historical session by what it was doing', () => {
9696
const historical = { status: 'idle', ownership: 'historical' as const, ptyAttached: false }
9797
expect(
98-
deriveSessionPresentation(base({ ...historical, interruptedStatus: 'running' })).labelKey,
99-
).toBe('status.interrupted')
98+
deriveSessionPresentation(base({ ...historical, interruptedStatus: 'running' })).statusLabel,
99+
).toBe('interrupted')
100100
expect(
101101
deriveSessionPresentation(base({ ...historical, interruptedStatus: 'waiting_input' }))
102-
.labelKey,
103-
).toBe('status.interruptedWaiting')
102+
.statusLabel,
103+
).toBe('interruptedWaiting')
104104
})
105105

106106
it('keeps an interrupted session idle, resumable and grey', () => {
@@ -136,8 +136,8 @@ describe('deriveSessionPresentation', () => {
136136
lifecycle: 'failed',
137137
failureReason: 'boom',
138138
}),
139-
).labelKey,
140-
).toBe('status.failed')
139+
).statusLabel,
140+
).toBe('failed')
141141
})
142142

143143
it('treats lifecycle resumable as historical (resume, observe-only)', () => {
@@ -159,7 +159,7 @@ describe('deriveSessionPresentation', () => {
159159
),
160160
).toMatchObject({
161161
kind: 'starting',
162-
labelKey: 'status.starting',
162+
statusLabel: 'starting',
163163
live: false,
164164
})
165165
})
@@ -170,7 +170,7 @@ describe('deriveSessionPresentation', () => {
170170
// @ts-expect-error — a newer streamer can emit a lifecycle this build has never heard of
171171
base({ status: 'idle', ptyAttached: false, lifecycle: 'quarantined' }),
172172
),
173-
).toMatchObject({ kind: 'idle', labelKey: 'status.idle' })
173+
).toMatchObject({ kind: 'idle', statusLabel: 'idle' })
174174
})
175175
})
176176

@@ -291,7 +291,7 @@ describe('deriveConversationPresentation', () => {
291291
}),
292292
).toMatchObject({
293293
kind: 'unavailable',
294-
labelKey: 'status.unavailablePath',
294+
statusLabel: 'unavailablePath',
295295
capabilities: { canResume: false },
296296
})
297297
})

‎__tests__/unit/services/server-diagnostics.test.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
parseServerDiagnosticsReport,
33
SERVER_DIAGNOSTICS_CONTRACT_VERSION,
4+
type RemediationCode,
45
} from '@/types/server-diagnostics'
56
import {
67
isSupportedDiagnosticsContract,
@@ -53,6 +54,10 @@ describe('parseServerDiagnosticsReport', () => {
5354
})
5455

5556
describe('serverDiagnostics helpers', () => {
57+
function actionableRemediation(code: RemediationCode): Exclude<RemediationCode, 'NONE'> | null {
58+
return needsRemediation(code) ? code : null
59+
}
60+
5661
const report = parseServerDiagnosticsReport({
5762
contractVersion: 1,
5863
generatedAt: '2026-07-24T12:00:00.000Z',
@@ -75,6 +80,8 @@ describe('serverDiagnostics helpers', () => {
7580
it('needsRemediation ignores NONE', () => {
7681
expect(needsRemediation('NONE')).toBe(false)
7782
expect(needsRemediation('PTY_UNAVAILABLE')).toBe(true)
83+
expect(actionableRemediation('NONE')).toBeNull()
84+
expect(actionableRemediation('PTY_UNAVAILABLE')).toBe('PTY_UNAVAILABLE')
7885
})
7986

8087
it('formats a sanitized plain-text report', () => {

0 commit comments

Comments
 (0)