Skip to content

Commit a7e38f9

Browse files
authored
add SORT_KEYS (#1509)
1 parent 9df43ae commit a7e38f9

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

tools/i18n/scan_i18n.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
TITLE_LEN : int = 60 # 标题显示长度
1010
KEY_LEN : int = 30 # 键名显示长度
1111
SHOW_KEYS : bool = False # 是否显示键信息
12+
SORT_KEYS : bool = False # 是否按全局键名写入文件
1213

1314
def extract_i18n_strings(node):
1415
i18n_strings = []
@@ -49,6 +50,7 @@ def scan_i18n_strings():
4950
return code_keys
5051

5152
def update_i18n_json(json_file, standard_keys):
53+
standard_keys = sorted(standard_keys)
5254
print(f" Process {json_file} ".center(TITLE_LEN, "="))
5355
# 读取 JSON 文件
5456
with open(json_file, "r", encoding="utf-8") as f:
@@ -79,8 +81,13 @@ def update_i18n_json(json_file, standard_keys):
7981
print(f"{'Removed Unused Key'.ljust(KEY_LEN)}: {key}")
8082
# 按键顺序排序
8183
json_data = OrderedDict(
82-
sorted(json_data.items(),
83-
key=lambda x: list(standard_keys).index(x[0])))
84+
sorted(
85+
json_data.items(),
86+
key=lambda x: (
87+
list(standard_keys).index(x[0]) if x[0] in standard_keys and not x[1].startswith('#!') else len(json_data),
88+
)
89+
)
90+
)
8491
# 打印处理后的 JSON 条目数
8592
if len(miss_keys) != 0 or len(diff_keys) != 0:
8693
print(f"{'Total Keys (After)'.ljust(KEY_LEN)}: {len(json_data)}")
@@ -107,7 +114,7 @@ def update_i18n_json(json_file, standard_keys):
107114
print(f"\033[32m[Passed] All Keys Translated\033[0m")
108115
# 将处理后的结果写入 JSON 文件
109116
with open(json_file, "w", encoding="utf-8") as f:
110-
json.dump(json_data, f, ensure_ascii=False, indent=4, sort_keys=True)
117+
json.dump(json_data, f, ensure_ascii=False, indent=4, sort_keys=SORT_KEYS)
111118
f.write("\n")
112119
print(f" Updated {json_file} ".center(TITLE_LEN, "=") + '\n')
113120

0 commit comments

Comments
 (0)