Repository navigation
fix(ui): make UI language change consistent (#2185) #2194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rrewll
wants to merge
4
commits into
Zoo-Code-Org:main
Choose a base branch
from
rrewll:fix/2185-make-language-switch-consistent
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c0cded8
fix(ui): make UI language change consistent (#2185)
rrewll b1a3432
test: add tests for TaskItemFooter and IssueFooter to check language …
rrewll ec2aac4
fix: fix i18n dot key selector overriting
rrewll ba97ea3
fix: return back {count: tag} for "marketplace:filters.tags.clear"
rrewll File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| // Guards that keep translated output in step with a language switch. Built-in no-restricted-syntax only. | ||
| // Translations must come from the component's `t` (webview-ui's `useAppTranslation`), which changes identity | ||
| // on a language switch; the global instance's `t` gives React (and React Compiler caches) nothing to | ||
| // invalidate on, so memoized output would keep the previous language. | ||
|
|
||
| // Selectors cannot follow bindings, so the instance is recognised by name: a default import or | ||
| // `useTranslation().i18n` held as `i18n`/`i18next`. The aliasing selectors below keep it under those names. | ||
| // Passing the instance as an argument and reading `.t` from the parameter is not caught. | ||
| const i18nInstanceName = "/^(i18n|i18next)$/" | ||
| const i18nInstance = `:matches([name=${i18nInstanceName}], MemberExpression[property.name=${i18nInstanceName}])` | ||
|
|
||
| // i18next itself and modules that re-export its instance as their default (webview-ui's i18n/setup). | ||
| const i18nInstanceSource = "/^(i18next|(.*\\/)?i18n\\/setup|\\.\\.?\\/setup)$/" | ||
|
|
||
| const globalTMessage = "Use t from useAppTranslation so memoized output follows language changes." | ||
| const aliasMessage = | ||
| "Keep the i18next instance named i18n or i18next: the global-t check recognises it by name, so an alias hides i18n.t." | ||
|
|
||
| export const i18nLanguageSwitchSelectors = [ | ||
| { | ||
| // Global t read off the instance: i18next.t(...), i18n?.t(...), i18next["t"], useTranslation().i18n.t. | ||
| selector: `MemberExpression:matches([property.name='t'][computed=false], [property.value='t']):has(> ${i18nInstance}.object)`, | ||
| message: globalTMessage, | ||
| }, | ||
| { | ||
| // Global t destructured from the instance: const { t } = i18next, const { t: translate } = i18n. | ||
| // Other properties stay allowed: const { language } = i18next. | ||
| selector: `VariableDeclarator:has(> ${i18nInstance}.init) > ObjectPattern > Property[key.name='t']`, | ||
| message: globalTMessage, | ||
| }, | ||
| { | ||
| // Default import under another name: import lang from "i18next", import { default as lang } from "@/i18n/setup". | ||
| // Named imports such as initReactI18next or `type TFunction` are other specifiers and are not matched. | ||
| selector: `ImportDeclaration[source.value=${i18nInstanceSource}] > :matches(ImportDefaultSpecifier, ImportSpecifier[imported.name='default']):not([local.name=${i18nInstanceName}])`, | ||
| message: aliasMessage, | ||
| }, | ||
| { | ||
| // Namespace import: import * as lang from "i18next" exposes the instance's t as lang.t whatever lang is called. | ||
| selector: `ImportDeclaration[source.value=${i18nInstanceSource}] > ImportNamespaceSpecifier`, | ||
| message: aliasMessage, | ||
| }, | ||
| { | ||
| // Re-export under another name: export { default as lang } from "i18next" lets importers alias it unseen. | ||
| selector: `ExportNamedDeclaration[source.value=${i18nInstanceSource}] > ExportSpecifier[local.name='default']:not([exported.name=${i18nInstanceName}])`, | ||
| message: aliasMessage, | ||
| }, | ||
| { | ||
| // Variable alias: const lang = i18next, const lang = useTranslation().i18n. | ||
| // Keeping the name is fine (const i18n = useTranslation().i18n); calls such as i18next.createInstance() are not | ||
| // the instance itself and are not matched. | ||
| selector: `VariableDeclarator[id.type='Identifier']:not([id.name=${i18nInstanceName}]):has(> ${i18nInstance}.init)`, | ||
| message: aliasMessage, | ||
| }, | ||
| { | ||
| // Assignment alias to an existing variable: lang = i18n. | ||
| selector: `AssignmentExpression:not([left.name=${i18nInstanceName}]):has(> ${i18nInstance}.right)`, | ||
| message: aliasMessage, | ||
| }, | ||
| { | ||
| // Destructuring rename, in declarations and parameters: const { i18n: lang } = useTranslation(), | ||
| // ({ i18n: lang }) => ..., and a nested pattern such as const { i18n: { t } } = useTranslation(). | ||
| // The shorthand const { i18n } = useTranslation() keeps the name and is allowed. | ||
| selector: `ObjectPattern > Property[key.name=${i18nInstanceName}]:not([value.name=${i18nInstanceName}])`, | ||
| message: aliasMessage, | ||
| }, | ||
| { | ||
| // <Trans> without its own t={t}: it reads the global instance, so a memoized element keeps the old language. | ||
| // Direct child only: <Trans components={{ b: <Foo t={1} /> }} /> is allowed. | ||
| selector: "JSXOpeningElement[name.name='Trans']:not(:has(> JSXAttribute[name.name='t']))", | ||
| message: | ||
| "Pass t={t} from useAppTranslation: Trans does not subscribe to language changes, so a cached <Trans> keeps the previous language.", | ||
| }, | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| import assert from "node:assert/strict" | ||
| import { describe, it } from "node:test" | ||
|
|
||
| import { Linter } from "eslint" | ||
| import typescriptEslint from "typescript-eslint" | ||
|
|
||
| import { i18nLanguageSwitchSelectors } from "./i18n-language-switch.js" | ||
|
|
||
| const linter = new Linter({ configType: "flat" }) | ||
| const config = [ | ||
| { | ||
| files: ["**/*.tsx"], | ||
| languageOptions: { parser: typescriptEslint.parser, parserOptions: { ecmaFeatures: { jsx: true } } }, | ||
| rules: { "no-restricted-syntax": ["error", ...i18nLanguageSwitchSelectors] }, | ||
| }, | ||
| ] | ||
|
|
||
| const lint = (code) => linter.verify(code, config, { filename: "example.tsx" }).map((message) => message.message) | ||
|
|
||
| describe("i18n language switch: global t", () => { | ||
| for (const code of [ | ||
| 'i18next.t("chat:about")', | ||
| 'import i18n from "i18next"\ni18n.t("chat:about")', | ||
| 'i18n?.t("chat:about")', | ||
| 'i18next["t"]("chat:about")', | ||
| 'useTranslation().i18n.t("chat:about")', | ||
| "const { t } = i18next", | ||
| "const { t: translate } = i18n", | ||
| "const { t } = useTranslation().i18n", | ||
| ]) { | ||
| it(`flags: ${code}`, () => { | ||
| assert.equal(lint(code).length, 1) | ||
| }) | ||
| } | ||
|
|
||
| for (const code of [ | ||
| 'const { t } = useTranslation()\nt("chat:about")', | ||
| 'i18next.changeLanguage("es")', | ||
| "i18n.language", | ||
| "i18next[t]", | ||
| "const { language } = i18n", | ||
| 'other.t("chat:about")', | ||
| ]) { | ||
| it(`allows: ${code}`, () => { | ||
| assert.deepEqual(lint(code), []) | ||
| }) | ||
| } | ||
| }) | ||
|
|
||
| describe("i18n language switch: aliasing the instance", () => { | ||
| for (const code of [ | ||
| 'import lang from "i18next"', | ||
| 'import { default as lang } from "i18next"', | ||
| 'import * as lang from "i18next"', | ||
| 'import lang from "@src/i18n/setup"', | ||
| 'import lang from "@/i18n/setup"', | ||
| 'import lang from "../setup"', | ||
| 'export { default as lang } from "i18next"', | ||
| "const lang = i18next", | ||
| "const lang = useTranslation().i18n", | ||
| "lang = i18n", | ||
| "const { i18n: lang } = useTranslation()", | ||
| "const { i18n: { t } } = useTranslation()", | ||
| "function Foo({ i18n: lang }) {}", | ||
| ]) { | ||
| it(`flags: ${code}`, () => { | ||
| assert.equal(lint(code).length, 1) | ||
| }) | ||
| } | ||
|
|
||
| for (const code of [ | ||
| 'import i18next from "i18next"', | ||
| 'import i18n from "i18next"', | ||
| 'import i18next, { type TFunction } from "i18next"', | ||
| 'import { initReactI18next } from "react-i18next"', | ||
| 'import i18next from "@src/i18n/setup"', | ||
| 'import { default as i18next } from "i18next"', | ||
| 'import config from "./config"', | ||
| "const i18n = useTranslation().i18n", | ||
| "const { i18n } = useTranslation()", | ||
| "const { language } = i18next", | ||
| "const instance = i18next.createInstance()", | ||
| "changeLanguage(i18next)", | ||
| ]) { | ||
| it(`allows: ${code}`, () => { | ||
| assert.deepEqual(lint(code), []) | ||
| }) | ||
| } | ||
| }) | ||
|
|
||
| describe("i18n language switch: Trans", () => { | ||
| for (const code of [ | ||
| '<Trans i18nKey="chat:about" />', | ||
| "<Trans components={{ b: <Foo t={1} /> }} />", | ||
| "<Trans {...props} />", | ||
| ]) { | ||
| it(`flags a Trans without its own t: ${code}`, () => { | ||
| assert.equal(lint(code).length, 1) | ||
| }) | ||
| } | ||
|
|
||
| for (const code of [ | ||
| '<Trans t={t} i18nKey="chat:about" />', | ||
| "<Trans t={t} components={{ b: <Foo t={1} /> }} />", | ||
| '<Other i18nKey="chat:about" />', | ||
| ]) { | ||
| it(`allows: ${code}`, () => { | ||
| assert.deepEqual(lint(code), []) | ||
| }) | ||
| } | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.