|
| 1 | +#!/bin/python |
| 2 | +# -*- coding:utf-8 -*- |
| 3 | +""" |
| 4 | +所有文件统一使用 utf-8 编码, 行尾统一使用 LF |
| 5 | +运行前请下载最新的 messages.pot 和 messages.po 文件, 并放置在 locales 目录下 (鉴于网络原因, 不使用脚本请手动下载) |
| 6 | +https://github.com/apache/superset/blob/master/superset/translations/messages.pot |
| 7 | +https://github.com/apache/superset/blob/master/superset/translations/zh/LC_MESSAGES/messages.po |
| 8 | +先在项目的当前目录运行该脚本, 生成 target 待翻译目录 |
| 9 | +target/tmp/messages.json : 保存所有翻译, 用于修改现有翻译(如果补充翻译会被filter_messages.json覆盖) |
| 10 | +target/tmp/filter_messages.json : 保存未翻译的, 用于补充翻译 |
| 11 | +target/tmp/plural_messages.json : 保存复数翻译, 标记文件, 请勿修改 |
| 12 | +完善翻译后, 在项目的当前目录运行 generate_messages.py |
| 13 | +""" |
| 14 | + |
| 15 | +import json |
| 16 | +import os |
| 17 | + |
| 18 | +import polib |
| 19 | +from babel.messages.frontend import CommandLineInterface |
| 20 | + |
| 21 | +if __name__ == "__main__": |
| 22 | + # 根据最新的翻译模版 messages.pot 生成新的 messages.po |
| 23 | + argv = ["pybabel", "init", "-i", "locales/messages.pot", "-d", "target/locales", "-l", "en"] |
| 24 | + CommandLineInterface().run(argv) |
| 25 | + |
| 26 | + # 读取旧的 messages.json |
| 27 | + with open("messages.json", "r", encoding="utf-8") as f: |
| 28 | + old_trans = json.load(f)["locale_data"]["superset"] |
| 29 | + del old_trans[""] |
| 30 | + |
| 31 | + # 读取新的 messages.po |
| 32 | + po = polib.pofile("target/locales/en/LC_MESSAGES/messages.po") |
| 33 | + new_trans = {entry.msgid: entry.msgstr for entry in po} |
| 34 | + plural_trans = {entry.msgid: entry.msgid_plural for entry in po if entry.msgstr_plural} |
| 35 | + |
| 36 | + # 新的 messages.po 合并新的中文 messages.po 和旧的 messages.json |
| 37 | + zh_po = polib.pofile("locales/zh/LC_MESSAGES/messages.po") |
| 38 | + new_trans.update({zh_entry.msgid: zh_entry.msgstr for zh_entry in zh_po}) |
| 39 | + new_trans.update(old_trans) |
| 40 | + |
| 41 | + # 生成新的 messages.json |
| 42 | + os.makedirs("target/tmp", exist_ok=True) |
| 43 | + with open("target/tmp/messages.json", "w", encoding="utf-8") as f: |
| 44 | + json.dump(new_trans, f, ensure_ascii=False, indent=2, sort_keys=True) |
| 45 | + |
| 46 | + with open("target/tmp/filter_messages.json", "w", encoding="utf-8") as f: |
| 47 | + filter_trans = {k: v for k, v in new_trans.items() if v == "" or v == [""]} |
| 48 | + json.dump(filter_trans, f, ensure_ascii=False, indent=2, sort_keys=True) |
| 49 | + |
| 50 | + with open("target/tmp/plural_messages.json", "w", encoding="utf-8") as f: |
| 51 | + json.dump(plural_trans, f, ensure_ascii=False, indent=2, sort_keys=True) |
0 commit comments