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..25aa93d1 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 + # 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..2518a367 100644 --- a/tools/i18n/scan_i18n.py +++ b/tools/i18n/scan_i18n.py @@ -1,7 +1,16 @@ 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 + + def extract_i18n_strings(node): @@ -21,20 +30,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 +70,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())