Spaces:
Sleeping
Sleeping
feat: 自动提取i18n文件
Browse files- ChuanhuChatbot.py +1 -1
- locale/extract_locale.py +75 -17
ChuanhuChatbot.py
CHANGED
@@ -467,7 +467,7 @@ with gr.Blocks(theme=small_and_beautiful_theme) as demo:
|
|
467 |
updatingMsg_i18n=i18n("正在尝试更新..."),
|
468 |
updateSuccess_i18n=i18n("更新成功,请重启本程序"),
|
469 |
updateFailure_i18n=i18n(
|
470 |
-
|
471 |
regenerate_i18n=i18n("重新生成"),
|
472 |
deleteRound_i18n=i18n("删除这轮问答"),
|
473 |
renameChat_i18n=i18n("重命名该对话"),
|
|
|
467 |
updatingMsg_i18n=i18n("正在尝试更新..."),
|
468 |
updateSuccess_i18n=i18n("更新成功,请重启本程序"),
|
469 |
updateFailure_i18n=i18n(
|
470 |
+
"更新失败,请尝试[手动更新](https://github.com/GaiZhenbiao/ChuanhuChatGPT/wiki/使用教程#手动更新)"),
|
471 |
regenerate_i18n=i18n("重新生成"),
|
472 |
deleteRound_i18n=i18n("删除这轮问答"),
|
473 |
renameChat_i18n=i18n("重命名该对话"),
|
locale/extract_locale.py
CHANGED
@@ -2,25 +2,83 @@ import os
|
|
2 |
import json
|
3 |
import re
|
4 |
|
5 |
-
|
6 |
-
pattern = r'i18n\(
|
7 |
|
8 |
-
# Load the .py
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
-
|
13 |
-
for filename in os.listdir("modules"):
|
14 |
-
if filename.endswith(".py"):
|
15 |
-
with open(os.path.join("modules", filename), "r", encoding="utf-8") as f:
|
16 |
-
contents += f.read()
|
17 |
|
18 |
-
# Matching with regular expressions
|
19 |
-
matches = re.findall(pattern, contents, re.DOTALL)
|
20 |
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import json
|
3 |
import re
|
4 |
|
5 |
+
def get_current_strings():
|
6 |
+
pattern = r'i18n\s*\(\s*["\']([^"\']*(?:\)[^"\']*)?)["\']\s*\)'
|
7 |
|
8 |
+
# Load the .py files
|
9 |
+
contents = ""
|
10 |
+
for dirpath, dirnames, filenames in os.walk("."):
|
11 |
+
for filename in filenames:
|
12 |
+
if filename.endswith(".py"):
|
13 |
+
filepath = os.path.join(dirpath, filename)
|
14 |
+
with open(filepath, 'r', encoding='utf-8') as f:
|
15 |
+
contents += f.read()
|
16 |
+
# Matching with regular expressions
|
17 |
+
matches = re.findall(pattern, contents, re.DOTALL)
|
18 |
+
data = {match.strip('()"'): '' for match in matches}
|
19 |
+
fixed_data = {} # fix some keys
|
20 |
+
for key, value in data.items():
|
21 |
+
if "](" in key and key.count("(") != key.count(")"):
|
22 |
+
fixed_data[key+")"] = value
|
23 |
+
else:
|
24 |
+
fixed_data[key] = value
|
25 |
|
26 |
+
return fixed_data
|
|
|
|
|
|
|
|
|
27 |
|
|
|
|
|
28 |
|
29 |
+
def get_locale_strings(filename):
|
30 |
+
try:
|
31 |
+
with open(filename, "r", encoding="utf-8") as f:
|
32 |
+
locale_strs = json.load(f)
|
33 |
+
except FileNotFoundError:
|
34 |
+
locale_strs = {}
|
35 |
+
return locale_strs
|
36 |
|
37 |
+
|
38 |
+
def sort_strings(existing_translations):
|
39 |
+
# Sort the merged data
|
40 |
+
sorted_translations = {}
|
41 |
+
# Add entries with (NOT USED) in their values
|
42 |
+
for key, value in sorted(existing_translations.items(), key=lambda x: x[0]):
|
43 |
+
if "(NOT USED)" in value:
|
44 |
+
sorted_translations[key] = value
|
45 |
+
# Add entries with empty values
|
46 |
+
for key, value in sorted(existing_translations.items(), key=lambda x: x[0]):
|
47 |
+
if value == "":
|
48 |
+
sorted_translations[key] = value
|
49 |
+
# Add the rest of the entries
|
50 |
+
for key, value in sorted(existing_translations.items(), key=lambda x: x[0]):
|
51 |
+
if value != "" and "(NOT USED)" not in value:
|
52 |
+
sorted_translations[key] = value
|
53 |
+
|
54 |
+
return sorted_translations
|
55 |
+
|
56 |
+
|
57 |
+
current_strs = get_current_strings()
|
58 |
+
|
59 |
+
locale_files = []
|
60 |
+
# 遍历locale目录下的所有json文件
|
61 |
+
for dirpath, dirnames, filenames in os.walk("locale"):
|
62 |
+
for filename in filenames:
|
63 |
+
if filename.endswith(".json"):
|
64 |
+
locale_files.append(os.path.join(dirpath, filename))
|
65 |
+
|
66 |
+
|
67 |
+
for locale_filename in locale_files:
|
68 |
+
if "zh_CN" in locale_filename:
|
69 |
+
continue
|
70 |
+
locale_strs = get_locale_strings(locale_filename)
|
71 |
+
|
72 |
+
# Add new keys
|
73 |
+
for key in current_strs:
|
74 |
+
if key not in locale_strs:
|
75 |
+
locale_strs[key] = ""
|
76 |
+
# Add (NOT USED) to invalid keys
|
77 |
+
for key in locale_strs:
|
78 |
+
if key not in current_strs:
|
79 |
+
locale_strs[key] = "(NOT USED)" + locale_strs[key]
|
80 |
+
|
81 |
+
locale_strs = sort_strings(locale_strs)
|
82 |
+
|
83 |
+
with open(locale_filename, 'w', encoding='utf-8') as f:
|
84 |
+
json.dump(locale_strs, f, ensure_ascii=False, indent=4)
|