mirror of
https://github.com/jumpserver/lina.git
synced 2025-04-28 03:20:24 +00:00
feat: 添加 i18n 自动 diff 和 update 工具 (i18n-util.py)
This commit is contained in:
parent
be6e1a3449
commit
05c2425bc6
@ -4,11 +4,17 @@ root = true
|
|||||||
[*]
|
[*]
|
||||||
charset = utf-8
|
charset = utf-8
|
||||||
indent_style = space
|
indent_style = space
|
||||||
indent_size = 2
|
|
||||||
end_of_line = lf
|
end_of_line = lf
|
||||||
insert_final_newline = true
|
insert_final_newline = true
|
||||||
trim_trailing_whitespace = true
|
trim_trailing_whitespace = true
|
||||||
|
|
||||||
|
|
||||||
|
[*.{js,jsx,ts,tsx,vue}]
|
||||||
|
indent_size = 2
|
||||||
|
|
||||||
|
[*.py]
|
||||||
|
indent_size = 4
|
||||||
|
|
||||||
[*.md]
|
[*.md]
|
||||||
insert_final_newline = false
|
insert_final_newline = false
|
||||||
trim_trailing_whitespace = false
|
trim_trailing_whitespace = false
|
||||||
|
103
src/i18n/langs/i18n-util.py
Executable file
103
src/i18n/langs/i18n-util.py
Executable file
@ -0,0 +1,103 @@
|
|||||||
|
"""
|
||||||
|
requirements:
|
||||||
|
pip install data-tree
|
||||||
|
pip install pathdict
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
import json
|
||||||
|
import argparse
|
||||||
|
import data_tree
|
||||||
|
from pathdict import PathDict
|
||||||
|
|
||||||
|
|
||||||
|
class I18NFileUtil(object):
|
||||||
|
def diff(self, lang):
|
||||||
|
zh_json = self.load_json('./zh.json')
|
||||||
|
zh_tree = data_tree.Data_tree_node(arg_data=zh_json)
|
||||||
|
zh_paths = list(zh_tree.paths(arg_bool_get_paths_as_strings=True))
|
||||||
|
|
||||||
|
lang_json = self.load_json(f'./{lang}.json')
|
||||||
|
lang_tree = data_tree.Data_tree_node(arg_data=lang_json)
|
||||||
|
lang_paths = list(lang_tree.paths(arg_bool_get_paths_as_strings=True))
|
||||||
|
|
||||||
|
diff_paths = set(zh_paths) - set(lang_paths)
|
||||||
|
|
||||||
|
data = {}
|
||||||
|
diff_filepath = f'./diff-zh-{lang}.json'
|
||||||
|
with open(diff_filepath, 'w', encoding='utf-8') as f:
|
||||||
|
for path in diff_paths:
|
||||||
|
value = zh_tree.get(path)
|
||||||
|
if not isinstance(value, str):
|
||||||
|
continue
|
||||||
|
data[path] = value
|
||||||
|
json_data = json.dumps(data, ensure_ascii=False, indent=2)
|
||||||
|
f.write(json_data)
|
||||||
|
|
||||||
|
def update(self, lang):
|
||||||
|
diff_data = self.load_json(f'./diff-zh-{lang}.json')
|
||||||
|
lang_data = self.load_json(f'./{lang}.json')
|
||||||
|
lang_pdict = PathDict(lang_data, create_if_not_exists=True)
|
||||||
|
for key_path, value in diff_data.items():
|
||||||
|
lang_pdict[key_path] = value
|
||||||
|
|
||||||
|
with open(f'./{lang}.json', 'w', encoding='utf-8') as f:
|
||||||
|
data = self.pathdict_to_dict(lang_pdict)
|
||||||
|
data = json.dumps(data, ensure_ascii=False, indent=2)
|
||||||
|
f.write(data)
|
||||||
|
|
||||||
|
def pathdict_to_dict(self, data):
|
||||||
|
d = {}
|
||||||
|
for k, v in data.items():
|
||||||
|
if isinstance(v, PathDict):
|
||||||
|
v = self.pathdict_to_dict(v)
|
||||||
|
d[k] = v
|
||||||
|
return d
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def load_json(filename):
|
||||||
|
with open(filename, 'r') as f:
|
||||||
|
data = f.read()
|
||||||
|
return json.loads(data)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
|
||||||
|
parser = argparse.ArgumentParser(
|
||||||
|
description="""
|
||||||
|
Lina i18n util
|
||||||
|
|
||||||
|
Example: \r\n
|
||||||
|
|
||||||
|
%(prog)s diff en(ja);
|
||||||
|
%(prog)s update en(ja);
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
'action', type=str, choices=("diff", "update"),
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
'langs', type=str, choices=("en", "ja"), nargs='*'
|
||||||
|
)
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
zh = 'zh'
|
||||||
|
en = 'en'
|
||||||
|
ja = 'ja'
|
||||||
|
|
||||||
|
# I18NFileUtil().diff(en)
|
||||||
|
# I18NFileUtil().update(en)
|
||||||
|
#
|
||||||
|
# I18NFileUtil().diff(ja)
|
||||||
|
# I18NFileUtil().update(ja)
|
||||||
|
|
||||||
|
action = args.action
|
||||||
|
langs = args.langs
|
||||||
|
|
||||||
|
print(action, '>>>>', args.langs)
|
||||||
|
|
||||||
|
util = I18NFileUtil()
|
||||||
|
method = getattr(util, action)
|
||||||
|
for _lang in langs:
|
||||||
|
method(_lang)
|
Loading…
Reference in New Issue
Block a user