Skip to content

Commit

Permalink
Fixes MT for languages with faulty BCP code
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeyStk committed Mar 16, 2023
1 parent 1e3fbb7 commit 119c7d7
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,7 @@ UNRELEASED
* [ [#1005](https://github.com/digitalfabrik/integreat-cms/issues/1005) ] Add possibility to filter for unused media files
* [ [#2048](https://github.com/digitalfabrik/integreat-cms/issues/2048) ] Add possibility to show media file usages in sidebar
* [ [#1875](https://github.com/digitalfabrik/integreat-cms/issues/1875) ] Add automatic translation options to Pages, Events and Locations
* [ [#2124](https://github.com/digitalfabrik/integreat-cms/issues/2124) ] Fixes MT for languages with faulty BCP code


2023.3.0
Expand Down
2 changes: 1 addition & 1 deletion integreat_cms/cms/templates/_base.html
Expand Up @@ -101,7 +101,7 @@
class="w-56 h-full fixed left-0 inset-y-0 text-gray-200 bg-gray-700 z-50 transform -translate-x-full lg:translate-x-0 motion-safe:transition-transform">
<div class="w-full p-1 h-14 bg-gray-800">
<a href="{% url 'public:region_selection' %}">
<div class="h-full w-full bg-{{ BRANDING }}-logo-white hover:bg-{{ BRANDING }}-logo-hover bg-contain bg-center bg-no-repeat">
<div class="h-full w-full bg-{{ BRANDING }}-logo-white hover:bg-{{ BRANDING }}-logo-hover ml-2 bg-contain bg-no-repeat">
</div>
</a>
</div>
Expand Down
8 changes: 7 additions & 1 deletion integreat_cms/cms/templates/languages/language_form.html
Expand Up @@ -54,7 +54,13 @@ <h3 class="font-bold text-black">
</div>
<label for="{{ form.bcp47_tag.id_for_label }}" class="block mb-2">{{ form.bcp47_tag.label }}</label>
{% render_field form.bcp47_tag|add_error_class:"border-red-500" %}
<div class="mb-2 text-s text-gray-600">{{ form.bcp47_tag.help_text }}</div>
<div class="mb-2 text-s text-gray-600">
{{ form.bcp47_tag.help_text }}
</ br>
{% if DEEPL_ENABLED %}
<i class="mr-2" icon-name="alert-triangle"></i>{% translate "For some language DeepL needs a correct BCP47 code (for example English or Portuguese). For exact information which languages require this, please checkout the" %} <a href="https://www.deepl.com/de/docs-api/translate-text/translate-text/">{% translate "DeepL documentation" %}</a>.
{% endif %}
</div>
</div>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion integreat_cms/cms/views/events/event_list_view.py
Expand Up @@ -109,7 +109,7 @@ def get(self, request, *args, **kwargs):

if settings.DEEPL_ENABLED:
deepl = DeepLApi()
DEEPL_AVAILABLE = deepl.check_availability(request, language_slug)
DEEPL_AVAILABLE = deepl.check_availability(request, region.default_language)
else:
DEEPL_AVAILABLE = False
MT_PERMITTED = mt_is_permitted(
Expand Down
2 changes: 1 addition & 1 deletion integreat_cms/cms/views/pois/poi_list_view.py
Expand Up @@ -120,7 +120,7 @@ def get(self, request, *args, **kwargs):
# DeepL available
if settings.DEEPL_ENABLED:
deepl = DeepLApi()
DEEPL_AVAILABLE = deepl.check_availability(request, language_slug)
DEEPL_AVAILABLE = deepl.check_availability(request, language)
else:
DEEPL_AVAILABLE = False
MT_PERMITTED = mt_is_permitted(
Expand Down
2 changes: 1 addition & 1 deletion integreat_cms/deepl_api/apps.py
Expand Up @@ -48,7 +48,7 @@ def ready(self):
)
assert self.supported_source_languages
self.supported_target_languages = [
target_languages.code.lower()[:2]
target_languages.code.lower()
for target_languages in deepl.translator.get_target_languages()
]
logger.debug(
Expand Down
37 changes: 26 additions & 11 deletions integreat_cms/deepl_api/utils.py
Expand Up @@ -27,27 +27,46 @@ def __init__(self):
self.translatable_attributes = ["title", "content", "meta_description"]

@staticmethod
def check_availability(request, language_slug):
def check_availability(request, target_language):
"""
This function checks, if the selected language is supported by DeepL
:param request: request that was sent to the server
:type request: ~django.http.HttpRequest
:param language_slug: current language slug
:type language_slug: str
:param target_language: transmitted target language
:type target_language: ~integreat_cms.cms.models.abstract_content_translation.AbstractContentTranslation
:return: true or false
:rtype: bool
"""
deepl_config = apps.get_app_config("deepl_api")
source_language = request.region.get_source_language(language_slug)
source_language = request.region.get_source_language(target_language.slug)
return (
source_language
and source_language.slug in deepl_config.supported_source_languages
and language_slug in deepl_config.supported_target_languages
and (
target_language.slug in deepl_config.supported_target_languages
or target_language.bcp47_code in deepl_config.supported_target_languages
)
)

def get_target_language_key(self, target_language):
"""
This function decides the correct target language key
:param target_language: the target language
:type target_language: ~integreat_cms.cms.models.abstract_content_translation.AbstractContentTranslation
:return: target_language_key which is 2 characters long for all languages except English and Portugese where the BCP code is transmitted
:rtype: str
"""
deepl_config = apps.get_app_config("deepl_api")
for code in [target_language.slug, target_language.bcp47_tag]:
if code in deepl_config.supported_target_languages:
return code
return None

def check_usage(self, region, source_translation):
"""
This function checks if the attempted translation would exceed the region's word limit
Expand Down Expand Up @@ -130,6 +149,8 @@ def deepl_translation(self, request, content_objects, language_slug, form_class)
)
return

target_language_key = self.get_target_language_key(target_language)

for content_object in content_objects:
source_translation = content_object.get_translation(
source_language.slug
Expand All @@ -147,12 +168,6 @@ def deepl_translation(self, request, content_objects, language_slug, form_class)
existing_target_translation = content_object.get_translation(
target_language.slug
)
# For some languages, the DeepL client expects the BCP tag instead of the short language code
target_language_key = (
target_language.bcp47_tag
if target_language.slug in ("en", "pt")
else target_language.slug
)

# Before translating, check if translation would exceed usage limit
(
Expand Down
17 changes: 17 additions & 0 deletions integreat_cms/locale/de/LC_MESSAGES/django.po
Expand Up @@ -5132,6 +5132,20 @@ msgstr "Das ist in vielen Fällen ein einzelner (Makro-)Sprachen-Tag aus der"
msgid "or"
msgstr "oder"

#: cms/templates/languages/language_form.html
msgid ""
"For some language DeepL needs a correct BCP47 code (for example English or "
"Portuguese). For exact information which languages require this, please "
"checkout the"
msgstr ""
"Für die maschinelle Übersetzung benötigt DeepL in manchen Sprachen den BCP47 "
"Code. Dies betrifft z.B. Englisch und Portugiesisch. Für aktuelle und genaue "
"Informationen, welche Sprachen das genau betrifft, lesen Sie bitte die"

#: cms/templates/languages/language_form.html
msgid "DeepL documentation"
msgstr "DeepL Dokumentation"

#: cms/templates/languages/language_form.html
#: cms/templates/offertemplates/offertemplate_form.html
#: cms/templates/organizations/organization_form.html
Expand Down Expand Up @@ -8452,6 +8466,9 @@ msgstr ""
"Diese Seite konnte nicht importiert werden, da sie zu einer anderen Region "
"gehört ({})."

#~ msgid "Your bcp code for the selected language seems to be incorrect."
#~ msgstr "Der BCP Code der ausgewählten Sprache ist leider nicht korrekt."

#~ msgid "No icon is set"
#~ msgstr "Kein Icon ist ausgewählt"

Expand Down

0 comments on commit 119c7d7

Please sign in to comment.