Merge 56e5c51589893adbdf98a27a7db03d292afc6fe2 into 368045c94ed3264d9dd671e5809f0dbe1bba7a6f

This commit is contained in:
箱庭XTer 2024-07-10 13:10:12 +08:00 committed by GitHub
commit d602e78897
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 36 additions and 14 deletions

View File

@ -3,22 +3,22 @@ import locale
import os import os
def load_language_list(language): def load_language_list(language, locale_path="./i18n/locale"):
with open(f"./i18n/locale/{language}.json", "r", encoding="utf-8") as f: with open(os.path.join(locale_path, f"{language}.json"), "r", encoding="utf-8") as f:
language_list = json.load(f) language_list = json.load(f)
return language_list return language_list
class I18nAuto: class I18nAuto:
def __init__(self, language=None): def __init__(self, language=None, locale_path="./i18n/locale"):
if language in ["Auto", None]: if language in ["Auto", None]:
language = locale.getdefaultlocale()[ language = locale.getdefaultlocale()[
0 0
] # getlocale can't identify the system's language ((None, None)) ] # getlocale can't identify the system's language ((None, None))
if not os.path.exists(f"./i18n/locale/{language}.json"): if not os.path.exists(os.path.join(locale_path, f"{language}.json")):
language = "en_US" language = "en_US"
self.language = language self.language = language
self.language_map = load_language_list(language) self.language_map = load_language_list(language, locale_path)
def __call__(self, key): def __call__(self, key):
return self.language_map.get(key, key) return self.language_map.get(key, key)

View File

@ -2,11 +2,12 @@ import json
import os import os
from collections import OrderedDict from collections import OrderedDict
dir_path = "./i18n/locale" # The path to the i18n locale directory, you can change it to your own path
# Define the standard file name # Define the standard file name
standard_file = "locale/zh_CN.json" standard_file = os.path.join(dir_path, "zh_CN.json")
# Find all JSON files in the directory # Find all JSON files in the directory
dir_path = "locale/"
languages = [ languages = [
os.path.join(dir_path, f) os.path.join(dir_path, f)
for f in os.listdir(dir_path) for f in os.listdir(dir_path)

View File

@ -1,7 +1,16 @@
import ast import ast
import glob
import json import json
from collections import OrderedDict from collections import OrderedDict
import os
locale_path = "./i18n/locale" # The path to the i18n locale directory, you can change it to your own path
scan_list = ["./",
"GPT_SoVITS/",
"tools/"
] # The path to the directory you want to scan, you can change it to your own path
scan_subfolders = False # Whether to scan subfolders
def extract_i18n_strings(node): def extract_i18n_strings(node):
@ -21,20 +30,32 @@ def extract_i18n_strings(node):
return i18n_strings return i18n_strings
strings = []
# scan the directory for all .py files (recursively)
# for each file, parse the code into an AST # for each file, parse the code into an AST
# for each AST, extract the i18n strings # for each AST, extract the i18n strings
def scan_i18n_strings(filename):
strings = [] with open(filename, "r", encoding="utf-8") as f:
for filename in glob.iglob("**/*.py", recursive=True):
with open(filename, "r") as f:
code = f.read() code = f.read()
if "I18nAuto" in code: if "I18nAuto" in code:
tree = ast.parse(code) tree = ast.parse(code)
i18n_strings = extract_i18n_strings(tree) i18n_strings = extract_i18n_strings(tree)
print(filename, len(i18n_strings)) print(filename, len(i18n_strings))
strings.extend(i18n_strings) strings.extend(i18n_strings)
# scan the directory for all .py files (recursively)
if scan_subfolders:
for folder in scan_list:
for dirpath, dirnames, filenames in os.walk(folder):
for filename in [f for f in filenames if f.endswith(".py")]:
scan_i18n_strings(os.path.join(dirpath, filename))
else:
for folder in scan_list:
for filename in os.listdir(folder):
if filename.endswith(".py"):
scan_i18n_strings(os.path.join(folder, filename))
code_keys = set(strings) code_keys = set(strings)
""" """
n_i18n.py n_i18n.py
@ -49,7 +70,7 @@ print()
print("Total unique:", len(code_keys)) print("Total unique:", len(code_keys))
standard_file = "i18n/locale/zh_CN.json" standard_file = os.path.join(locale_path, "zh_CN.json")
with open(standard_file, "r", encoding="utf-8") as f: with open(standard_file, "r", encoding="utf-8") as f:
standard_data = json.load(f, object_pairs_hook=OrderedDict) standard_data = json.load(f, object_pairs_hook=OrderedDict)
standard_keys = set(standard_data.keys()) standard_keys = set(standard_data.keys())