Reference
Translator
Translator(
default_language: str,
languages: tuple[str, ...],
fallback_languages: dict[str, tuple[str, ...]] | None = None,
)
A translator object that manages translations for registered SQLModel classes.
Parameters:
-
(default_languagestr) –The language to use if no language was set externally.
-
(languagestuple[str, ...]) –All supported languages i.e the translations you want to store.
-
(fallback_languagesdict[str, tuple[str, ...]] | None, default:None) –Fallbacks for each language used when the active language is not in
languages. An example:{ 'default': ('en', 'pl', 'de'), 'fr': 'es' }. The default key is required.
Raises:
-
ImproperlyConfiguredError–If the configuration is internally inconsistent.
Methods:
-
register–Register a SQLModel class for translations.
Source code in src/modeltranslation/translator.py
register
Register a SQLModel class for translations.
This function returns a decorator that applies TranslationOptions
to the given SQLModel class. After applying, the model
will have translation accessors and metadata set up automatically.
Parameters:
-
(modelSQLModel) –the class to apply translations on.
Raises:
-
ImproperlyConfiguredError–If the translation options are inconsistent with the Translator.
Examples:
>>> from sqlmodel import SQLModel
>>> from modeltranslation import Translator
...
>>> class Book(SQLModel, table=True):
... title: str
...
>>> translator = Translator(
... default_language="en",
... languages=("en", "pl"))
...
>>> @translator.register(Book)
... class BookTranslationOptions(TranslationOptions):
... fields=('title',)
Source code in src/modeltranslation/translator.py
TranslationOptions
Base class for configuring the translation of SQLModel classes.
This class defines which fields are translated, which translations are required and how to handle missing values.
Examples:
>>> from modeltranslation import TranslationOptions
>>> class BookTranslationOptions(TranslationOptions):
... fields = ("title",)
... required_languages = ("en",)
Attributes:
-
fallback_languages(dict[str, tuple[str, ...]] | None) –Languages to use when the current language is missing.
-
fallback_values(dict[str, Any] | Any) –The values to use if all fallback languages yielded no value.
-
fields(tuple[str, ...]) –Names of fields to translate.
-
required_languages(dict[str, tuple[str, ...]] | tuple[str, ...] | None) –The required translations for this class.
fallback_languages
class-attribute
instance-attribute
fallback_values
class-attribute
instance-attribute
fields
class-attribute
instance-attribute
Names of fields to translate.
Example
(title,description)
required_languages
class-attribute
instance-attribute
The required translations for this class.
This also affects the pydantic model and typehints.
Example
('en',)
The fallbacks can be also specified with a dictionary. This makes it possible to set the requirements per field.
The default key is required.
For english, title and author are required. For all other languages only title is required.
apply_translation
apply_translation(app: FastAPI, translator: Translator) -> None
Configure the app set the current language as a context variable.
Applies middleware to FastAPI app which sets language based on the accept-language HTTP header. The resolved language is stored in the translator per execution context.
Parameters:
-
(appFastAPI) –FastAPI application.
-
(translatorTranslator) –The translator used to register translations in this app.
Examples:
>>> from fastapi import FastAPI
>>> from modeltranslation import Translator, apply_translation
...
>>> translator = Translator(
... default_language="en",
... languages=("en", "pl"),
... )
>>> app = FastAPI()
>>> apply_translation(app, translator)
Note
In a typical use case, you would register translations with the translator before calling this function.