
伪本地化:无需真实翻译即可测试 i18n 就绪情况
真实翻译需要时间和费用。伪本地化会将源字符串转换为可读但明显虚假的文本,立即暴露 i18n 缺陷:硬编码字符串、文本溢出、RTL 问题和串联问题。
1
什么是伪本地化?
伪本地化会将源字符串中的字符替换为外观相似的重音或扩展字符,添加文本扩展填充,并用方括号包裹字符串。结果对开发者仍然可读,但与真实文本明显不同,因此很容易发现未使用翻译函数包装的字符串。
Pseudo-localization examples
// Pseudo-localization transforms your source strings
// to simulate translation without real translators
// Original:
"Welcome to our application"
// Accented (simulates diacritics):
"[Ẁëľčöṁë ţö öüŕ àṗṗľïčàţïöñ]"
// Expanded (simulates text expansion ~30%):
"[Weeelcooomee tooo ouuur aaappliiicaaatiiioon]"
// Mirrored (simulates RTL layout):
"[noitacilppa ruo ot emocleW]"
// Brackets make untranslated strings immediately visible伪本地化是发现 i18n 缺陷最快的方式。生成只需几秒,无需费用,还能发现在使用真实语言测试前看不到的问题。每个 i18n 就绪项目都应在委托真实翻译前使用它。
2
在项目中使用
安装 i18n-pseudo 并对区域设置文件运行。该工具会自动检测文件格式并生成伪本地化版本,您可将其作为测试区域设置加载到应用中。
Issues caught by pseudo-localization
// 1. Hardcoded strings (not wrapped in t())
// Pseudo strings have brackets/accents, so untranslated
// strings are immediately obvious in the UI
// 2. Text truncation
// Expanded text reveals buttons and labels that break
// when translations are longer than English
// 3. Layout issues
// Accented characters reveal font rendering problems
// RTL mode reveals directional layout bugs
// 4. Concatenation bugs
// "Hello " + name vs t('greeting', { name })
// Pseudo-localization breaks concatenated strings
// 5. Missing i18n wrappers
// Any string not going through the i18n system
// appears as plain English among pseudo text开发期间,在应用的语言切换器中添加 'pseudo' 区域设置。开发者切换到该设置后,未翻译字符串会立即显现。发布到生产环境前请移除伪区域设置。
3
7 种转换策略
i18n-pseudo 提供 7 种转换策略,每种测试 i18n 实现的不同方面。可单独使用以进行针对性测试,也可通过预设组合使用以全面覆盖。
Pseudo-localization strategies
// Strategy 1: Accented characters
// Replace ASCII with similar-looking Unicode
// a -> à, e -> ë, o -> ö, etc.
// Preserves readability while testing rendering
// Strategy 2: Text expansion
// Pad strings to simulate longer translations
// German is ~30% longer, Finnish ~40% longer
// Helps catch UI overflow early
// Strategy 3: Brackets/wrappers
// Wrap strings in [brackets] or «guillemets»
// Makes untranslated strings visually obvious
// Strategy 4: Bidi/RTL
// Mirror text for right-to-left testing
// Catches layout issues before Arabic/Hebrew testing
// Strategy 5: Combined
// Apply all strategies simultaneously
// Maximum coverage in one pass4
5 种内置预设
预设将多种策略组合为常用配置。可使用预设快速测试,也可根据特定需求创建自定义组合。
CLI & Configuration
// Using i18n-pseudo CLI
npx i18n-pseudo generate \
--source locales/en.json \
--output locales/pseudo.json \
--preset accented
# Available presets:
# accented - Ḣëľľö Ẁöŕľð (default)
# expanded - Heeellooo Wooorld
# mirrored - dlroW olleH
# brackets - [Hello World]
# maximum - [Ḣëëëľľľöööö Ẁöööŕŕŕľľľðððð]
// i18n-pseudo.config.json
{
"source": "locales/en.json",
"output": "locales/pseudo.json",
"preset": "maximum",
"expansion": 1.4,
"wrapper": ["[", "]"],
"exclude": [
"*.url",
"*.email",
"branding.*"
]
}5
CI/CD 集成
在 CI 中运行伪本地化,自动检测 i18n 回归。生成伪本地化文件、呈现关键屏幕并与基准屏幕截图比较。屏幕截图中任何未转换文本都表示回归,即出现绕过翻译函数的新硬编码字符串。
.github/workflows/pseudo-check.yml
# .github/workflows/pseudo-check.yml
name: Pseudo-Localization Check
on:
pull_request:
paths:
- 'src/**'
jobs:
pseudo-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 20
- name: Install dependencies
run: npm ci
- name: Generate pseudo locale
run: npx i18n-pseudo generate \
--source locales/en.json \
--output locales/pseudo.json \
--preset maximum
- name: Build with pseudo locale
run: npm run build
env:
NEXT_PUBLIC_LOCALE: pseudo
- name: Run visual regression tests
run: npx playwright test --project=pseudo
env:
LOCALE: pseudo立即试用 i18n Agent
将翻译文件拖放到此处
JSON, YAML, PO, XML, CSV, Markdown, Properties
或点击选择文件
目标语言
无需注册即时估价