From 9fc894152914fa06fb6f0aaf04821fa841788349 Mon Sep 17 00:00:00 2001 From: yumoqing Date: Thu, 25 Jun 2026 13:53:32 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0i18n=E5=90=88?= =?UTF-8?q?=E5=B9=B6=E8=84=9A=E6=9C=AC=20-=20=E5=B0=86=E6=89=80=E6=9C=89?= =?UTF-8?q?=E6=A8=A1=E5=9D=97i18n=E5=90=88=E5=B9=B6=E5=88=B0wwwroot/i18n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- script/merge_i18n.py | 118 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 script/merge_i18n.py diff --git a/script/merge_i18n.py b/script/merge_i18n.py new file mode 100644 index 00000000..56532268 --- /dev/null +++ b/script/merge_i18n.py @@ -0,0 +1,118 @@ +#!/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//i18n 或 ~/repos//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()