diff --git "a/docs/development/i18n/\345\233\275\351\231\205\345\214\226\345\274\200\345\217\221\346\214\207\345\215\227.md" "b/docs/development/i18n/\345\233\275\351\231\205\345\214\226\345\274\200\345\217\221\346\214\207\345\215\227.md" index 6c545f27fc918ef1b8975875f9e842a63609ac0a..d4c865fa1f54d78114ce04cfcce64c2e6c1c17d4 100644 --- "a/docs/development/i18n/\345\233\275\351\231\205\345\214\226\345\274\200\345\217\221\346\214\207\345\215\227.md" +++ "b/docs/development/i18n/\345\233\275\351\231\205\345\214\226\345\274\200\345\217\221\346\214\207\345\215\227.md" @@ -133,6 +133,26 @@ msgstr[0] "找到 {n} 个文件" 编译后的 `.mo` 文件会被应用程序加载和使用。 +#### 5. 去除重复条目(可选) + +如果翻译文件中存在重复的条目,可以使用 `uniq` 命令清理: + +```bash +./scripts/tools/i18n-manager.sh uniq +``` + +这会使用 `msguniq` 工具去除重复的翻译条目,保留第一次出现的条目。 + +#### 6. 查看翻译统计 + +查看翻译文件的完成情况和统计信息: + +```bash +./scripts/tools/i18n-manager.sh stats +``` + +这会显示每种语言的翻译进度,包括已翻译、未翻译和模糊翻译的数量。 + #### 完整流程(推荐) 你也可以一次执行所有步骤: @@ -363,7 +383,10 @@ oi --help # 应显示英文 ### 验证翻译完整性 ```bash -# 检查未翻译的字符串 +# 使用 i18n 管理工具查看所有语言的翻译统计 +./scripts/tools/i18n-manager.sh stats + +# 或直接使用 msgfmt 检查特定语言 msgfmt --check --statistics src/i18n/locales/zh_CN/LC_MESSAGES/messages.po ``` diff --git "a/docs/development/i18n/\345\233\275\351\231\205\345\214\226\345\277\253\351\200\237\345\205\245\351\227\250.md" "b/docs/development/i18n/\345\233\275\351\231\205\345\214\226\345\277\253\351\200\237\345\205\245\351\227\250.md" index 3d8459d650a270c7bfd769e6d78dcd9c966dcccd..01fe7a61ac12911653f1ba0a4a9993c0ff9d568a 100644 --- "a/docs/development/i18n/\345\233\275\351\231\205\345\214\226\345\277\253\351\200\237\345\205\245\351\227\250.md" +++ "b/docs/development/i18n/\345\233\275\351\231\205\345\214\226\345\277\253\351\200\237\345\205\245\351\227\250.md" @@ -52,9 +52,36 @@ greeting = _("Hello, {name}!").format(name=username) ./scripts/tools/i18n-manager.sh extract # 提取字符串 ./scripts/tools/i18n-manager.sh update # 更新翻译文件 # 编辑 .po 文件... +./scripts/tools/i18n-manager.sh uniq # 去除重复条目(可选) +./scripts/tools/i18n-manager.sh stats # 查看翻译统计(可选) ./scripts/tools/i18n-manager.sh compile # 编译翻译 ``` +### 管理工具命令 + +```bash +# 查看帮助 +./scripts/tools/i18n-manager.sh help + +# 提取可翻译字符串 +./scripts/tools/i18n-manager.sh extract + +# 更新翻译文件 +./scripts/tools/i18n-manager.sh update + +# 去除重复条目 +./scripts/tools/i18n-manager.sh uniq + +# 查看翻译统计 +./scripts/tools/i18n-manager.sh stats + +# 编译翻译文件 +./scripts/tools/i18n-manager.sh compile + +# 完整流程(提取→更新→编译) +./scripts/tools/i18n-manager.sh all +``` + ### 测试翻译 ```bash diff --git a/scripts/tools/i18n-manager.sh b/scripts/tools/i18n-manager.sh index 7d3ebc61ad048e2fffe52285555a94e72756f07f..012767f35ec0c07ac8c204690f6d84ee93898a78 100755 --- a/scripts/tools/i18n-manager.sh +++ b/scripts/tools/i18n-manager.sh @@ -52,6 +52,10 @@ show_help() { echo " 更新所有语言的翻译文件" print_green " compile" echo " 编译翻译文件为二进制格式" + print_green " uniq" + echo " 去除翻译文件中的重复条目" + print_green " stats" + echo " 显示翻译文件的统计信息" print_green " all" echo " 执行完整流程 (extract -> update -> compile)" print_green " help" @@ -60,6 +64,8 @@ show_help() { echo "示例:" echo " $0 extract # 提取可翻译字符串" echo " $0 compile # 编译翻译文件" + echo " $0 uniq # 去除重复条目" + echo " $0 stats # 查看翻译统计" echo " $0 all # 完整翻译工作流" echo "" echo "更多信息请参考: docs/development/国际化开发指南.md" @@ -212,6 +218,102 @@ compile() { fi } +# 去除重复的翻译条目 +uniq() { + print_blue "🔧 去除重复的翻译条目..." + + check_gettext + + if ! command -v msguniq &>/dev/null; then + print_red "❌ msguniq command not found. Please install gettext tools." + exit 1 + fi + + processed=0 + failed=0 + + # 遍历所有语言目录 + for locale_path in "$LOCALE_DIR"/*; do + if [ ! -d "$locale_path" ]; then + continue + fi + + locale_name=$(basename "$locale_path") + po_file="$locale_path/LC_MESSAGES/messages.po" + + if [ ! -f "$po_file" ]; then + print_yellow "⚠️ Skipping $locale_name: PO file not found" + continue + fi + + echo " Processing $locale_name..." + # 创建临时文件 + temp_file="${po_file}.tmp" + + set +e + set +o pipefail + if msguniq --use-first "$po_file" -o "$temp_file" 2>/dev/null; then + mv "$temp_file" "$po_file" + echo " ✅ Processed $locale_name" + processed=$((processed + 1)) + else + print_yellow " ⚠️ Failed to process $locale_name" + rm -f "$temp_file" + failed=$((failed + 1)) + fi + set -e + set -o pipefail + done + + echo "" + if [ "$processed" -gt 0 ]; then + print_green "✅ Successfully processed $processed translation file(s)" + fi + + if [ "$failed" -gt 0 ]; then + print_yellow "⚠️ Failed to process $failed translation file(s)" + fi + + if [ "$processed" -eq 0 ] && [ "$failed" -eq 0 ]; then + print_yellow "⚠️ No translation files found to process" + fi +} + +# 显示翻译统计信息 +stats() { + print_blue "📊 翻译统计信息..." + + check_gettext + + found=0 + + # 遍历所有语言目录 + for locale_path in "$LOCALE_DIR"/*; do + if [ ! -d "$locale_path" ]; then + continue + fi + + locale_name=$(basename "$locale_path") + po_file="$locale_path/LC_MESSAGES/messages.po" + + if [ ! -f "$po_file" ]; then + print_yellow "⚠️ Skipping $locale_name: PO file not found" + continue + fi + + echo "" + print_green "=== $locale_name ===" + msgfmt --statistics "$po_file" 2>&1 || true + found=$((found + 1)) + done + + if [ "$found" -eq 0 ]; then + echo "" + print_yellow "⚠️ No translation files found" + fi + echo "" +} + # 执行完整流程 all() { extract @@ -241,6 +343,12 @@ main() { compile) compile ;; + uniq) + uniq + ;; + stats) + stats + ;; all) all ;;