Skip to content

Commit a92c348

Browse files
committed
fix(ui): make UI language change consistent (Zoo-Code-Org#2185)
1 parent 2baac5e commit a92c348

232 files changed

Lines changed: 1188 additions & 1239 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: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ Localize all strings into the following locale files: ca, de, en, es, fr, hi, id
2929

3030
The VSCode extension has two main areas that require localization:
3131

32-
| Component | Path | Purpose |
33-
|-----------|------|---------|
34-
| **Core Extension** | `src/i18n/locales/` | Extension backend strings |
35-
| **WebView UI** | `webview-ui/src/i18n/locales/` | User interface strings |
32+
| Component | Path | Purpose |
33+
| ------------------ | ------------------------------ | ------------------------- |
34+
| **Core Extension** | `src/i18n/locales/` | Extension backend strings |
35+
| **WebView UI** | `webview-ui/src/i18n/locales/` | User interface strings |
3636

3737
## Brand Voice, Tone, and Word Choice
3838

@@ -70,24 +70,30 @@ 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

7780
### Trans Component Example
7881

7982
Translation string:
83+
8084
```json
8185
"changeSettings": "You can always change this at the bottom of the <settingsLink>settings</settingsLink>"
8286
```
8387

8488
React component usage:
89+
8590
```tsx
8691
<Trans
87-
i18nKey="welcome:telemetry.changeSettings"
88-
components={{
89-
settingsLink: <VSCodeLink href="#" onClick={handleOpenSettings} />
90-
}}
92+
t={t}
93+
i18nKey="welcome:telemetry.changeSettings"
94+
components={{
95+
settingsLink: <VSCodeLink href="#" onClick={handleOpenSettings} />,
96+
}}
9197
/>
9298
```
9399

@@ -105,19 +111,19 @@ React component usage:
105111

106112
1. First add or modify English strings, then ask for confirmation before translating to all other languages
107113
2. Use this process for each localization task:
108-
1. Identify where the string appears in the UI/codebase
109-
2. Understand the context and purpose of the string
110-
3. Update English translation first
111-
4. Use the `search_files` tool to find JSON keys that are near new keys in English translations but do not yet exist in the other language files for `apply_diff` SEARCH context
112-
5. Create appropriate translations for all other supported languages utilizing the `search_files` result using `apply_diff` without reading every file
113-
6. Do not output the translated text into the chat, just modify the files
114-
7. Validate your changes with the missing translations script
114+
1. Identify where the string appears in the UI/codebase
115+
2. Understand the context and purpose of the string
116+
3. Update English translation first
117+
4. Use the `search_files` tool to find JSON keys that are near new keys in English translations but do not yet exist in the other language files for `apply_diff` SEARCH context
118+
5. Create appropriate translations for all other supported languages utilizing the `search_files` result using `apply_diff` without reading every file
119+
6. Do not output the translated text into the chat, just modify the files
120+
7. Validate your changes with the missing translations script
115121

116122
3. Flag or comment if an English source string is incomplete ("please see this...") to avoid truncated or unclear translations
117123

118124
4. For UI elements, distinguish between:
119-
- Button labels: Use short imperative commands ("Save", "Cancel")
120-
- Tooltip text: Can be slightly more descriptive
125+
- Button labels: Use short imperative commands ("Save", "Cancel")
126+
- Tooltip text: Can be slightly more descriptive
121127

122128
5. Preserve the original perspective: If text is a user command directed at the software, ensure the translation maintains this direction
123129

‎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: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import React from "react"
22
import { fireEvent, render, screen } from "@/utils/test-utils"
33
import type { ClineMessage } from "@roo-code/types"
4-
import { TranslationProvider } from "@/i18n/__mocks__/TranslationContext"
54
import FileChangesPanel from "../components/chat/FileChangesPanel"
65

76
const mockPostMessage = vi.fn()
@@ -65,11 +64,7 @@ function createFileEditMessage(
6564
}
6665

6766
function renderPanel(messages: ClineMessage[] | undefined) {
68-
return render(
69-
<TranslationProvider>
70-
<FileChangesPanel clineMessages={messages} />
71-
</TranslationProvider>,
72-
)
67+
return render(<FileChangesPanel clineMessages={messages} />)
7368
}
7469

7570
describe("FileChangesPanel", () => {

‎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)