diff --git a/src/bin/extra-text.ts b/src/bin/extra-text.ts index a861c32..2dc3bf0 100644 --- a/src/bin/extra-text.ts +++ b/src/bin/extra-text.ts @@ -105,16 +105,16 @@ export function extraTextFromTDotT( if (textValidate) { textSuccess.push(textContent) } else { - textError.push(textLabel) + textError.push(textContent) } // 自定义key和文案都有效 if (keyValidate && textValidate) { const text = keyTextMap[keyContent] - if (text && text != text) { + if (text && text != textContent) { textError.push( t( - '当前自定义key={0},配置了不一样的文案({1}和{2}),应该满足key与文案一对一的关系', + '当前自定义 key=`{0}`,配置了不一样的文案(`{1}` 和 `{2}`),应该满足 key 与文案一对一的关系', keyContent, text, textContent, @@ -122,12 +122,11 @@ export function extraTextFromTDotT( ) } else if (!text) { keyTextMap[keyContent] = textContent - } - - const keys = textKeyMap[textContent] || [] - if (!keys.includes(keyContent)) { - keys.push(keyContent) - textKeyMap[textContent] = keys + const keys = textKeyMap[textContent] || [] + if (!keys.includes(keyContent)) { + keys.push(keyContent) + textKeyMap[textContent] = keys + } } } } diff --git a/test/bin/extra-text.test.ts b/test/bin/extra-text.test.ts index e8840b4..ab017d6 100644 --- a/test/bin/extra-text.test.ts +++ b/test/bin/extra-text.test.ts @@ -2,9 +2,9 @@ import { readFileSync } from 'fs' import { join } from 'path' import { binExtraText } from '../utils' -const { extraTextFromT } = binExtraText +const { extraTextFromT, extraTextFromTDotT } = binExtraText -describe('验证翻译文本提取功能', () => { +describe('验证 t 翻译文本提取功能', () => { const content = readFileSync(join(__dirname, '../i18n/text.ts'), { encoding: 'utf-8', }) @@ -58,3 +58,160 @@ describe('验证翻译文本提取功能', () => { }) }) }) + +describe('验证 t.t 翻译文本提取功能', () => { + const content = readFileSync(join(__dirname, '../i18n/text2.ts'), { + encoding: 'utf-8', + }) + + const successTexts = [ + '中间 有空格', + '普通文本', + '普通文本{0}', + '普通文案', + 'a', + '自定义key', + '自定义key', + ] + + const errorTexts = [ + '${foo}', + '你好\\n啊', // NOTE 这里需要是\\n,不然匹配不正确 + '你好\\t啊', + ' 前面有空格', + '后面空格 ', + '当前自定义 key=`test`,配置了不一样的文案(`普通文案` 和 `a`),应该满足 key 与文案一对一的关系', + ] + + const successKeys = [ + '中间 有空格的key', + '普通文本key', + '普通文本', + 'test', + 'test', + 'key2', + 'key3', + ] + + const errorKeys = [ + '${key}', + 'key\\n中间', // NOTE 这里需要是\\n,不然匹配不正确 + 'key\\t中间', + ' key前面有空格', + 'key后面空格 ', + ] + + const expectKeyTextMap = { + '中间 有空格的key': '中间 有空格', + 普通文本key: '普通文本', + 普通文本: '普通文本{0}', + test: '普通文案', + key2: '自定义key', + key3: '自定义key', + } + + const expectTextKeyMap = { + '中间 有空格': ['中间 有空格的key'], + 普通文本: ['普通文本key'], + '普通文本{0}': ['普通文本'], + 普通文案: ['test'], + 自定义key: ['key2', 'key3'], + } + + function getParams() { + return { + keySuccess: [], + keyError: [], + keyTextMap: {}, + textKeyMap: {}, + textSuccess: [], + textError: [], + } + } + + describe('验证规范文本提取', () => { + it('默认提取', () => { + const { + keySuccess, + keyError, + keyTextMap, + textKeyMap, + textSuccess, + textError, + } = getParams() + extraTextFromTDotT( + content, + 't', + keyTextMap, + textKeyMap, + textSuccess, + textError, + keySuccess, + keyError, + ) + expect(textSuccess).toEqual(successTexts) + expect(textError).toEqual(errorTexts) + expect(keySuccess).toEqual(successKeys) + expect(keyError).toEqual(errorKeys) + expect(keyTextMap).toEqual(expectKeyTextMap) + expect(textKeyMap).toEqual(expectTextKeyMap) + }) + + it('自定义函数名(t -> i18n)', () => { + const { + keySuccess, + keyError, + keyTextMap, + textKeyMap, + textSuccess, + textError, + } = getParams() + extraTextFromTDotT( + content.replaceAll(/t\.t\(/g, 'i18n.t('), + 'i18n', + keyTextMap, + textKeyMap, + textSuccess, + textError, + keySuccess, + keyError, + ) + expect(textSuccess).toEqual(successTexts) + expect(textError).toEqual(errorTexts) + expect(keySuccess).toEqual(successKeys) + expect(keyError).toEqual(errorKeys) + expect(keyError).toEqual(errorKeys) + expect(keyTextMap).toEqual(expectKeyTextMap) + expect(textKeyMap).toEqual(expectTextKeyMap) + }) + }) + + describe('验证不规范文本提取', () => { + it('未知匹配函数名i18n', () => { + const { + keySuccess, + keyError, + keyTextMap, + textKeyMap, + textSuccess, + textError, + } = getParams() + extraTextFromTDotT( + content, + 'i18n', + keyTextMap, + textKeyMap, + textSuccess, + textError, + keySuccess, + keyError, + ) + expect(keySuccess).toEqual([]) + expect(keyError).toEqual([]) + expect(textSuccess).toEqual([]) + expect(textSuccess).toEqual([]) + expect(keyTextMap).toEqual({}) + expect(textKeyMap).toEqual({}) + }) + }) +}) diff --git a/test/i18n/text2.ts b/test/i18n/text2.ts new file mode 100644 index 0000000..c9446b5 --- /dev/null +++ b/test/i18n/text2.ts @@ -0,0 +1,33 @@ +import { initI18n } from '../../src/lib' +const { t } = initI18n({ namespace: 'text' }) +const foo = 'foo' +const key = 'key' +const fooFunc = (x: string) => x +const keyFunc = (x: string) => x + +// 不能记录的错误信息的错误 +t.t(key, foo) +t.t(keyFunc(key), fooFunc(foo)) +t.t('xxx' + key, 'xxx' + foo) +t.t( + `中间有 +换行`, + `中间有 +换行`, +) + +// 能记录到错误信息的错误 +t.t(`${key}`, `${foo}`) +t.t('key\n中间', '你好\n啊') +t.t('key\t中间', '你好\t啊') +t.t(' key前面有空格', ' 前面有空格') +t.t('key后面空格 ', '后面空格 ') + +// 正常解析的示例 +t.t('中间 有空格的key', '中间 有空格') +t.t('普通文本key', '普通文本') +t.t('普通文本', '普通文本{0}', foo) +t.t('test', '普通文案') +t.t('test', 'a', t('b')) +t.t('key2', '自定义key') +t.t('key3', '自定义key')