Skip to content

The IDE you know, rebuilt in Rust. IntelliJ IDEA–style keymap, tool windows and search-everywhere in a fast native app: editor, code insight, debugger, Git, terminal, database and HTTP clients for 60+ languages.

fix(i18n): translate every action title (#360)

l0ng-ai22039384910c
diff --git a/src/features/settings/__tests__/keymap.test.ts b/src/features/settings/__tests__/keymap.test.ts
index 4e8ac876e..b9c5b4c7c 100644
--- a/src/features/settings/__tests__/keymap.test.ts
+++ b/src/features/settings/__tests__/keymap.test.ts
@@ -1,5 +1,5 @@
import { afterEach, describe, expect, it, vi } from "vitest";
-import { registerAction } from "@/platform/actions";
+import { allActions, registerAction } from "@/platform/actions";
import {
applyKeymapSettings,
bindingsFor,
@@ -37,6 +37,24 @@ import {
} from "../keymap/model";
import { buildKeymapTree } from "../keymap/KeymapPage";
import { groupPath } from "../keymap/groups";
+import { registerLspActions } from "@/editor/lsp/actions";
+import { _resetI18nForTests, setLocale } from "@/platform/i18n";
+
+it("finds the translated declaration action by 声明 and its English title", async () => {
+ const dispose = registerLspActions();
+ try {
+ await setLocale("zh");
+ for (const query of ["声明", "Go to Declaration or Usages"]) {
+ const tree = buildKeymapTree(allActions(), { preset: "intellij-macos", overrides: {} }, query, "");
+ const titles = (nodes: typeof tree): string[] =>
+ nodes.flatMap((node) => (node.kind === "group" ? titles(node.children) : [node.action.title]));
+ expect(titles(tree)).toContain("转到声明或用法");
+ }
+ } finally {
+ dispose();
+ _resetI18nForTests();
+ }
+});
const key = (
code: string,
diff --git a/src/platform/locales/actionTitles.test.ts b/src/platform/locales/actionTitles.test.ts
new file mode 100644
index 000000000..18274c970
--- /dev/null
+++ b/src/platform/locales/actionTitles.test.ts
@@ -0,0 +1,89 @@
+// @vitest-environment node
+import { fileURLToPath } from "node:url";
+import ts from "typescript";
+import { expect, it } from "vitest";
+import zh from "./text/zh.json";
+
+const root = fileURLToPath(new URL("../../../", import.meta.url));
+const config = ts.readConfigFile(`${root}tsconfig.json`, ts.sys.readFile);
+const parsed = ts.parseJsonConfigFileContent(config.config, ts.sys, root);
+const program = ts.createProgram(parsed.fileNames, parsed.options);
+const checker = program.getTypeChecker();
+
+/** Literal branches also cover conditional titles in generated action arrays. */
+function literals(node: ts.Expression): string[] {
+ if (ts.isStringLiteralLike(node)) return [node.text];
+ if (ts.isParenthesizedExpression(node) || ts.isAsExpression(node) || ts.isSatisfiesExpression(node))
+ return literals(node.expression);
+ if (ts.isConditionalExpression(node)) return [...literals(node.whenTrue), ...literals(node.whenFalse)];
+ if (
+ ts.isBinaryExpression(node) &&
+ [ts.SyntaxKind.QuestionQuestionToken, ts.SyntaxKind.BarBarToken].includes(node.operatorToken.kind)
+ )
+ return [...literals(node.left), ...literals(node.right)];
+ if (ts.isCallExpression(node) && ts.isIdentifier(node.expression) && node.expression.text === "tx")
+ return node.arguments[0] ? literals(node.arguments[0]) : [];
+ const type = checker.getTypeAtLocation(node);
+ return (type.isUnion() ? type.types : [type]).flatMap((part) => (part.isStringLiteral() ? [part.value] : []));
+}
+
+function isAction(type: ts.Type): boolean {
+ return ["id", "title", "run"].every((name) => checker.getPropertyOfType(type, name));
+}
+
+// Runtime user/plugin names have no source literal; tx templates are covered by textCatalog.test.ts.
+function actionTitles(): Map<string, string> {
+ const titles = new Map<string, string>();
+ const add = (node: ts.Expression) => {
+ const source = node.getSourceFile();
+ const line = source.getLineAndCharacterOfPosition(node.getStart()).line + 1;
+ for (const title of literals(node)) titles.set(title, `${source.fileName.slice(root.length)}:${line}`);
+ };
+ for (const source of program.getSourceFiles()) {
+ if (!source.fileName.startsWith(`${root}src/`) || /\.test\.tsx?$|\/__tests__\/|\/test\//.test(source.fileName))
+ continue;
+ const visit = (node: ts.Node) => {
+ if (
+ ts.isObjectLiteralExpression(node) &&
+ isAction(checker.getContextualType(node) ?? checker.getTypeAtLocation(node))
+ ) {
+ for (const property of node.properties) {
+ if (ts.isPropertyAssignment(property) && property.name.getText(source).replace(/["']/g, "") === "title")
+ add(property.initializer);
+ if (ts.isShorthandPropertyAssignment(property) && property.name.text === "title") add(property.name);
+ }
+ }
+ if (ts.isCallExpression(node)) {
+ const signature = checker.getResolvedSignature(node);
+ if (
+ signature?.parameters.some((parameter) => parameter.name === "title") &&
+ isAction(checker.getReturnTypeOfSignature(signature))
+ ) {
+ signature.parameters.forEach((parameter, index) => {
+ const argument = node.arguments[index];
+ if (parameter.name === "title" && argument) add(argument);
+ });
+ }
+ }
+ ts.forEachChild(node, visit);
+ };
+ visit(source);
+ }
+ return titles;
+}
+
+const titles = actionTitles();
+/** Product names shown as they are in every language. */
+const PROPER_NAMES = new Set(["Vim"]);
+
+it("has a Chinese entry for every literal action title, including action factory arguments", () => {
+ // Ensure the helper-argument regression cannot disappear through a scanner change.
+ expect(titles.has("Go to Declaration or Usages")).toBe(true);
+ expect(titles.has("Next Result")).toBe(true);
+ const catalog: Record<string, string> = zh;
+ expect(
+ [...titles]
+ .filter(([title]) => !PROPER_NAMES.has(title) && (!catalog[title] || catalog[title] === title))
+ .map(([title, at]) => `${at} ${JSON.stringify(title)}`),
+ ).toEqual([]);
+});
diff --git a/src/platform/locales/text/zh.json b/src/platform/locales/text/zh.json
index 388242113..df54ae260 100644
--- a/src/platform/locales/text/zh.json
+++ b/src/platform/locales/text/zh.json
@@ -1,4 +1,67 @@
{
+ "Semantic Highlighting": "语义高亮",
+ "Rainbow Brackets": "彩虹括号",
+ "Comment with Line Comment": "使用行注释",
+ "Comment with Block Comment": "使用块注释",
+ "Duplicate Line or Selection": "复制行或选区",
+ "Delete Line": "删除行",
+ "Move Line Up": "上移行",
+ "Move Line Down": "下移行",
+ "Move Statement Up": "上移语句",
+ "Move Statement Down": "下移语句",
+ "Extend Selection": "扩展选区",
+ "Shrink Selection": "缩小选区",
+ "Toggle Fold": "切换折叠",
+ "Cyclic Expand Word": "循环补全单词",
+ "Cyclic Expand Word (Backward)": "循环补全单词(反向)",
+ "Paste as Plain Text": "粘贴为纯文本",
+ "Add Selection for Next Occurrence": "添加下一个匹配项到选区",
+ "Select All Occurrences": "选择所有匹配项",
+ "Complete Current Statement": "补全当前语句",
+ "Start New Line": "开始新行",
+ "Start New Line Before Current": "在当前行前开始新行",
+ "Split Line": "拆分行",
+ "Scroll to Center": "滚动到中央",
+ "Move Caret to Code Block Start": "将光标移至代码块开头",
+ "Move Caret to Code Block End": "将光标移至代码块末尾",
+ "Clone Caret Above": "在上方添加光标",
+ "Clone Caret Below": "在下方添加光标",
+ "Join Lines": "合并行",
+ "Toggle Case": "切换大小写",
+ "Add Carets to Ends of Selected Lines": "在所选行末尾添加光标",
+ "Unselect Occurrence": "取消选择匹配项",
+ "Paste from History…": "从历史记录粘贴…",
+ "Sort Lines": "排序行",
+ "Reverse Lines": "反转行序",
+ "Unwrap/Remove…": "解包/移除…",
+ "Basic": "基本补全",
+ "Quick Definition": "快速定义",
+ "Reformat File…": "重新格式化文件…",
+ "Go to Declaration or Usages": "转到声明或用法",
+ "Go to Implementation(s)": "转到实现",
+ "Go to Type Declaration": "转到类型声明",
+ "Next Highlighted Error": "下一个高亮错误",
+ "Previous Highlighted Error": "上一个高亮错误",
+ "Highlight Usages in File": "高亮文件中的用法",
+ "Implement Methods…": "实现方法…",
+ "Override Methods…": "重写方法…",
+ "Expression Type Info": "表达式类型信息",
+ "Next Method": "下一个方法",
+ "Previous Method": "上一个方法",
+ "Copy Absolute Path": "复制绝对路径",
+ "Copy Path From Content Root": "复制相对于内容根目录的路径",
+ "Copy File Name": "复制文件名",
+ "Fixup…": "修补提交…",
+ "Step Over": "单步跳过",
+ "Step Out": "单步跳出",
+ "Zoom In": "放大",
+ "Zoom Out": "缩小",
+ "Actual Size": "实际大小",
+ "Fit Zoom to Window": "缩放以适应窗口",
+ "Show Chessboard": "显示棋盘格",
+ "Editor Only": "仅编辑器",
+ "Editor and Preview": "编辑器和预览",
+ "Preview Only": "仅预览",
"Add Maven Projects": "添加 Maven 项目",
"Unlink Maven Projects": "取消链接 Maven 项目",
"Unlink Maven projects?": "取消链接 Maven 项目?",