9
9
TITLE_LEN : int = 60 # 标题显示长度
10
10
KEY_LEN : int = 30 # 键名显示长度
11
11
SHOW_KEYS : bool = False # 是否显示键信息
12
+ SORT_KEYS : bool = False # 是否按全局键名写入文件
12
13
13
14
def extract_i18n_strings (node ):
14
15
i18n_strings = []
@@ -49,6 +50,7 @@ def scan_i18n_strings():
49
50
return code_keys
50
51
51
52
def update_i18n_json (json_file , standard_keys ):
53
+ standard_keys = sorted (standard_keys )
52
54
print (f" Process { json_file } " .center (TITLE_LEN , "=" ))
53
55
# 读取 JSON 文件
54
56
with open (json_file , "r" , encoding = "utf-8" ) as f :
@@ -79,8 +81,13 @@ def update_i18n_json(json_file, standard_keys):
79
81
print (f"{ 'Removed Unused Key' .ljust (KEY_LEN )} : { key } " )
80
82
# 按键顺序排序
81
83
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
+ )
84
91
# 打印处理后的 JSON 条目数
85
92
if len (miss_keys ) != 0 or len (diff_keys ) != 0 :
86
93
print (f"{ 'Total Keys (After)' .ljust (KEY_LEN )} : { len (json_data )} " )
@@ -107,7 +114,7 @@ def update_i18n_json(json_file, standard_keys):
107
114
print (f"\033 [32m[Passed] All Keys Translated\033 [0m" )
108
115
# 将处理后的结果写入 JSON 文件
109
116
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 )
111
118
f .write ("\n " )
112
119
print (f" Updated { json_file } " .center (TITLE_LEN , "=" ) + '\n ' )
113
120
0 commit comments