|
|
#!/usr/bin/env python3
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
import os
|
|
|
import re
|
|
|
import glob
|
|
|
|
|
|
def extract_keys_from_file(file_path):
|
|
|
"""从本地化文件中提取所有key"""
|
|
|
keys = set()
|
|
|
with open(file_path, 'r', encoding='utf-8') as f:
|
|
|
for line in f:
|
|
|
match = re.match(r'^"([^"]+)"\s*=', line.strip())
|
|
|
if match:
|
|
|
keys.add(match.group(1))
|
|
|
return keys
|
|
|
|
|
|
def get_english_translations():
|
|
|
"""获取英语翻译作为模板"""
|
|
|
translations = {}
|
|
|
en_file = 'MyQrCode/en.lproj/Localizable.strings'
|
|
|
with open(en_file, 'r', encoding='utf-8') as f:
|
|
|
for line in f:
|
|
|
match = re.match(r'^"([^"]+)"\s*=\s*"([^"]*)"\s*;', line.strip())
|
|
|
if match:
|
|
|
key, value = match.group(1), match.group(2)
|
|
|
translations[key] = value
|
|
|
return translations
|
|
|
|
|
|
def add_missing_keys_to_file(file_path, missing_keys, english_translations):
|
|
|
"""为文件添加缺失的key"""
|
|
|
print(f"Processing {file_path}...")
|
|
|
|
|
|
# 读取现有内容
|
|
|
with open(file_path, 'r', encoding='utf-8') as f:
|
|
|
content = f.read()
|
|
|
|
|
|
# 提取现有的key
|
|
|
existing_keys = extract_keys_from_file(file_path)
|
|
|
|
|
|
# 找出真正缺失的key
|
|
|
actually_missing = missing_keys - existing_keys
|
|
|
|
|
|
if not actually_missing:
|
|
|
print(f" No missing keys for {file_path}")
|
|
|
return
|
|
|
|
|
|
print(f" Adding {len(actually_missing)} missing keys...")
|
|
|
|
|
|
# 在文件末尾添加缺失的key
|
|
|
additions = []
|
|
|
for key in sorted(actually_missing):
|
|
|
if key in english_translations:
|
|
|
# 使用英语作为默认值
|
|
|
additions.append(f'"{key}" = "{english_translations[key]}";')
|
|
|
else:
|
|
|
# 如果没有英语翻译,使用key本身
|
|
|
additions.append(f'"{key}" = "{key}";')
|
|
|
|
|
|
if additions:
|
|
|
# 确保文件以换行符结尾
|
|
|
if not content.endswith('\n'):
|
|
|
content += '\n'
|
|
|
|
|
|
# 添加缺失的条目
|
|
|
content += '\n// Missing keys (using English as fallback)\n'
|
|
|
content += '\n'.join(additions) + '\n'
|
|
|
|
|
|
# 写回文件
|
|
|
with open(file_path, 'w', encoding='utf-8') as f:
|
|
|
f.write(content)
|
|
|
|
|
|
print(f" Added {len(additions)} keys to {file_path}")
|
|
|
|
|
|
def main():
|
|
|
"""主函数"""
|
|
|
print("Adding missing localization keys...")
|
|
|
|
|
|
# 获取英语翻译作为模板
|
|
|
english_translations = get_english_translations()
|
|
|
print(f"Found {len(english_translations)} English translations")
|
|
|
|
|
|
# 获取英语文件中的所有key
|
|
|
en_keys = extract_keys_from_file('MyQrCode/en.lproj/Localizable.strings')
|
|
|
print(f"Found {len(en_keys)} keys in English file")
|
|
|
|
|
|
# 处理所有语言文件(除了英语)
|
|
|
lproj_files = glob.glob('MyQrCode/*.lproj/Localizable.strings')
|
|
|
lproj_files = [f for f in lproj_files if 'en.lproj' not in f]
|
|
|
|
|
|
for file_path in lproj_files:
|
|
|
add_missing_keys_to_file(file_path, en_keys, english_translations)
|
|
|
|
|
|
print("Done!")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
main()
|