diff --git a/backend/common/i18n.py b/backend/common/i18n.py index 2785c4f8..bcd46905 100644 --- a/backend/common/i18n.py +++ b/backend/common/i18n.py @@ -1,15 +1,25 @@ +import glob +import json + +from pathlib import Path from typing import Any +import yaml + from starlette_context.errors import ContextDoesNotExistError from backend.common.context import ctx from backend.core.conf import settings -from backend.locales.loader import locale_loader +from backend.core.path_conf import LOCALE_DIR class I18n: """国际化管理器""" + def __init__(self) -> None: + self.locales: dict[str, dict[str, Any]] = {} + self.load_locales() + @property def current_language(self) -> str: """获取当前请求的语言""" @@ -23,6 +33,29 @@ class I18n: """设置当前请求的语言""" ctx.language = language + def load_locales(self) -> None: + """加载语言文本""" + patterns = [ + LOCALE_DIR / '*.json', + LOCALE_DIR / '*.yaml', + LOCALE_DIR / '*.yml', + ] + + lang_files = [] + + for pattern in patterns: + lang_files.extend(glob.glob(str(pattern))) + + for lang_file in lang_files: + with open(lang_file, encoding='utf-8') as f: + lang = Path(lang_file).stem + file_type = Path(lang_file).suffix[1:] + match file_type: + case 'json': + self.locales[lang] = json.loads(f.read()) + case 'yaml' | 'yml': + self.locales[lang] = yaml.full_load(f.read()) + def t(self, key: str, default: Any | None = None, **kwargs) -> str: """ 翻译函数 @@ -35,10 +68,10 @@ class I18n: keys = key.split('.') try: - translation = locale_loader.locales[self.current_language] + translation = self.locales[self.current_language] except KeyError: keys = 'error.language_not_found'.split('.') - translation = locale_loader.locales[settings.I18N_DEFAULT_LANGUAGE] + translation = self.locales[settings.I18N_DEFAULT_LANGUAGE] for k in keys: if isinstance(translation, dict) and k in list(translation.keys()): diff --git a/backend/core/path_conf.py b/backend/core/path_conf.py index e1ef24b6..eef12ba7 100644 --- a/backend/core/path_conf.py +++ b/backend/core/path_conf.py @@ -25,7 +25,7 @@ UPLOAD_DIR = STATIC_DIR / 'upload' PLUGIN_DIR = BASE_PATH / 'plugin' # 国际化文件目录 -LOCALE_DIR = BASE_PATH / 'locales' / 'langs' +LOCALE_DIR = BASE_PATH / 'langs' # MySQL 脚本目录 MYSQL_SCRIPT_DIR = BASE_PATH / 'sql' / 'mysql' diff --git a/backend/locales/langs/en-US.json b/backend/langs/en-US.json similarity index 100% rename from backend/locales/langs/en-US.json rename to backend/langs/en-US.json diff --git a/backend/locales/langs/zh-CN.yml b/backend/langs/zh-CN.yml similarity index 100% rename from backend/locales/langs/zh-CN.yml rename to backend/langs/zh-CN.yml diff --git a/backend/locales/__init__.py b/backend/locales/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/backend/locales/loader.py b/backend/locales/loader.py deleted file mode 100644 index 54784be2..00000000 --- a/backend/locales/loader.py +++ /dev/null @@ -1,44 +0,0 @@ -import glob -import json - -from pathlib import Path -from typing import Any - -import yaml - -from backend.core.path_conf import LOCALE_DIR - - -class LocaleLoader: - """语言文件加载器""" - - def __init__(self) -> None: - self.locales: dict[str, dict[str, Any]] = {} - self.load_locales() - - def load_locales(self) -> None: - """加载语言文本""" - patterns = [ - LOCALE_DIR / '*.json', - LOCALE_DIR / '*.yaml', - LOCALE_DIR / '*.yml', - ] - - lang_files = [] - - for pattern in patterns: - lang_files.extend(glob.glob(str(pattern))) - - for lang_file in lang_files: - with open(lang_file, encoding='utf-8') as f: - lang = Path(lang_file).stem - file_type = Path(lang_file).suffix[1:] - match file_type: - case 'json': - self.locales[lang] = json.loads(f.read()) - case 'yaml' | 'yml': - self.locales[lang] = yaml.full_load(f.read()) - - -# 创建语言加载器单例 -locale_loader = LocaleLoader()