增强了i18n工具

This commit is contained in:
XTer 2024-03-14 23:35:50 +08:00
parent 6317c3a2f4
commit 7d6f02521b
4 changed files with 39 additions and 15 deletions

@ -1 +1 @@
Subproject commit 605ebb782519a37779fd4b67cf224a0758c4d762
Subproject commit 33abd1a11e3d60a747e327626fac8bbd5011c2a0

View File

@ -3,22 +3,22 @@ import locale
import os
def load_language_list(language):
with open(f"./i18n/locale/{language}.json", "r", encoding="utf-8") as f:
def load_language_list(language, locale_path="./i18n/locale"):
with open(os.path.join(locale_path, f"{language}.json"), "r", encoding="utf-8") as f:
language_list = json.load(f)
return language_list
class I18nAuto:
def __init__(self, language=None):
def __init__(self, language=None, locale_path="./i18n/locale"):
if language in ["Auto", None]:
language = locale.getdefaultlocale()[
0
] # 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"
self.language = language
self.language_map = load_language_list(language)
self.language_map = load_language_list(language, locale_path)
def __call__(self, key):
return self.language_map.get(key, key)

View File

@ -2,11 +2,12 @@ import json
import os
from collections import OrderedDict
# dir_path = "./i18n/locale" # The path to the i18n locale directory, you can change it to your own path
dir_path = "./Inference/i18n/locale"
# 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
dir_path = "locale/"
languages = [
os.path.join(dir_path, f)
for f in os.listdir(dir_path)

View File

@ -1,7 +1,18 @@
import ast
import glob
import json
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
locale_path = "./Inference/i18n/locale"
scan_list = ["./Inference/"] # The path to the directory you want to scan, you can change it to your own path
scan_subfolders = True
def extract_i18n_strings(node):
@ -21,20 +32,32 @@ def extract_i18n_strings(node):
return i18n_strings
strings = []
# scan the directory for all .py files (recursively)
# for each file, parse the code into an AST
# for each AST, extract the i18n strings
strings = []
for filename in glob.iglob("**/*.py", recursive=True):
with open(filename, "r") as f:
def scan_i18n_strings(filename):
with open(filename, "r", encoding="utf-8") as f:
code = f.read()
if "I18nAuto" in code:
tree = ast.parse(code)
i18n_strings = extract_i18n_strings(tree)
print(filename, len(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)
"""
n_i18n.py
@ -49,7 +72,7 @@ print()
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:
standard_data = json.load(f, object_pairs_hook=OrderedDict)
standard_keys = set(standard_data.keys())