From 7d6f02521b9023446a41b589420dd6fedc6208b2 Mon Sep 17 00:00:00 2001 From: XTer Date: Thu, 14 Mar 2024 23:35:50 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=BC=BA=E4=BA=86i18n=E5=B7=A5?= =?UTF-8?q?=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Inference | 2 +- tools/i18n/i18n.py | 10 +++++----- tools/i18n/locale_diff.py | 5 +++-- tools/i18n/scan_i18n.py | 37 ++++++++++++++++++++++++++++++------- 4 files changed, 39 insertions(+), 15 deletions(-) diff --git a/Inference b/Inference index 605ebb78..33abd1a1 160000 --- a/Inference +++ b/Inference @@ -1 +1 @@ -Subproject commit 605ebb782519a37779fd4b67cf224a0758c4d762 +Subproject commit 33abd1a11e3d60a747e327626fac8bbd5011c2a0 diff --git a/tools/i18n/i18n.py b/tools/i18n/i18n.py index 00e91bf3..1861a034 100644 --- a/tools/i18n/i18n.py +++ b/tools/i18n/i18n.py @@ -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) diff --git a/tools/i18n/locale_diff.py b/tools/i18n/locale_diff.py index 674f7dd2..a9fd073f 100644 --- a/tools/i18n/locale_diff.py +++ b/tools/i18n/locale_diff.py @@ -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) diff --git a/tools/i18n/scan_i18n.py b/tools/i18n/scan_i18n.py index f3e52cf4..4f3a396a 100644 --- a/tools/i18n/scan_i18n.py +++ b/tools/i18n/scan_i18n.py @@ -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())