Translator API¶
django_traduire.translator
¶
Translate the django-modeltranslation fields of a model instance.
The public entry points are :func:translate_instance and
:func:translate_queryset. Everything about request size, chunking and markup
is handled by the backend layer; what happens here is deciding what to
translate, and writing the result back without overflowing a column.
translate_instance(instance, source_language=None, target_languages=None, overwrite=False, fields=None)
¶
Translate the modeltranslation fields of one instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instance
|
Model
|
A model instance registered with modeltranslation. |
required |
source_language
|
str | None
|
Source language code. Defaults to
|
None
|
target_languages
|
Sequence[str] | None
|
Target language codes. Defaults to
|
None
|
overwrite
|
bool
|
Overwrite translations that are already filled in. |
False
|
fields
|
Sequence[str] | None
|
Restrict the run to these field names. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict[str, str]
|
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
BackendError
|
If a provider failed and |
Source code in src/django_traduire/translator.py
translate_queryset(queryset, source_language=None, target_languages=None, overwrite=False, fields=None, limit=MAX_INSTANCES_PER_RUN)
¶
Translate every instance of a queryset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
queryset
|
QuerySet
|
The queryset to walk. It is read with |
required |
source_language
|
str | None
|
Source language code. |
None
|
target_languages
|
Sequence[str] | None
|
Target language codes. |
None
|
overwrite
|
bool
|
Overwrite translations that are already filled in. |
False
|
fields
|
Sequence[str] | None
|
Restrict the run to these field names. |
None
|
limit
|
int
|
Hard bound on the instances walked, defaults to
:data: |
MAX_INSTANCES_PER_RUN
|
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Number of instances that received at least one translation. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/django_traduire/translator.py
get_backend()
cached
¶
Instantiate the configured backend, once per process.
Raises:
| Type | Description |
|---|---|
ConfigurationError
|
If |
Source code in src/django_traduire/translator.py
get_translation_fields(model)
¶
Return the field names modeltranslation registered for model.