feat: 增强国际化验证功能,添加对共享翻译字符串的支持,优化提示模板加载逻辑
This commit is contained in:
@@ -3,18 +3,16 @@ from __future__ import annotations
|
||||
from collections.abc import Iterator
|
||||
from datetime import date, datetime
|
||||
from decimal import Decimal
|
||||
from functools import lru_cache
|
||||
|
||||
from .loaders import DEFAULT_LOCALE
|
||||
|
||||
|
||||
@lru_cache(maxsize=1)
|
||||
def _get_manager():
|
||||
from .manager import I18nManager
|
||||
|
||||
manager = getattr(_get_manager, "_manager", None)
|
||||
if manager is None:
|
||||
manager = I18nManager()
|
||||
_get_manager._manager = manager
|
||||
return manager
|
||||
return I18nManager()
|
||||
|
||||
|
||||
def set_locale(locale: str) -> str:
|
||||
|
||||
@@ -67,22 +67,12 @@ class I18nManager:
|
||||
self._catalog_cache.pop(normalize_locale(locale), None)
|
||||
|
||||
def t(self, key: str, locale: str | None = None, **kwargs: object) -> str:
|
||||
translation_value, _ = self._get_translation_value(key, locale)
|
||||
if translation_value is None:
|
||||
translation_value, translation_locale = self._get_translation_value(key, locale)
|
||||
template = self._get_standard_template(key, translation_value, translation_locale)
|
||||
if template is None:
|
||||
return key
|
||||
|
||||
if isinstance(translation_value, dict):
|
||||
template = translation_value.get("other")
|
||||
if template is None:
|
||||
self._log_once(
|
||||
("plural_missing_other", self.get_locale(), key),
|
||||
logging.WARNING,
|
||||
"翻译 key '%s' 缺少 other plural category,已回退到 key 本身",
|
||||
key,
|
||||
)
|
||||
return key
|
||||
return self._format_translation(key, template, kwargs)
|
||||
return self._format_translation(key, translation_value, kwargs)
|
||||
return self._format_translation(key, template, kwargs)
|
||||
|
||||
def tn(self, key: str, count: int | float, locale: str | None = None, **kwargs: object) -> str:
|
||||
translation_value, translation_locale = self._get_translation_value(key, locale)
|
||||
@@ -118,6 +108,27 @@ class I18nManager:
|
||||
formatting_kwargs["count"] = count
|
||||
return self._format_translation(key, template, formatting_kwargs)
|
||||
|
||||
def _get_standard_template(
|
||||
self,
|
||||
key: str,
|
||||
translation_value: TranslationValue | None,
|
||||
translation_locale: str,
|
||||
) -> str | None:
|
||||
if translation_value is None:
|
||||
return None
|
||||
if not isinstance(translation_value, dict):
|
||||
return translation_value
|
||||
|
||||
template = translation_value.get("other")
|
||||
if template is None:
|
||||
self._log_once(
|
||||
("plural_missing_other", translation_locale, key),
|
||||
logging.WARNING,
|
||||
"翻译 key '%s' 缺少 other plural category,已回退到 key 本身",
|
||||
key,
|
||||
)
|
||||
return template
|
||||
|
||||
def _format_translation(self, key: str, template: str, kwargs: dict[str, object]) -> str:
|
||||
try:
|
||||
return format_template(template, **kwargs)
|
||||
@@ -161,14 +172,15 @@ class I18nManager:
|
||||
try:
|
||||
return normalize_locale(locale)
|
||||
except InvalidLocaleError:
|
||||
current_locale = self.get_locale()
|
||||
self._log_once(
|
||||
("invalid_locale", "explicit", locale),
|
||||
logging.WARNING,
|
||||
"检测到非法 locale='%s',已回退到当前默认 locale %s",
|
||||
locale,
|
||||
self.get_locale(),
|
||||
current_locale,
|
||||
)
|
||||
return self.get_locale()
|
||||
return current_locale
|
||||
|
||||
def _get_catalog(self, locale: str) -> dict[str, TranslationValue]:
|
||||
normalized_locale = normalize_locale(locale)
|
||||
|
||||
Reference in New Issue
Block a user