Liman

languages

attributeLANGUAGE_CODES
= get_args('LanguageCode')
attributeLanguageCode
= Literal['en', 'ru', 'zh', 'fr', 'de', 'es', 'it', 'pt', 'ja', 'ko']
attributeT
= TypeVar('T', bound='BaseModel')
attributeLocalizedValueTypeAlias
= Annotated[dict[LanguageCode, Any], BeforeValidator(validate_localized_value)]
funcis_valid_language_codeis_valid_language_code(code) -> TypeGuard[LanguageCode]
paramcodestr

Returns

TypeGuard[LanguageCode]
funcvalidate_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.

paramvaluedict[str, Any] | str
paraminfoValidationInfo

Returns

dict[LanguageCode, Any]
funcnormalize_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").
paramdatadict[str, Any]
paramdefault_langLanguageCode
= 'en'

Returns

dict[LanguageCode, dict[str, Any] | str]
funcget_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.

paramdatadict[LanguageCode, Any]
paramlangLanguageCode
paramfallback_langLanguageCode
= 'en'

Returns

Any
funcflatten_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
paramdatadict[str, Any]
paramprefixstr
= ''

Returns

str

Last updated on