Google Translate API
Apps
Configuration of Google Translate API app
- class integreat_cms.google_translate_api.apps.GoogleTranslateApiClientConfig(app_name, app_module)[source]
Bases:
AppConfigGoogle Translate API config inheriting the django AppConfig
- classmethod create(entry)[source]
Factory that creates an app config from an entry in INSTALLED_APPS.
- 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.
- name: Final[str] = 'integreat_cms.google_translate_api'[source]
Full Python path to the application
- ready_v2(credentials: Credentials) None[source]
Preparing Google Translate with basic version
- Parameters:
credentials (Credentials)
- Return type:
None
Google Translate API Client
- class integreat_cms.google_translate_api.google_translate_api_client.GoogleTranslateApiClient(request: HttpRequest, form_class: ModelFormMetaclass)[source]
Bases:
MachineTranslationApiClientGoogle Translate API client to automatically translate selected objects.
- Parameters:
request (HttpRequest)
form_class (ModelFormMetaclass)
- __init__(request: HttpRequest, form_class: ModelFormMetaclass) None[source]
Initialize the Google Translate client
- Parameters:
request (HttpRequest) – The current request
form_class (ModelFormMetaclass) – The
CustomContentModelFormsubclass of the current content type
- 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_changes_made() None[source]
Add messages alerting the user that machine translation failed due to missing source translations
- 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
- alert_too_long_text() None[source]
Add messages alerting the user that machine translation failed because translation generated by MT was too long
- 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_changes_made: list[str] = [][source]
Content objects untranslatable since no actual changes were made
- failed_because_no_source_translation: list[str] = [][source]
Content objects untranslatable due to missing source translations
- failed_because_too_long_text: list[str] = [][source]
Content objects untranslatable due to too long text (only applies for push notification translation)
- filter_exceeds_limit(context: list[TranslationContext]) list[TranslationContext][source]
This method filters out entries from the context list if translating them would exceed the translation budget.
The removed entries are stored in order to show users batched error messages after all objects have been handled.
- Parameters:
context (list[TranslationContext]) – The list of translation contexts to filter
- Returns:
The filtered list of translation contexts
- Return type:
- filter_insufficient_hix_score(context: list[TranslationContext]) list[TranslationContext][source]
This method filters out entries from the context list if they do not have the required HIX score.
The removed entries are stored in order to show users batched error messages after all objects have been handled.
- Parameters:
context (list[TranslationContext]) – The list of translation contexts to filter
- Returns:
The filtered list of translation contexts
- Return type:
- filter_no_source_translation(context: list[TranslationContext]) list[TranslationContext][source]
This method filters out entries from the context list if they do not have the required source translation.
The removed entries are stored in order to show users batched error messages after all objects have been handled.
- Parameters:
context (list[TranslationContext]) – The list of translation contexts to filter
- Returns:
The filtered list of translation contexts
- Return type:
- filter_unchanged_translations(context: list[TranslationContext]) list[TranslationContext][source]
This method filters out entries from the context list if there have been no changes made to the source_translation.
The removed entries are stored in order to show users batched error messages after all objects have been handled.
- Parameters:
context (list[TranslationContext]) – The list of translation contexts to filter
- Returns:
The filtered list of translation contexts
- Return type:
- static get_target_language_key(target_language: Language) str[source]
This function decides the correct target language key
- invoke_translation_api(context: list[TranslationContext]) None[source]
Translate all content objects (wrapped by TranslationContext) stored in context using Google Translate.
- Parameters:
context (list[TranslationContext])
- Return type:
None
- mark_successful(ctx: TranslationContext) None[source]
Mark a content object as having been translated successfully
- Parameters:
ctx (TranslationContext)
- Return type:
None
- mark_too_long(ctx: TranslationContext, errors: bool) None[source]
Mark a translation as too long
- Parameters:
ctx (TranslationContext)
errors (bool)
- Return type:
None
- mark_unsuccessful(ctx: TranslationContext, errors: bool) None[source]
Mark a translation as unsuccessful (usually due to API errors)
- Parameters:
ctx (TranslationContext)
errors (bool)
- Return type:
None
- prepare_content_objects() list[TranslationContext][source]
Wrap each content object in a
TranslationContextand populate it with pre-computed translation metadata (source/target translations, translatable attributes, word count).- Returns:
A list of
TranslationContextinstances ready for filtering- Return type:
- 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(ctx: TranslationContext, translation_data: dict) None[source]
Create a translation form based on the extracted content object data, save it, and validate the translation.
- Parameters:
ctx (TranslationContext)
translation_data (dict)
- Return type:
None
- successful_translations: list[TranslationContext] = [][source]
Successful translations
- translate_object(obj: AbstractContentModel, language_slug: str) None[source]
This function translates one content object
- Parameters:
obj (AbstractContentModel) – The content object
language_slug (str) – The target language slug
- Return type:
None
- translate_queryset(queryset: list[AbstractContentModel], language_slug: str) None[source]
This function translates a content queryset via the configured machine translation provider.
Content objects are wrapped into
TranslationContextinstances, filtered for translatability, and then passed to the provider-specific translation API.- Parameters:
queryset (list[AbstractContentModel]) – The content QuerySet
language_slug (str) – The target language slug
- Return type:
None
Google Translate Provider
- class integreat_cms.google_translate_api.google_translate_provider.GoogleTranslateProvider[source]
Bases:
MachineTranslationProviderThe provider for Google machine translations
- api_client[source]
The API client class for this provider
alias of
GoogleTranslateApiClient
- 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.
- 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
- 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.