feat(i18n): add backend translation utility with shared locale files
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
|
||||
from .file_parser import FileParser
|
||||
from .llm_client import LLMClient
|
||||
from .locale import t, get_locale, get_language_instruction
|
||||
|
||||
__all__ = ['FileParser', 'LLMClient']
|
||||
__all__ = ['FileParser', 'LLMClient', 't', 'get_locale', 'get_language_instruction']
|
||||
|
||||
|
||||
60
backend/app/utils/locale.py
Normal file
60
backend/app/utils/locale.py
Normal file
@@ -0,0 +1,60 @@
|
||||
import json
|
||||
import os
|
||||
from flask import request, has_request_context
|
||||
|
||||
_locales_dir = os.path.join(os.path.dirname(__file__), '..', '..', '..', 'locales')
|
||||
|
||||
# Load language registry
|
||||
with open(os.path.join(_locales_dir, 'languages.json'), 'r', encoding='utf-8') as f:
|
||||
_languages = json.load(f)
|
||||
|
||||
# Load translation files
|
||||
_translations = {}
|
||||
for filename in os.listdir(_locales_dir):
|
||||
if filename.endswith('.json') and filename != 'languages.json':
|
||||
locale_name = filename[:-5]
|
||||
with open(os.path.join(_locales_dir, filename), 'r', encoding='utf-8') as f:
|
||||
_translations[locale_name] = json.load(f)
|
||||
|
||||
|
||||
def get_locale() -> str:
|
||||
if has_request_context():
|
||||
return request.headers.get('Accept-Language', 'zh')
|
||||
return 'zh'
|
||||
|
||||
|
||||
def t(key: str, **kwargs) -> str:
|
||||
locale = get_locale()
|
||||
messages = _translations.get(locale, _translations.get('zh', {}))
|
||||
|
||||
value = messages
|
||||
for part in key.split('.'):
|
||||
if isinstance(value, dict):
|
||||
value = value.get(part)
|
||||
else:
|
||||
value = None
|
||||
break
|
||||
|
||||
if value is None:
|
||||
value = _translations.get('zh', {})
|
||||
for part in key.split('.'):
|
||||
if isinstance(value, dict):
|
||||
value = value.get(part)
|
||||
else:
|
||||
value = None
|
||||
break
|
||||
|
||||
if value is None:
|
||||
return key
|
||||
|
||||
if kwargs:
|
||||
for k, v in kwargs.items():
|
||||
value = value.replace(f'{{{k}}}', str(v))
|
||||
|
||||
return value
|
||||
|
||||
|
||||
def get_language_instruction() -> str:
|
||||
locale = get_locale()
|
||||
lang_config = _languages.get(locale, _languages.get('zh', {}))
|
||||
return lang_config.get('llmInstruction', '请使用中文回答。')
|
||||
Reference in New Issue
Block a user