🐛 fix(vscode): 逻辑与、或译成「并且」「或者」

This commit is contained in:
cruldra committed 2026-09-25 03:27:49 +08:00
1 parent da632a1540
commit 5d2355748b
5 files changed
+20 -12

No files matched your search

+1 -1
View File
@@ -23,7 +23,7 @@ export interface CodeBlock {
export type TokenRole
= | 'keyword' // 流程关键字:import from export if else for return try catch await …
| 'storage' // 声明关键字与字面量关键字:const function async class interface new true false null …
| 'operator' // 被翻成文字的运算符:全等 与 或 非
| 'operator' // 被翻成文字的运算符:全等 并且 或者 非
| 'variable' | 'function' | 'type' | 'string' | 'number' | 'literal' | 'comment' | 'plain'
export interface Segment { text: string, role: TokenRole }
@@ -56,8 +56,8 @@ export const pythonZhTranslator: Translator = createZhTemplateTranslator({
'del': '删除',
'assert': '断言',
'not': '非',
'and': '与',
'or': '或',
'and': '并且',
'or': '或者',
'is': '是',
'not in': '不在',
'is not': '不是',
@@ -83,7 +83,7 @@ export const typescriptZhTranslator: Translator = createZhTemplateTranslator({
symbol: '符号',
},
isTypePosition: leaf => leaf.parent?.type === 'predefined_type',
operators: { '===': '全等', '!==': '不全等', '==': '等于', '!=': '不等于', '&&': '与', '||': '或', '!': '非' },
operators: { '===': '全等', '!==': '不全等', '==': '等于', '!=': '不等于', '&&': '并且', '||': '或者', '!': '非' },
operatorParents: ['binary_expression', 'unary_expression'],
emptyLiterals: { array: '空数组' },
stringNodes: ['string', 'template_string', 'regex'],
@@ -85,7 +85,7 @@ describe('typeScript 中文直译', () => {
expect(issue[1]).toBe('导入 类型 { Issue } 来自 \'../gitea/types\'')
expect(issue[5]).toBe('prerequisiteNumber?: 数值')
expect(issue[9]).toBe('纯判定:工单被未完成的前置挡住才算锁定。快照里找不到工单或前置一律放行, 与 webview 的 `isIssueLocked` 保持同一语义。')
expect(issue[15]).toBe('如果 (非 issue 或 issue.prerequisite 全等 未定义)')
expect(issue[15]).toBe('如果 (非 issue 或者 issue.prerequisite 全等 未定义)')
})
it('每个被翻的词单独成段,着色取语法树词性', async () => {
@@ -112,11 +112,15 @@ describe('typeScript 中文直译', () => {
].join('\n')
const map = texts(await translateAll(ts, 'typescript', typescriptZhTranslator, source))
expect(map[1]).toBe('常量 o = { if: 1, return: i.number, string: x!.type, new: a.default }')
expect(map[2]).toBe('变量 n = a 不全等 b 与 非 c 或 d 等于 e 不等于 f')
expect(map[2]).toBe('变量 n = a 不全等 b 并且 非 c 或者 d 等于 e 不等于 f')
// eslint-disable-next-line no-template-curly-in-string -- 模板字符串原样照抄进译文
expect(map[3]).toBe('常量 s: 字符串 | 无 = 如果 类型为 v 全等 \'string\' 则 `${v}` 否则 \'number\'')
})
it('逻辑与、或译成「并且」「或者」', async () => {
expect(texts(await translateAll(ts, 'typescript', typescriptZhTranslator, 'a && b || c'))).toEqual({ 1: 'a 并且 b 或者 c' })
})
it('多行合成一行、去掉右括号前的尾逗号、行中注释不进译文', async () => {
const source = [
'call(',
@@ -172,7 +176,7 @@ describe('typeScript 中文直译', () => {
1: '如果 err 属于 Error 则 err.message 否则 String(err)',
2: 'z = 如果 x 则 (如果 y 则 1 否则 2) 否则 3',
3: '常量 w = 如果 a 则 b 否则 如果 c 则 d 否则 e',
4: '常量 v = 如果 a 不全等 b 与 非 c 则 类型为 d 否则 等待 e',
4: '常量 v = 如果 a 不全等 b 并且 非 c 则 类型为 d 否则 等待 e',
})
expect(map.get(1)?.segments.filter(segment => segment.role === 'keyword')).toEqual([
{ text: '如果', role: 'keyword' },
@@ -195,7 +199,7 @@ describe('python 中文直译', () => {
10: '函数 find_env_files(root: 字符串) -> 列表[字符串]',
11: 'Return every .env file under root.',
12: '跳过备份文件',
13: 'found = [p 遍历 p 取自 os.listdir(root) 如果 p.startswith(\'.env\') 与 非 p.endswith(\'.bak\')]',
13: 'found = [p 遍历 p 取自 os.listdir(root) 如果 p.startswith(\'.env\') 并且 非 p.endswith(\'.bak\')]',
14: '如果 非 found',
15: '返回 空列表',
16: '否则如果 len(found) > MAX_DEPTH',
@@ -234,6 +238,10 @@ describe('python 中文直译', () => {
expect(map[2]).toBe('返回 [str(k) 遍历 k 取自 items 如果 k 不在 list]')
})
it('逻辑与、或译成「并且」「或者」', async () => {
expect(texts(await translateAll(py, 'python', pythonZhTranslator, 'a and b or c'))).toEqual({ 1: 'a 并且 b 或者 c' })
})
it('from:导入里「从」,raise … from 里「源自」,yield from 里「自」', async () => {
const source = [
'from typing import Annotated',
@@ -286,7 +294,7 @@ describe('python 中文直译', () => {
1: '_default_port = 如果 _role 等于 "beat" 则 5680 否则 5679',
2: 'y = 如果 x 则 a 否则 如果 y 则 b 否则 c',
3: 'z = 如果 y 则 (如果 x 则 a 否则 b) 否则 c',
4: 'v = 如果 非 done 与 ok 则 [k 遍历 k 取自 items 如果 k 不在 seen] 否则 空',
4: 'v = 如果 非 done 并且 ok 则 [k 遍历 k 取自 items 如果 k 不在 seen] 否则 空',
})
expect(map.get(1)?.segments.filter(segment => segment.role === 'keyword' || segment.role === 'operator')).toEqual([
{ text: '如果', role: 'keyword' },