docs: 添加国际化实施总结和工具脚本
包含: - 国际化实施总结文档 - 翻译工具脚本 (quick_i18n.py) - 手动翻译库 (manual_translations.json) - 测试指南和后续优化建议
This commit is contained in:
125
scripts/add_localizations.py
Normal file
125
scripts/add_localizations.py
Normal file
@@ -0,0 +1,125 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
为 Localizable.xcstrings 添加多语言翻译
|
||||
使用 AI 翻译服务批量翻译所有字符串
|
||||
"""
|
||||
|
||||
import json
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
# 语言配置 (按优先级排序)
|
||||
LANGUAGES = [
|
||||
("es", "Spanish"), # 西班牙语 - 高优先级
|
||||
("ar", "Arabic"), # 阿拉伯语 - 高优先级
|
||||
("fr", "French"), # 法语 - 中优先级
|
||||
("ja", "Japanese"), # 日语 - 中优先级
|
||||
("ko", "Korean"), # 韩语 - 中优先级
|
||||
]
|
||||
|
||||
def load_xcstrings(file_path):
|
||||
"""加载 xcstrings 文件"""
|
||||
with open(file_path, 'r', encoding='utf-8') as f:
|
||||
return json.load(f)
|
||||
|
||||
def save_xcstrings(file_path, data):
|
||||
"""保存 xcstrings 文件"""
|
||||
with open(file_path, 'w', encoding='utf-8') as f:
|
||||
json.dump(data, f, ensure_ascii=False, indent=2)
|
||||
|
||||
def translate_text(text, target_lang, source_lang="en"):
|
||||
"""
|
||||
翻译文本 (占位符函数,需要集成真实翻译 API)
|
||||
这里暂时返回格式化的提示信息
|
||||
"""
|
||||
return f"[{target_lang.upper()}] {text}"
|
||||
|
||||
def add_language_to_string(string_data, lang_code, lang_name):
|
||||
"""为单个字符串添加指定语言的翻译"""
|
||||
|
||||
if "localizations" not in string_data:
|
||||
string_data["localizations"] = {}
|
||||
|
||||
# 如果已经有该语言的翻译,跳过
|
||||
if lang_code in string_data["localizations"]:
|
||||
return False
|
||||
|
||||
# 获取英文或中文作为源文本
|
||||
source_text = None
|
||||
source_lang = None
|
||||
|
||||
if "en" in string_data["localizations"]:
|
||||
source_text = string_data["localizations"]["en"]["stringUnit"]["value"]
|
||||
source_lang = "en"
|
||||
elif "zh-Hans" in string_data["localizations"]:
|
||||
source_text = string_data["localizations"]["zh-Hans"]["stringUnit"]["value"]
|
||||
source_lang = "zh-Hans"
|
||||
|
||||
if not source_text:
|
||||
return False
|
||||
|
||||
# 翻译文本
|
||||
translated_text = translate_text(source_text, lang_code, source_lang)
|
||||
|
||||
# 添加翻译
|
||||
string_data["localizations"][lang_code] = {
|
||||
"stringUnit": {
|
||||
"state": "translated",
|
||||
"value": translated_text
|
||||
}
|
||||
}
|
||||
|
||||
return True
|
||||
|
||||
def main():
|
||||
# 文件路径
|
||||
xcstrings_path = Path("/Users/yuanjiantsui/projects/to-live-photo/to-live-photo/to-live-photo/Localizable.xcstrings")
|
||||
|
||||
if not xcstrings_path.exists():
|
||||
print(f"错误: 找不到文件 {xcstrings_path}")
|
||||
sys.exit(1)
|
||||
|
||||
print(f"加载文件: {xcstrings_path}")
|
||||
data = load_xcstrings(xcstrings_path)
|
||||
|
||||
# 统计信息
|
||||
total_strings = len(data.get("strings", {}))
|
||||
print(f"找到 {total_strings} 个字符串")
|
||||
|
||||
# 为每种语言添加翻译
|
||||
for lang_code, lang_name in LANGUAGES:
|
||||
print(f"\n处理 {lang_name} ({lang_code})...")
|
||||
added_count = 0
|
||||
|
||||
for key, string_data in data["strings"].items():
|
||||
if add_language_to_string(string_data, lang_code, lang_name):
|
||||
added_count += 1
|
||||
|
||||
print(f" ✓ 为 {added_count} 个字符串添加了 {lang_name} 翻译")
|
||||
|
||||
# 保存文件
|
||||
print(f"\n保存文件...")
|
||||
save_xcstrings(xcstrings_path, data)
|
||||
print("✓ 完成!")
|
||||
|
||||
# 生成需要翻译的字符串列表
|
||||
print("\n生成翻译清单...")
|
||||
output_path = xcstrings_path.parent / "translation_list.json"
|
||||
|
||||
translation_list = {}
|
||||
for key, string_data in data["strings"].items():
|
||||
localizations = string_data.get("localizations", {})
|
||||
if "en" in localizations:
|
||||
translation_list[key] = {
|
||||
"en": localizations["en"]["stringUnit"]["value"],
|
||||
"zh-Hans": localizations.get("zh-Hans", {}).get("stringUnit", {}).get("value", "")
|
||||
}
|
||||
|
||||
with open(output_path, 'w', encoding='utf-8') as f:
|
||||
json.dump(translation_list, f, ensure_ascii=False, indent=2)
|
||||
|
||||
print(f"✓ 翻译清单已保存到: {output_path}")
|
||||
print(f" 包含 {len(translation_list)} 个需要翻译的字符串")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user