Deepl API

Apps

Configuration of DeepL API app

class integreat_cms.deepl_api.apps.DeepLApiClientConfig(app_name, app_module)[source]

Bases: AppConfig

DeepL API config inheriting the django AppConfig

__init__(app_name, app_module)[source]
static assert_usage_limit_not_reached(translator: Translator) None[source]

Requests the usage from the translator and asserts that no limit was reached

Parameters:

translator (Translator) – The deepl translator

Return type:

None

classmethod create(entry)[source]

Factory that creates an app config from an entry in INSTALLED_APPS.

default_auto_field[source]
get_glossary(source_language: str, target_language: str) GlossaryInfo | None[source]

Looks up a glossary for the specified source language and target language pair. This method also returns the correct glossary for region variants (for example en-gb)

Parameters:
  • source_language (str) – The source language

  • target_language (str) – The target language

Returns:

A GlossaryInfo object or None

Return type:

GlossaryInfo | None

get_model(model_name, require_ready=True)[source]

Return the model with the given case-insensitive model_name.

Raise LookupError if no model exists with this name.

get_models(include_auto_created=False, include_swapped=False)[source]

Return an iterable of models.

By default, the following models aren’t included:

  • auto-created models for many-to-many relations without an explicit intermediate table,

  • models that have been swapped out.

Set the corresponding keyword argument to True to include such models. Keyword arguments aren’t documented; they’re a private API.

import_models()[source]
init_supported_glossaries(translator: Translator) None[source]

Requests the supported glossaries from the translator and sets them

Parameters:

translator (Translator) – The deepl translator

Return type:

None

init_supported_source_languages(translator: Translator) None[source]

Requests the supported sources languages from the translator and sets them

Parameters:

translator (Translator) – The deepl translator

Return type:

None

init_supported_target_languages(translator: Translator) None[source]

Requests the supported target languages from the translator and sets them

Parameters:

translator (Translator) – The deepl translator

Return type:

None

name: Final[str] = 'integreat_cms.deepl_api'[source]

Full Python path to the application

ready() None[source]

Checking if API is available

Return type:

None

supported_glossaries: dict[tuple[str, str], GlossaryInfo] = {}[source]

The supported glossaries, a map from (source_language, target_language) to glossary info

supported_source_languages: list[str] = [][source]

The supported source languages

supported_target_languages: list[str] = [][source]

The supported target languages

verbose_name: Final[Promise] = 'DeepL API'[source]

Human-readable name for the application

Deepl API Client

class integreat_cms.deepl_api.deepl_api_client.DeepLApiClient(request: HttpRequest, form_class: ModelFormMetaclass)[source]

Bases: MachineTranslationApiClient

DeepL API client to automatically translate selected objects.

Parameters:
  • request (HttpRequest)

  • form_class (ModelFormMetaclass)

__init__(request: HttpRequest, form_class: ModelFormMetaclass) None[source]

Initialize the DeepL client

Parameters:
  • region – The current region

  • form_class (ModelFormMetaclass) – The CustomContentModelForm subclass of the current content type

  • request (HttpRequest)

Return type:

None

alert_exceeds_limit() None[source]

Add messages alerting the user that machine translation failed due to insufficient translation budget

Return type:

None

alert_failed_translations() None[source]

Add messages informing the user about failed translations

Return type:

None

alert_insufficient_hix_score() None[source]

Add messages alerting the user that machine translation failed due to insufficient HIX scores

Return type:

None

alert_no_source_translation() None[source]

Add messages alerting the user that machine translation failed due to missing source translations

Return type:

None

alert_successful_translations() None[source]

Add messages informing the user about successful translations

Return type:

None

failed_because_exceeds_limit: list[str] = [][source]

Content objects untranslatable due to insufficient translation budget

failed_because_insufficient_hix_score: list[str] = [][source]

Content objects untranslatable due to too-low hix score

failed_because_no_source_translation: list[str] = [][source]

Content objects untranslatable due to missing source translations

failed_translations: list[str] = [][source]

Translations with an attached API failure

filter_exceeds_limit() None[source]

This method removes content objects from the main queryset if translating them would exceed the translation budget.

The removed elements are stored in order to show users batched error messages after all objects have been handled.

Parameters:

source_language – The source language slug

Return type:

None

filter_insufficient_hix_score() None[source]

This method removes content objects from the main queryset if they do not have the required HIX score.

The removed elements are stored in order to show users batched error messages after all objects have been handled.

Parameters:

source_language – The source language slug

Return type:

None

filter_no_source_translation() None[source]

This method removes content objects from the main queryset if they do not have the required source translation.

The removed elements are stored in order to show users batched error messages after all objects have been handled.

Parameters:

source_language – The source language slug

Return type:

None

form_class: ModelFormMetaclass[source]

The CustomContentModelForm

static get_target_language_key(target_language: Language) str[source]

This function decides the correct target language key

Parameters:

target_language (Language) – the target language

Returns:

target_language_key which is 2 characters long for all languages except English and Portuguese where the BCP tag is transmitted

Return type:

str

invoke_translation_api() None[source]

Translate all content objects stored in self.queryset using DeepL.

Return type:

None

mark_successful(content_object: Event | Page | POI) None[source]

Mark a content object as having been translated successfully

Parameters:

content_object (Event | Page | POI)

Return type:

None

mark_unsuccessful(content_object: Event | Page | POI, errors: bool) None[source]

Mark a translation as unuccessfull (usually due to API errors)

Parameters:
Return type:

None

prepare_content_objects() None[source]

Prepare the content objects to be translated by annotating them with information which otherwise would need to be recalculated multiple times.

Return type:

None

region: Region[source]

The current region

request: HttpRequest[source]

The current request

reset() None[source]

A single instance of a MachineTranslationApiClient may be used multiple times in succession, requiring a reset of the stored results and failures.

Forgetting to call this function will not result in additional machine translation budget use, but will produce nonsense messages shown to the user.

Return type:

None

save_translation(content_object: Event | Page | POI, translation_data: dict) None[source]

Create a translation form based on the extracted content object data, save it, and validate the translation.

Parameters:
Return type:

None

successful_translations: list[Event] | list[Page] | list[POI] = [][source]

Successful translations

translate_object(obj: Event | Page | POI, language_slug: str) None[source]

This function translates one content object

Parameters:
  • obj (Event | Page | POI) – The content object

  • language_slug (str) – The target language slug

Return type:

None

translate_queryset(queryset: list[Event] | list[Page] | list[POI], language_slug: str) None[source]

This function translates a content queryset via DeepL

Parameters:
Return type:

None

Deepl Provider

class integreat_cms.deepl_api.deepl_provider.DeepLProvider[source]

Bases: MachineTranslationProvider

The provider for DeepL machine translations

__init__()[source]
api_client[source]

The API client class for this provider

alias of DeepLApiClient

bulk_only_for_staff: bool = False[source]

Whether to require the staff permission for bulk actions

enabled: bool = False[source]

Whether the provider is globally enabled

classmethod is_enabled(region: Region, language: Language) bool[source]

Whether this provider is enabled for a given region and language. Call this from the parent class.

Parameters:
  • region (Region) – The given region

  • language (Language) – The given language

Returns:

Wether this provider is enabled for the given region and language

Return type:

bool

is_needed(queryset: QuerySet[Event | Page | POI], target_language: Language) list[Event | Page | POI][source]

Checks if a machine translation is needed, thus checking if the translation status is UP_TO_DATE or MACHINE_TRANSLATED and then returns a lit of translations which are to be updated

Parameters:
  • queryset (QuerySet[Event | Page | POI]) – The content model which should be translated

  • target_language (Language) – The target language

Returns:

translations which need to be translated and updated

Return type:

list[Event | Page | POI]

static is_permitted(region: Region, user: SimpleLazyObject, content_type: ModelBase) bool[source]

Checks if a machine translation is permitted, i.e. if for the given region, MT of the given content type is allowed and MT into the target language is enabled for the requesting user.

Parameters:
  • region (Region) – The current region

  • user (SimpleLazyObject) – The current user

  • content_type (ModelBase) – The content model which should be translated

Returns:

Whether the translation is permitted

Return type:

bool

name: str = 'DeepL'[source]

The readable name for this provider

region_enabled_attr: str | None = None[source]

The name of the region attribute which denotes whether the provider is enabled in a region

supported_source_languages: list[str] = [][source]

The supported source languages

supported_target_languages: list[str] = [][source]

The supported target languages