Skip to content

Commit b61131e

Browse files
committed
feat: support single-char locale text; update tests
1 parent 28007f0 commit b61131e

4 files changed

Lines changed: 42 additions & 2 deletions

File tree

packages/i18n-migrate-cli/src/__tests__/architecture.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ describe('i18n migrate architecture primitives', () => {
130130

131131
it('applies default filter rules and force-pattern precedence', () => {
132132
expect(shouldTranslate({ text: '请输入用户名', context: 'template' }, DEFAULT_CONFIG.rules)).toBe(true)
133-
expect(shouldTranslate({ text: '中', context: 'template' }, DEFAULT_CONFIG.rules)).toBe(false)
133+
expect(shouldTranslate({ text: '中', context: 'template' }, DEFAULT_CONFIG.rules)).toBe(true)
134134
expect(shouldTranslate({ text: '请求失败', context: 'console' }, DEFAULT_CONFIG.rules)).toBe(false)
135135

136136
const rules = defineConfig({

packages/i18n-migrate-cli/src/__tests__/replacer.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,26 @@ describe('replacer syntax-aware writeback', () => {
155155
])).toContain('{ title: "Scenic Area", dataIndex: "scenicName" }')
156156
})
157157

158+
it('extracts single-character units in data objects without field-name special cases', () => {
159+
const content = [
160+
'export const statList = [',
161+
' { title: "销售额", prefix: "元", subPrefix: "单" },',
162+
' { title: "销售数量", prefix: "张", unit: "人" },',
163+
']',
164+
'export const showTotal = (total) => "共" + total + "条"',
165+
].join('\n')
166+
const extractor = new Extractor(config)
167+
const segments = extractor.extract(content, 'src/data.ts')
168+
169+
expect(segments.map(segment => segment.text)).toEqual(['销售额', '元', '单', '销售数量', '张', '人', '共', '条'])
170+
expect(replace(content, 'src/data.ts', [
171+
['元', 'yuan'],
172+
['单', 'order'],
173+
['张', 'ticket'],
174+
['人', 'people'],
175+
])).toContain('{ title: "销售额", prefix: "yuan", subPrefix: "order" }')
176+
})
177+
158178
it('keeps HTML text ranges after attributes containing greater-than signs', () => {
159179
const content = '<div><template data-test="fileMax > 1">最大上传{{ fileMax }}张图片</template></div>'
160180
const extractor = new Extractor(config)

packages/i18n-migrate-cli/src/utils/filter.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export function shouldTranslate(input: FilterInput, rules: FilterRule[]): boolea
2424
return false
2525
if (rule.type === 'skip-pattern' && shouldApplySkipPattern(rule.value, sourceLocale) && new RegExp(rule.value).test(visibleText))
2626
return false
27-
if (rule.type === 'min-length' && localeTextLength(visibleText, sourceLocale) < rule.value)
27+
if (rule.type === 'min-length' && localeTextLength(visibleText, sourceLocale) < rule.value && !isSingleLocaleText(visibleText, sourceLocale))
2828
return false
2929
if (rule.type === 'max-length' && localeTextLength(visibleText, sourceLocale) > rule.value)
3030
return false
@@ -33,6 +33,13 @@ export function shouldTranslate(input: FilterInput, rules: FilterRule[]): boolea
3333
return true
3434
}
3535

36+
function isSingleLocaleText(text: string, sourceLocale: string): boolean {
37+
const trimmed = text.trim()
38+
return Array.from(trimmed).length === 1
39+
&& localeTextLength(trimmed, sourceLocale) === 1
40+
&& hasLocaleText(trimmed, sourceLocale)
41+
}
42+
3643
function stripInterpolation(text: string): string {
3744
return text
3845
.replace(/\{\{[\s\S]*?\}\}/g, ' ')

playground/src/i18n-migrate-scene/source-code.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,19 @@ export const uploadTips = {
2828
placeholder: '请使用扫码枪扫描客户微信/支付宝付款码',
2929
}
3030

31+
export const statUnits = [
32+
{
33+
title: '销售额',
34+
prefix: '元',
35+
subPrefix: '单',
36+
},
37+
{
38+
title: '销售数量',
39+
prefix: '张',
40+
unit: '人',
41+
},
42+
]
43+
3144
export function getPaymentTitle(record: PaymentRecord): string {
3245
return `订单支付 ${record.amount ? ` - ¥${record.amount}` : ''}`
3346
}

0 commit comments

Comments
 (0)