[add] i18n support zh,en

This commit is contained in:
Mark
2026-03-11 10:45:07 +08:00
parent f1207dc8b9
commit 4f5ee24bc5
44 changed files with 5730 additions and 75 deletions

View File

@@ -0,0 +1,73 @@
"""
I18n Management API Schemas
This module defines Pydantic schemas for i18n management APIs.
"""
from pydantic import BaseModel, Field
from typing import Dict, List, Optional, Any
# ============================================================================
# Language Management Schemas
# ============================================================================
class LanguageInfo(BaseModel):
"""Language information"""
code: str = Field(..., description="Language code (e.g., 'zh', 'en')")
name: str = Field(..., description="Language name (e.g., 'Chinese', 'English')")
native_name: str = Field(..., description="Native language name (e.g., '中文', 'English')")
is_enabled: bool = Field(..., description="Whether the language is enabled")
is_default: bool = Field(..., description="Whether this is the default language")
class LanguageListResponse(BaseModel):
"""Response for language list"""
languages: List[LanguageInfo] = Field(..., description="List of available languages")
class LanguageCreateRequest(BaseModel):
"""Request to add a new language"""
code: str = Field(..., description="Language code (e.g., 'ja', 'ko')", min_length=2, max_length=10)
name: str = Field(..., description="Language name", min_length=1, max_length=100)
native_name: str = Field(..., description="Native language name", min_length=1, max_length=100)
is_enabled: bool = Field(default=True, description="Whether to enable the language")
class LanguageUpdateRequest(BaseModel):
"""Request to update language configuration"""
is_enabled: Optional[bool] = Field(None, description="Whether the language is enabled")
is_default: Optional[bool] = Field(None, description="Whether this is the default language")
# ============================================================================
# Translation Management Schemas
# ============================================================================
class TranslationResponse(BaseModel):
"""Response for translation data"""
translations: Dict[str, Dict[str, Any]] = Field(
...,
description="Translations organized by locale and namespace"
)
class TranslationUpdateRequest(BaseModel):
"""Request to update a translation"""
value: str = Field(..., description="New translation value", min_length=1)
description: Optional[str] = Field(None, description="Optional description of the translation")
class MissingTranslationsResponse(BaseModel):
"""Response for missing translations"""
missing_translations: Dict[str, List[str]] = Field(
...,
description="Missing translation keys organized by locale"
)
class ReloadResponse(BaseModel):
"""Response for translation reload"""
success: bool = Field(..., description="Whether the reload was successful")
reloaded_locales: List[str] = Field(..., description="List of reloaded locales")
total_locales: int = Field(..., description="Total number of available locales")

View File

@@ -11,6 +11,8 @@ class TenantBase(BaseModel):
name: str = Field(..., description="租户名称", max_length=255)
description: Optional[str] = Field(None, description="租户描述", max_length=1000)
is_active: bool = Field(True, description="是否激活")
default_language: Optional[str] = Field('zh', description="租户默认语言", max_length=10)
supported_languages: Optional[List[str]] = Field(['zh', 'en'], description="租户支持的语言列表")
@field_validator('name')
@classmethod
@@ -18,6 +20,26 @@ class TenantBase(BaseModel):
if not v or not v.strip():
raise ValidationException('租户名称不能为空', code=BizCode.VALIDATION_FAILED)
return v.strip()
@field_validator('default_language')
@classmethod
def validate_default_language(cls, v):
if v:
# Validate language code format (2-letter code, optionally with region)
import re
if not re.match(r'^[a-z]{2}(-[A-Z]{2})?$', v):
raise ValidationException('语言代码格式不正确', code=BizCode.VALIDATION_FAILED)
return v
@field_validator('supported_languages')
@classmethod
def validate_supported_languages(cls, v):
if v:
import re
for lang in v:
if not re.match(r'^[a-z]{2}(-[A-Z]{2})?$', lang):
raise ValidationException(f'语言代码格式不正确: {lang}', code=BizCode.VALIDATION_FAILED)
return v
class TenantCreate(TenantBase):
@@ -30,6 +52,8 @@ class TenantUpdate(BaseModel):
name: Optional[str] = Field(None, description="租户名称", max_length=255)
description: Optional[str] = Field(None, description="租户描述", max_length=1000)
is_active: Optional[bool] = Field(None, description="是否激活")
default_language: Optional[str] = Field(None, description="租户默认语言", max_length=10)
supported_languages: Optional[List[str]] = Field(None, description="租户支持的语言列表")
@field_validator('name')
@classmethod
@@ -37,6 +61,25 @@ class TenantUpdate(BaseModel):
if v is not None and (not v or not v.strip()):
raise ValidationException('租户名称不能为空', code=BizCode.VALIDATION_FAILED)
return v.strip() if v else v
@field_validator('default_language')
@classmethod
def validate_default_language(cls, v):
if v:
import re
if not re.match(r'^[a-z]{2}(-[A-Z]{2})?$', v):
raise ValidationException('语言代码格式不正确', code=BizCode.VALIDATION_FAILED)
return v
@field_validator('supported_languages')
@classmethod
def validate_supported_languages(cls, v):
if v:
import re
for lang in v:
if not re.match(r'^[a-z]{2}(-[A-Z]{2})?$', lang):
raise ValidationException(f'语言代码格式不正确: {lang}', code=BizCode.VALIDATION_FAILED)
return v
class Tenant(TenantBase):
@@ -62,4 +105,29 @@ class TenantList(BaseModel):
total: int
page: int
size: int
pages: int
pages: int
class TenantLanguageConfig(BaseModel):
"""租户语言配置Schema"""
default_language: str = Field(..., description="租户默认语言", max_length=10)
supported_languages: List[str] = Field(..., description="租户支持的语言列表")
@field_validator('default_language')
@classmethod
def validate_default_language(cls, v):
import re
if not re.match(r'^[a-z]{2}(-[A-Z]{2})?$', v):
raise ValidationException('语言代码格式不正确', code=BizCode.VALIDATION_FAILED)
return v
@field_validator('supported_languages')
@classmethod
def validate_supported_languages(cls, v):
if not v:
raise ValidationException('支持的语言列表不能为空', code=BizCode.VALIDATION_FAILED)
import re
for lang in v:
if not re.match(r'^[a-z]{2}(-[A-Z]{2})?$', lang):
raise ValidationException(f'语言代码格式不正确: {lang}', code=BizCode.VALIDATION_FAILED)
return v

View File

@@ -58,6 +58,16 @@ class VerifyPasswordRequest(BaseModel):
password: str = Field(..., description="密码")
class LanguagePreferenceRequest(BaseModel):
"""语言偏好设置请求"""
language: str = Field(..., min_length=2, max_length=10, description="语言代码,如 'zh', 'en'")
class LanguagePreferenceResponse(BaseModel):
"""语言偏好响应"""
language: str = Field(..., description="当前语言偏好")
class ChangePasswordResponse(BaseModel):
"""修改密码响应"""
message: str
@@ -74,6 +84,7 @@ class User(UserBase):
current_workspace_id: Optional[uuid.UUID] = None
current_workspace_name: Optional[str] = None
role: Optional[WorkspaceRole] = None
preferred_language: Optional[str] = "zh" # 用户语言偏好
# 将 datetime 转换为毫秒时间戳
@validator("created_at", pre=True)