119 lines
3.6 KiB
Python
119 lines
3.6 KiB
Python
#!/usr/bin/env python3
|
||
"""
|
||
合并所有模块的i18n到wwwroot/i18n目录
|
||
|
||
用法: python3 script/merge_i18n.py
|
||
"""
|
||
|
||
import os
|
||
import json
|
||
from pathlib import Path
|
||
from collections import OrderedDict
|
||
|
||
|
||
def parse_msg_txt(filepath):
|
||
"""解析msg.txt文件,返回字典"""
|
||
result = OrderedDict()
|
||
if not os.path.exists(filepath):
|
||
return result
|
||
|
||
with open(filepath, 'r', encoding='utf-8') as f:
|
||
for line in f:
|
||
line = line.strip()
|
||
if not line or line.startswith('#'):
|
||
continue
|
||
if ':' in line:
|
||
key, value = line.split(':', 1)
|
||
result[key.strip()] = value.strip()
|
||
return result
|
||
|
||
|
||
def write_msg_txt(data, filepath):
|
||
"""写入msg.txt文件"""
|
||
os.makedirs(os.path.dirname(filepath), exist_ok=True)
|
||
with open(filepath, 'w', encoding='utf-8') as f:
|
||
for key, value in data.items():
|
||
f.write(f"{key}: {value}\n")
|
||
|
||
|
||
def write_i18n_json(data, filepath):
|
||
"""写入i18n.json文件"""
|
||
os.makedirs(os.path.dirname(filepath), exist_ok=True)
|
||
with open(filepath, 'w', encoding='utf-8') as f:
|
||
json.dump(data, f, ensure_ascii=False, indent=2)
|
||
|
||
|
||
def get_modules_from_lnwww():
|
||
"""从lnwww.sh读取模块列表"""
|
||
lnwww_path = Path(__file__).parent.parent / 'wwwroot' / 'lnwww.sh'
|
||
if not lnwww_path.exists():
|
||
print(f"警告: {lnwww_path} 不存在")
|
||
return []
|
||
|
||
modules = []
|
||
with open(lnwww_path, 'r', encoding='utf-8') as f:
|
||
for line in f:
|
||
line = line.strip()
|
||
if line.startswith('for m in'):
|
||
# "for m in xxx yyy zzz" -> "xxx yyy zzz"
|
||
parts = line.split('for m in ', 1)[1].strip().split()
|
||
modules = [p for p in parts if p != 'do']
|
||
break
|
||
return modules
|
||
|
||
|
||
def merge_i18n():
|
||
"""主函数:合并i18n"""
|
||
sage_root = Path(__file__).parent.parent
|
||
wwwroot_i18n = sage_root / 'wwwroot' / 'i18n'
|
||
base_i18n = sage_root / 'i18n'
|
||
|
||
# 获取模块列表
|
||
modules = get_modules_from_lnwww()
|
||
print(f"发现模块: {', '.join(modules)}")
|
||
|
||
# 语言列表
|
||
languages = ['zh', 'en', 'jp', 'ko']
|
||
|
||
for lang in languages:
|
||
print(f"\n处理语言: {lang}")
|
||
|
||
# 从基础i18n开始
|
||
merged = OrderedDict()
|
||
base_msg = base_i18n / lang / 'msg.txt'
|
||
if base_msg.exists():
|
||
merged.update(parse_msg_txt(base_msg))
|
||
print(f" ✓ 加载基础i18n: {len(merged)} 条")
|
||
|
||
# 合并每个模块的i18n
|
||
for module in modules:
|
||
# 模块可能在 ~/py/<module>/i18n 或 ~/repos/<module>/i18n
|
||
module_i18n_paths = [
|
||
Path.home() / 'py' / module / 'i18n' / lang / 'msg.txt',
|
||
Path.home() / 'repos' / module / 'i18n' / lang / 'msg.txt',
|
||
]
|
||
|
||
for msg_path in module_i18n_paths:
|
||
if msg_path.exists():
|
||
before = len(merged)
|
||
merged.update(parse_msg_txt(msg_path))
|
||
added = len(merged) - before
|
||
print(f" ✓ {module}: +{added} 条 (共 {len(merged)} 条)")
|
||
break
|
||
|
||
# 写入结果
|
||
out_msg = wwwroot_i18n / lang / 'msg.txt'
|
||
out_json = wwwroot_i18n / lang / 'i18n.json'
|
||
|
||
write_msg_txt(merged, out_msg)
|
||
write_i18n_json(merged, out_json)
|
||
|
||
print(f" → 写入 {out_msg}")
|
||
print(f" → 写入 {out_json}")
|
||
|
||
print("\n✓ 合并完成")
|
||
|
||
|
||
if __name__ == '__main__':
|
||
merge_i18n()
|