Skip to content

Commit a3f60ad

Browse files
committed
fix(ui): make UI language change consistent (Zoo-Code-Org#2185)
1 parent 5cc5a37 commit a3f60ad

238 files changed

Lines changed: 1190 additions & 1251 deletions

File tree

Some content is hidden

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

‎.roo/rules-translate/001-general-rules.md‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@
3030
# 4. WEBVIEW UI LOCALIZATION (webview-ui/src/)
3131

3232
- Located in webview-ui/src/i18n/locales/
33-
- Uses standard React i18next patterns with the useTranslation hook
33+
- Use react-i18next's `useTranslation` and `Trans` (`import { Trans, useTranslation } from "react-i18next"`):
34+
- Always pass the component's `t` to `Trans` (`<Trans t={t} i18nKey="..." />`); `Trans` does not follow language changes on its own (ESLint enforces this)
35+
- Never use the global `t` from `i18next` (`i18next.t`); memoized output would keep showing the previous language after a switch (ESLint enforces this). Pass `t` to helpers instead, as `utils/format.ts` and `utils/validate.ts` do
36+
- `useTranslation()` takes fully qualified keys (`t("chat:about")`); `useTranslation("chat")` also takes keys relative to that namespace (`t("about")`); fully qualified keys still work
3437
- All user interface strings should be internationalized
3538
- Always use the Trans component with named components for text with embedded components
3639

@@ -40,6 +43,7 @@
4043

4144
```
4245
<Trans
46+
t={t}
4347
i18nKey="welcome:telemetry.changeSettings"
4448
components={{
4549
settingsLink: <VSCodeLink href="#" onClick={handleOpenSettings} />

‎.roo/skills/roo-translation/SKILL.md‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@ This guidance file is loaded at runtime and should be consulted for the latest b
7070
## WebView UI Localization (webview-ui/src/)
7171

7272
- Located in `webview-ui/src/i18n/locales/`
73-
- Uses standard React i18next patterns with the `useTranslation` hook
73+
- Use react-i18next's `useTranslation` and `Trans` (`import { Trans, useTranslation } from "react-i18next"`):
74+
- Always pass the component's `t` to `Trans` (`<Trans t={t} i18nKey="..." />`); `Trans` does not follow language changes on its own (ESLint enforces this)
75+
- Never use the global `t` from `i18next` (`i18next.t`); memoized output would keep showing the previous language after a switch (ESLint enforces this). Pass `t` to helpers instead, as `utils/format.ts` and `utils/validate.ts` do
76+
- `useTranslation()` takes fully qualified keys (`t("chat:about")`); `useTranslation("chat")` also takes keys relative to that namespace (`t("about")`); fully qualified keys still work
7477
- All user interface strings should be internationalized
7578
- Always use the `Trans` component with named components for text with embedded components
7679

@@ -86,6 +89,7 @@ React component usage:
8689

8790
```tsx
8891
<Trans
92+
t={t}
8993
i18nKey="welcome:telemetry.changeSettings"
9094
components={{
9195
settingsLink: <VSCodeLink href="#" onClick={handleOpenSettings} />,

‎webview-ui/eslint.config.mjs‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,39 @@ export default [
4040
"no-case-declarations": "off",
4141
},
4242
},
43+
{
44+
// Translations must come from the component's `t` (react-i18next's `useTranslation`), which changes
45+
// identity on a language switch; the global `t` gives React (and React Compiler caches) nothing to
46+
// invalidate on, so memoized output would keep the previous language.
47+
files: ["src/**/*.{ts,tsx}"],
48+
ignores: ["src/i18n/**"],
49+
rules: {
50+
"no-restricted-imports": [
51+
"error",
52+
{
53+
paths: [
54+
{
55+
name: "i18next",
56+
importNames: ["t"],
57+
message: "Use t from react-i18next's useTranslation so memoized output follows language changes.",
58+
},
59+
],
60+
},
61+
],
62+
"no-restricted-syntax": [
63+
"error",
64+
{
65+
selector: "MemberExpression[object.name='i18next'][property.name='t']",
66+
message: "Use t from react-i18next's useTranslation so memoized output follows language changes.",
67+
},
68+
{
69+
selector: "JSXOpeningElement[name.name='Trans']:not(:has(JSXAttribute[name.name='t']))",
70+
message:
71+
"Pass t={t} from useTranslation: Trans does not subscribe to language changes, so a cached <Trans> keeps the previous language.",
72+
},
73+
],
74+
},
75+
},
4376
{
4477
files: ["src/__mocks__/**/*.js"],
4578
rules: {

‎webview-ui/playwright/AppProviders.tsx‎

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ import { I18nextProvider } from "react-i18next"
44
import { providerIdentifiers } from "@roo-code/types"
55

66
import { ExtensionStateContextProvider } from "@/context/ExtensionStateContext"
7-
import TranslationProvider from "@/i18n/TranslationContext"
87
import i18next, { loadTranslations } from "@/i18n/setup"
98
import { TooltipProvider } from "@/components/ui/tooltip"
10-
import { TranslationContext as PlaywrightTranslationContext } from "@src/i18n/TranslationContext"
119

1210
loadTranslations()
1311

@@ -42,16 +40,11 @@ export function AppProviders({ children, initialState }: AppProvidersProps) {
4240
return (
4341
<I18nextProvider i18n={i18next}>
4442
<ExtensionStateContextProvider initialState={{ ...defaultInitialState, ...initialState }}>
45-
<TranslationProvider>
46-
<PlaywrightTranslationContext.Provider
47-
value={{ t: (key, options) => i18next.t(key, options), i18n: i18next }}>
48-
<QueryClientProvider client={queryClient}>
49-
<TooltipProvider>
50-
<div data-testid="ct-app-shell">{children}</div>
51-
</TooltipProvider>
52-
</QueryClientProvider>
53-
</PlaywrightTranslationContext.Provider>
54-
</TranslationProvider>
43+
<QueryClientProvider client={queryClient}>
44+
<TooltipProvider>
45+
<div data-testid="ct-app-shell">{children}</div>
46+
</TooltipProvider>
47+
</QueryClientProvider>
5548
</ExtensionStateContextProvider>
5649
</I18nextProvider>
5750
)

‎webview-ui/playwright/TranslationContext.ts‎

Lines changed: 0 additions & 13 deletions
This file was deleted.

‎webview-ui/playwright/vite.config.ts‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,6 @@ export default defineConfig({
4848
find: "@src/context/ExtensionStateContext",
4949
replacement: path.resolve(dirname, "ExtensionStateContext.tsx"),
5050
},
51-
{
52-
find: "@src/i18n/TranslationContext",
53-
replacement: path.resolve(dirname, "TranslationContext.ts"),
54-
},
5551
{ find: "@", replacement: path.resolve(webviewRoot, "src") },
5652
{ find: "@src", replacement: path.resolve(webviewRoot, "src") },
5753
{ find: "@roo", replacement: path.resolve(webviewRoot, "../src/shared") },

‎webview-ui/src/App.tsx‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { QueryClient, QueryClientProvider } from "@tanstack/react-query"
44

55
import { type ExtensionMessage, TelemetryEventName } from "@roo-code/types"
66

7-
import TranslationProvider from "./i18n/TranslationContext"
7+
import "./i18n/setup"
88
import { MarketplaceViewStateManager } from "./components/marketplace/MarketplaceViewStateManager"
99

1010
import { vscode } from "./utils/vscode"
@@ -344,13 +344,11 @@ const queryClient = new QueryClient()
344344
const AppWithProviders = () => (
345345
<ErrorBoundary>
346346
<ExtensionStateContextProvider>
347-
<TranslationProvider>
348-
<QueryClientProvider client={queryClient}>
349-
<TooltipProvider delayDuration={STANDARD_TOOLTIP_DELAY}>
350-
<App />
351-
</TooltipProvider>
352-
</QueryClientProvider>
353-
</TranslationProvider>
347+
<QueryClientProvider client={queryClient}>
348+
<TooltipProvider delayDuration={STANDARD_TOOLTIP_DELAY}>
349+
<App />
350+
</TooltipProvider>
351+
</QueryClientProvider>
354352
</ExtensionStateContextProvider>
355353
</ErrorBoundary>
356354
)

‎webview-ui/src/__tests__/App.spec.tsx‎

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -114,36 +114,18 @@ vi.mock("react-i18next", () => {
114114
MockedComponent.displayName = `withTranslation(${Component.displayName || Component.name || "Component"})`
115115
return MockedComponent
116116
},
117-
Trans: ({ children }: { children: React.ReactNode }) => <>{children}</>,
118-
useTranslation: () => {
119-
return {
120-
t: tFunction,
121-
i18n: {
122-
t: tFunction,
123-
changeLanguage: vi.fn(() => Promise.resolve()),
124-
},
125-
}
126-
},
127-
initReactI18next: {
128-
type: "3rdParty",
129-
init: vi.fn(),
130-
},
131-
}
132-
})
133-
134-
// Mock TranslationProvider to pass through children
135-
vi.mock("@src/i18n/TranslationContext", () => {
136-
const tFunction = (key: string) => key
137-
return {
138-
__esModule: true,
139-
default: ({ children }: { children: React.ReactNode }) => <>{children}</>,
140-
useAppTranslation: () => ({
117+
useTranslation: () => ({
141118
t: tFunction,
142119
i18n: {
143120
t: tFunction,
144121
changeLanguage: vi.fn(() => Promise.resolve()),
145122
},
146123
}),
124+
Trans: ({ children }: { children: React.ReactNode }) => <>{children}</>,
125+
initReactI18next: {
126+
type: "3rdParty",
127+
init: vi.fn(),
128+
},
147129
}
148130
})
149131

‎webview-ui/src/__tests__/FileChangesPanel.spec.tsx‎

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import React from "react"
22
import { act } from "@testing-library/react"
33
import { fireEvent, render, screen } from "@/utils/test-utils"
44
import type { ClineMessage } from "@roo-code/types"
5-
import { TranslationProvider } from "@/i18n/__mocks__/TranslationContext"
65
import FileChangesPanel from "../components/chat/FileChangesPanel"
76

87
const mockPostMessage = vi.fn()
@@ -69,11 +68,7 @@ function createFileEditMessage(
6968
}
7069

7170
function renderPanel(messages: ClineMessage[] | undefined, taskId?: string) {
72-
return render(
73-
<TranslationProvider>
74-
<FileChangesPanel clineMessages={messages} taskId={taskId} />
75-
</TranslationProvider>,
76-
)
71+
return render(<FileChangesPanel clineMessages={messages} taskId={taskId} />)
7772
}
7873

7974
describe("FileChangesPanel", () => {
@@ -333,11 +328,7 @@ describe("FileChangesPanel", () => {
333328
expandRow()
334329
expect(requestsOfType("readOriginalContent")).toHaveLength(1)
335330

336-
rerender(
337-
<TranslationProvider>
338-
<FileChangesPanel clineMessages={messages} taskId="task-1" />
339-
</TranslationProvider>,
340-
)
331+
rerender(<FileChangesPanel clineMessages={messages} taskId="task-1" />)
341332
fireEvent.click(screen.getByTestId("accordian-toggle"))
342333

343334
const requests = requestsOfType("readOriginalContent")
@@ -347,11 +338,7 @@ describe("FileChangesPanel", () => {
347338

348339
it("does not send a duplicate request when switching A -> B -> A before the first response arrives", () => {
349340
const messages = [createEditWithOriginal({ originalContentLength: 5000 })]
350-
const panel = (taskId: string) => (
351-
<TranslationProvider>
352-
<FileChangesPanel clineMessages={messages} taskId={taskId} />
353-
</TranslationProvider>
354-
)
341+
const panel = (taskId: string) => (<FileChangesPanel clineMessages={messages} taskId={taskId} />)
355342
const { rerender } = renderPanel(messages, "task-A")
356343
expandRow()
357344
expect(requestsOfType("readOriginalContent")).toHaveLength(1)
@@ -372,11 +359,7 @@ describe("FileChangesPanel", () => {
372359

373360
it("requests again after a response that arrived for a task that is no longer current", () => {
374361
const messages = [createEditWithOriginal({ originalContentLength: 5000 })]
375-
const panel = (taskId: string) => (
376-
<TranslationProvider>
377-
<FileChangesPanel clineMessages={messages} taskId={taskId} />
378-
</TranslationProvider>
379-
)
362+
const panel = (taskId: string) => (<FileChangesPanel clineMessages={messages} taskId={taskId} />)
380363
const { rerender } = renderPanel(messages, "task-A")
381364
expandRow()
382365

@@ -397,11 +380,7 @@ describe("FileChangesPanel", () => {
397380
expandRow()
398381
mockPostMessage.mockClear()
399382

400-
rerender(
401-
<TranslationProvider>
402-
<FileChangesPanel clineMessages={messages} taskId="task-B" />
403-
</TranslationProvider>,
404-
)
383+
rerender(<FileChangesPanel clineMessages={messages} taskId="task-B" />)
405384

406385
expect(mockPostMessage).not.toHaveBeenCalled()
407386
})
@@ -417,11 +396,7 @@ describe("FileChangesPanel", () => {
417396
})
418397
expect(requestsOfType("readOriginalContent")).toHaveLength(1)
419398

420-
rerender(
421-
<TranslationProvider>
422-
<FileChangesPanel clineMessages={[...first]} taskId="task-A" />
423-
</TranslationProvider>,
424-
)
399+
rerender(<FileChangesPanel clineMessages={[...first]} taskId="task-A" />)
425400
fireEvent.click(screen.getByTestId("accordian-toggle"))
426401
respond({ type: "fileContent", fileContent: { path: "src/foo.ts", content: "new line\n" } })
427402

‎webview-ui/src/components/chat/Announcement.tsx‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { memo, type MouseEvent, type ReactNode, useState } from "react"
2-
import { Trans } from "react-i18next"
32
import { SiDiscord, SiReddit, SiX } from "react-icons/si"
43

54
import { Package } from "@roo/package"
6-
import { useAppTranslation } from "@src/i18n/TranslationContext"
5+
import { Trans, useTranslation } from "react-i18next"
76
import { vscode } from "@src/utils/vscode"
87
import { Dialog, DialogContent, DialogHeader, DialogTitle } from "@src/components/ui"
98
import { EXTERNAL_LINKS } from "@src/constants/externalLinks"
@@ -24,7 +23,7 @@ interface AnnouncementProps {
2423
*/
2524

2625
const Announcement = ({ hideAnnouncement }: AnnouncementProps) => {
27-
const { t } = useAppTranslation()
26+
const { t } = useTranslation()
2827
const [open, setOpen] = useState(true)
2928

3029
return (
@@ -72,7 +71,7 @@ const Announcement = ({ hideAnnouncement }: AnnouncementProps) => {
7271
</div>
7372

7473
<div className="mt-3 text-sm text-center text-vscode-descriptionForeground">
75-
<Trans i18nKey="chat:announcement.support" components={{ githubLink: <GitHubLink /> }} />
74+
<Trans t={t} i18nKey="chat:announcement.support" components={{ githubLink: <GitHubLink /> }} />
7675
</div>
7776
</div>
7877
</DialogContent>

0 commit comments

Comments
 (0)