languages
attribute
LANGUAGE_CODES= get_args('LanguageCode')
attribute
LanguageCode= Literal['en', 'ru', 'zh', 'fr', 'de', 'es', 'it', 'pt', 'ja', 'ko']
attribute
T= TypeVar('T', bound='BaseModel')
attribute
LocalizedValueTypeAlias
= Annotated[dict[LanguageCode, Any], BeforeValidator(validate_localized_value)]
func
is_valid_language_codeis_valid_language_code(code) -> TypeGuard[LanguageCode]
param
codestr
Returns
TypeGuard[LanguageCode]
func
validate_localized_valuevalidate_localized_value(value, /, info) -> dict[LanguageCode, Any]
Validate and normalize a localized value to ensure it has the correct structure. If the value is a string, it will be converted to a dictionary with the default language.
param
valuedict[str, Any] | str
param
infoValidationInfo
Returns
dict[LanguageCode, Any]
func
normalize_dictnormalize_dict(data, /, default_lang='en') -> dict[LanguageCode, dict[str, Any] | str]
Normalize a nested dictionary to have top-level language keys. Each value under the language keys will be a flattened dict of keys from different levels.
Implementation Note:
- Use pre-order DFS traversal
- Detect language keys (e.g. "en", "ru") at any level.
- Accumulate the full key path to place values in the final structure under the correct lang.
- Treat non-language values as belonging to a default language (e.g. "en").
param
datadict[str, Any]
param
default_langLanguageCode
= 'en'
Returns
dict[LanguageCode, dict[str, Any] | str]
func
get_localized_valueget_localized_value(data, /, lang, fallback_lang='en') -> Any
Get a localized value from a dictionary of localized values. If the specified language is not available, fallback to the fallback language.
param
datadict[LanguageCode, Any]
param
langLanguageCode
param
fallback_langLanguageCode
= 'en'
Returns
Any
func
flatten_dictflatten_dict(data, /, prefix='') -> str
Flatten a dictionary with language keys into a single string in the format "path: value". Each path is a dot-separated string representing the hierarchy of keys.
\{
"user": \{
"profile": \{
"name": "Alice",
"age": 30
\},
"contact": \{
"email": "alice@example.com"
\}
\},
"product": "Laptop"
\}
```json
becomes:
```text
user.profile.name: Alice
user.profile.age: 30
user.contact.email: alice@example.com
product: Laptop
param
datadict[str, Any]
param
prefixstr
= ''
Returns
str
Last updated on