Backends API¶
django_traduire.backends.base.BaseBackend
¶
Bases: ABC
Abstract base class for translation backends.
Class attributes describe what a single request to the provider accepts:
Attributes:
| Name | Type | Description |
|---|---|---|
max_chars |
int
|
Characters a single request may carry. Long fields are cut on paragraph, then sentence, then word boundaries to fit. |
max_texts |
int
|
Texts a single request may carry. Short fields are grouped up to this number. |
supports_html |
bool
|
True when the provider understands markup and keeps it. Rich text is then sent sentence by sentence with its inline tags left in. When False, the inline tags are replaced by placeholders before the call and restored after it. |
translate_attributes |
bool
|
Translate the attributes a reader sees — |
Source code in src/django_traduire/backends/base.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | |
translate_batch(texts, source, target, is_html=False)
¶
Translate texts of any length, splitting requests as needed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
texts
|
Sequence[str]
|
Texts to translate. Length is not limited. |
required |
source
|
str
|
Source language code. |
required |
target
|
str
|
Target language code. |
required |
is_html
|
bool
|
True when the texts are rich text / HTML. |
False
|
Returns:
| Type | Description |
|---|---|
list[str]
|
list[str]: One translation per input text, in the same order. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
BackendError
|
If the provider returns an unusable answer. |
Source code in src/django_traduire/backends/base.py
translate(text, source, target, is_html=False)
¶
Translate a single string. Convenience wrapper around :meth:translate_batch.
Source code in src/django_traduire/backends/base.py
translate_raw(texts, source, target, is_html=False)
abstractmethod
¶
Send one request to the provider.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
texts
|
list[str]
|
Texts that already fit |
required |
source
|
str
|
Source language code (e.g. |
required |
target
|
str
|
Target language code (e.g. |
required |
is_html
|
bool
|
True when the texts carry markup and the provider was declared able to handle it. |
False
|
Returns:
| Type | Description |
|---|---|
list[str]
|
list[str]: Translations, in order, as many as there were inputs. |
Source code in src/django_traduire/backends/base.py
django_traduire.backends.google_free.GoogleFreeBackend
¶
Bases: BaseBackend
Translation backend using the free Google Translate endpoint.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
endpoint
|
str
|
Endpoint URL. Override it to point at a mirror. |
DEFAULT_ENDPOINT
|
timeout
|
float
|
Seconds to wait for one request. |
10.0
|
retries
|
int
|
Attempts per request, capped at :data: |
3
|
rate_limit
|
float
|
Seconds to wait between two requests. Raise it when translating large tables. |
0.0
|
max_chars
|
int | None
|
Characters per request. The endpoint refuses much more than 5000; long fields are split before they get here. |
None
|
user_agent
|
str
|
|
DEFAULT_USER_AGENT
|
Source code in src/django_traduire/backends/google_free.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | |
translate_raw(texts, source, target, is_html=False)
¶
Translate texts one request at a time (the endpoint takes one query).
Source code in src/django_traduire/backends/google_free.py
django_traduire.backends.deepl.DeepLBackend
¶
Bases: BaseBackend
Translation backend using the DeepL API.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
auth_key
|
str
|
Your DeepL API authentication key. |
required |
max_chars
|
int | None
|
Characters per request (DeepL caps a request at 128 KiB). |
None
|
**kwargs
|
Any
|
Additional options passed to |
{}
|
Source code in src/django_traduire/backends/deepl.py
translate_raw(texts, source, target, is_html=False)
¶
Send one request to DeepL, with tag handling when the text is markup.
Source code in src/django_traduire/backends/deepl.py
django_traduire.backends.google.GoogleBackend
¶
Bases: BaseBackend
Translation backend using the Google Cloud Translation API v3.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
project_id
|
str
|
Your Google Cloud project ID. |
required |
location
|
str
|
API location (default |
'global'
|
max_chars
|
int | None
|
Characters per request (the API caps one call at 30 000 code points). |
None
|
**kwargs
|
Any
|
Additional options passed to |
{}
|
Source code in src/django_traduire/backends/google.py
translate_raw(texts, source, target, is_html=False)
¶
Send one request to Google Cloud Translation.
Source code in src/django_traduire/backends/google.py
django_traduire.backends.openai.OpenAIBackend
¶
Bases: BaseBackend
Translation backend using an OpenAI-compatible chat completions API.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
api_key
|
str | None
|
API key for the server. |
None
|
model
|
str
|
Model name (default |
'gpt-4o-mini'
|
temperature
|
float
|
Sampling temperature; keep it low for translation. |
0.1
|
json_mode
|
bool
|
Ask the server for a JSON object response. Turn it off for
servers that do not implement |
True
|
max_chars
|
int | None
|
Characters per request. Keep it well under the model's output limit — the answer is as long as the input. |
None
|
**kwargs
|
Any
|
Additional options passed to |
{}
|
Source code in src/django_traduire/backends/openai.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | |
translate_raw(texts, source, target, is_html=False)
¶
Ask the model for one JSON answer covering every text of the request.