面向独立 .NET 开发者的 $0 本地化技术栈
Source: Dev.to
(未提供需要翻译的正文内容。如需翻译,请提供完整的文本。)
问题
我在应用中有 400+ 条字符串。我需要至少希腊语和英语的翻译,理想情况下更多。我的选项是:
| 选项 | 成本 | 速度 |
|---|---|---|
| Lokalise | $120/mo | 快 |
| Phrase | $25/mo | 快 |
| 手动谷歌翻译 | $0 | 数小时的复制粘贴噩梦 |
| 雇佣翻译 | $100+ | 天数 |
这些都不适合单独开发者的副项目。
思路
如果你可以在 自己的机器 上运行翻译模型会怎样?无需 API 费用、没有速率限制,数据也不会离开你的电脑。
这正是我在 LRM(Localization Resource Manager) 中实现的。
1️⃣ 安装 Ollama(本地 LLM 运行时)
# Linux
curl -fsSL https://ollama.ai/install.sh | sh
# Windows / macOS – download from https://ollama.ai
2️⃣ 拉取模型
ollama pull llama3.2 # default
# or for better quality
ollama pull llama3.1:8b
3️⃣ 安装 LRM
# Linux (one‑liner)
curl -sSL https://raw.githubusercontent.com/nickprotop/LocalizationManager/main/install-lrm.sh | bash
# Or via APT (Ubuntu/Debian)
sudo add-apt-repository ppa:nickprotop/lrm-tool
sudo apt install lrm-standalone
# Windows / macOS – download from GitHub releases
# https://github.com/nickprotop/LocalizationManager/releases
就这样。无需 API 密钥、无需账户、无需信用卡。
Source: …
4️⃣ 翻译
# 进入你的项目目录
cd MyApp/Resources
# 将所有缺失的键翻译为西班牙语、法语、德语
lrm translate --only-missing --provider ollama --target-languages es,fr,de
LRM 会查找所有 .resx 文件,识别缺失的翻译,并使用本地大模型自动填充。
- 400+ 条键 × 3 种语言 = 1,200+ 条翻译
- 零费用
- 不到 10 分钟
没有 GPU?使用免费在线提供商(仍然 $0)
lrm translate --only-missing --provider mymemory --target-languages es,fr,de
# 每天免费 5,000 字符 – 对大多数项目已足够
lrm translate --only-missing --provider lingva --target-languages es,fr,de
# 使用开源代理调用 Google 翻译 – 无需 API 密钥
添加意大利语 – 实际操作演练
# 添加新的语言文件
lrm add-language --culture it
# 检查缺少的内容
lrm stats
本地化统计
┌───────────────┬────────────┬───────────┬───────┬──────────┬───────────┐
│ Language │ Total Keys │ Completed │ Empty │ Coverage │ File Size │
├───────────────┼────────────┼───────────┼───────┼──────────┼───────────┤
│ 默认 │ 12 │ 12 │ 0 │ 100.0% │ 1.8 KB │
│ Deutsch (de) │ 12 │ 5 │ 7 │ 41.7% │ 1.6 KB │
│ français (fr)│ 12 │ 7 │ 5 │ 58.3% │ 1.7 KB │
│ italiano (it)│ 12 │ 0 │ 12 │ 0.0% │ 1.6 KB │
└───────────────┴────────────┴───────────┴───────┴──────────┴───────────┘
意大利语的完成度为 0 % – 让我们来修复它:
lrm translate --only-missing --provider ollama --target-languages it
╭─────────────────────┬──────────┬─────────────────────────────────────┬────────╮
│ Key │ Language │ Translation │ Status │
├─────────────────────┼──────────┼─────────────────────────────────────┼────────┤
│ AppTitle │ it │ Responsabile dello studio medico │ ✓ │
│ WelcomeMessage │ it │ Eccoci di nuovo, {0}! │ ✓ │
│ LoginButton │ it │ Accedi │ ✓ │
│ LogoutButton │ it │ Disconnettere │ ✓ │
│ SaveButton │ it │ Salva │ ✓ │
│ CancelButton │ it │ Cancella │ ✓ │
│ DeleteConfirmation │ it │ Sei sicuro di voler eliminare... │ ✓ │
│ ErrorOccurred │ it │ Si è verificato un errore... │ ✓ │
│ PatientName │ it │ Nome del paziente │ ✓ │
│ AppointmentDate │ it │ Data prenotazione │ ✓ │
│ NoResultsFound │ it │ Nessun risultato trovato │ ✓ │
│ SearchPlaceholder │ it │ Cerca pazienti │ ✓ │
╰─────────────────────┴──────────┴─────────────────────────────────────┴────────╯
12 条中的 12 条已在几秒内翻译完成。 完成。
CLI 使用彩色输出 – 缺失的翻译显示为红色,已完成的显示为绿色。
“质量足够好吗?”
| Provider | Quality | Cost | Speed |
|---|---|---|---|
| DeepL | 优秀 | $20/月 | 快速 |
| 非常好 | 按使用付费 | 快速 | |
| Ollama (llama3.2) | 良好 | $0 | 中等 |
| MyMemory | 良好 | $0 | 快速 |
- UI 字符串如 “Save”、 “Cancel”、 “Error occurred”?Ollama 完全足够好。
- 市场文案或法律文本?也许需要付费使用 DeepL。
- 90 % 的应用本地化?免费足够好。
超越翻译 – 验证与清理
一旦完成翻译,LRM 便扩展为包括验证功能:
lrm validate
⚠ Validation found 12 issue(s)
Empty Values
┌──────────┬───────────────────────────────────────────────────────────────────┐
│ Language │ Empty Keys │
├──────────┼───────────────────────────────────────────────────────────────────┤
│ de │ CancelButton, DeleteConfirmation, ErrorOccurred, PatientName… │
│ fr │ ErrorOccurred, PatientName, AppointmentDate, NoResultsFound… │
└──────────┴───────────────────────────────────────────────────────────────────┘
LRM 会扫描你的 C#、Razor 和 XAML 文件,查找:
- 缺失的键 – 在代码中使用但未在
.resx中定义(会导致运行时错误) - 未使用的键 – 已在
.resx中定义但从未被引用(造成冗余)
随后,你可以使用一条命令将它们清理掉:
lrm prune-unused
TL;DR – 整体工作流程
# 1️⃣ Install Ollama
curl -fsSL https://ollama.ai/install.sh | sh
# 2️⃣ Pull a model
ollama pull llama3.2
# 3️⃣ Install LRM
curl -sSL https://raw.githubusercontent.com/nickprotop/LocalizationManager/main/install-lrm.sh | bash
# 4️⃣ Translate missing strings
cd MyApp/Resources
lrm translate --only-missing --provider ollama --target-languages es,fr,de,it
# 5️⃣ Validate & prune
lrm validate
lrm prune-unused
结果: 一个完全本地化的 .NET 应用,支持 5 种语言,费用 $0,工作时间 不到 10 分钟。 🎉
sed in code(死翻译)
lrm scan --source-path ./src
✓ Scanned 3 files
Found 18 key references (16 unique keys)
Missing Keys (in code, not in .resx)
┌─────────────────────┬────────────┬───────────────────────────┐
│ Key │ References │ Locations │
├─────────────────────┼────────────┼───────────────────────────┤
│ AboutUs │ 1 │ HomeController.cs:31 │
│ AppSubtitle │ 1 │ HomeController.cs:22 │
│ CompanyDescription │ 1 │ HomeController.cs:32 │
│ GetStarted │ 1 │ Index.cshtml:9 │
│ OperationFailed │ 1 │ NotificationService.cs:23 │
│ OperationSuccessful │ 1 │ NotificationService.cs:17 │
│ … and 7 more │ │ │
└─────────────────────┴────────────┴───────────────────────────┘
✗ Found 13 missing keys and 2 unused keys
13 个在代码中使用但
.resx文件中不存在的键!
这些会导致运行时错误。现在我可以在用户看到破碎的 UI 之前把它们补上。
在更大的项目(我的实际应用)中,扫描还发现了相反的情况:
lrm scan --source-path ./src
✓ Scanned 497 files
Found 607 key references (394 unique keys)
Unused Keys (in .resx, not in code)
┌────────────────────────────┬───────┐
│ Key │ Count │
├────────────────────────────┼───────┤
│ ActiveAdmissions │ - │
│ AdmissionCount │ - │
│ DailyAdmissions │ - │
│ LanguageAutoDetect │ - │
│ MarkAsComplete │ - │
│ … and 51 more │ │
└────────────────────────────┴───────┘
✗ Found 0 missing keys and 56 unused keys
56 条死键可以清理。
减少杂乱,资源文件更小。
交互式编辑器(TUI)
lrm edit
在格式之间转换
lrm convert --from resx --to json
为译者导出
lrm export --format csv
支持的格式(单一工具)
.resx– WPF、WinForms、ASP.NET- JSON – ASP.NET Core、Blazor
- i18next – React、Vue、Angular
- Android
strings.xml - iOS
.strings
所有格式,单一工具。
我的完整本地化工作流
-
添加新的英文字符串,通常在你的应用中。
-
验证(及早捕获问题)
lrm validate -
翻译缺失的键
lrm translate --only-missing --provider ollama -
在 TUI 中快速审查
lrm edit -
提交
git add Resources/ git commit -m "Add translations"
总时间: 5 分钟 总成本: $0.
开源项目
- 仓库:
- 许可证: MIT(随意使用)
- 特性: 100% 离线,无账号,无追踪,可自行托管。
最初只是“我需要翻译我的应用”,结果发展得更大。CLI 表现如此出色,我不断添加功能,随后构建了网页 UI、云同步和 OTA 更新。现在它已经成为一个完整平台。
云服务为独立开发者的成长提供所需
| 功能 | 描述 |
|---|---|
| 格式无关 | 一个项目可同步到 RESX、JSON 或 i18next |
| 网页编辑器 | 随时随地编辑翻译,无需 CLI |
| OTA 更新 | 在不重新部署的情况下更新翻译(首个 .NET OTA!) |
| GitHub 同步 | 与你的仓库双向同步 |
| 翻译记忆库 | 重用已有翻译,节省 API 成本 |
| 术语表 | 强制使用一致的术语 |
| 邀请译者 | 他们在浏览器中编辑,你进行审阅和批准 |
| 免费层 | 3 个项目,5 K 字符/每月——足以支持副项目 |
CLI 仍然是 100% 免费且开源的。云服务是可选的——如果需要,也可以自行托管!
安装(Linux)
curl -sSL https://raw.githubusercontent.com/nickprotop/LocalizationManager/main/install-lrm.sh | bash
ollama pull llama3.2
免费翻译你的应用
cd YourProject/Resources
lrm translate --only-missing --provider ollama --target-languages es,fr,de,it,pt
完成。 $0, 5 minutes.
需要帮助?
- 在 GitHub 上打开一个 issue。
- 在 Reddit 上找到我。
如果你觉得这有用,给 GitHub 项目加星可以帮助其他人发现它。
Tags: dotnet, localization, i18n, translation, ollama, llm, open-source, csharp, blazor, aspnetcore