import json import locale import os def load_language_list(language): # 获取当前文件所在的目录 current_dir = os.path.dirname(os.path.abspath(__file__)) # 构建语言文件的绝对路径 language_file_path = os.path.join(current_dir, "locale", f"{language}.json") with open(language_file_path, "r", encoding="utf-8") as f: language_list = json.load(f) return language_list class I18nAuto: def __init__(self, language=None): if language in ["Auto", None]: language = locale.getdefaultlocale()[0] # getlocale can't identify the system's language ((None, None)) # 获取当前文件所在的目录 current_dir = os.path.dirname(os.path.abspath(__file__)) # 构建语言文件的绝对路径 language_file_path = os.path.join(current_dir, "locale", f"{language}.json") if not os.path.exists(language_file_path): language = "en_US" self.language = language self.language_map = load_language_list(language) def __call__(self, key): return self.language_map.get(key, key) def __repr__(self): return "Use Language: " + self.language