Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add machine_translate_pofile management command #263

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions wagtail_localize/machine_translators/deepl.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ def language_code(code, is_target=False):
class DeepLTranslator(BaseMachineTranslator):
display_name = "DeepL"

def translate(self, source_locale, target_locale, strings):
def translate(self, source_language_code, target_language_code, strings):
response = requests.post('https://api.deepl.com/v2/translate', {
'auth_key': self.options['AUTH_KEY'],
'text': [string.data for string in strings],
'tag_handling': 'xml',
'source_lang': language_code(source_locale.language_code),
'target_lang': language_code(target_locale.language_code, is_target=True),
'source_lang': language_code(source_language_code),
'target_lang': language_code(target_language_code, is_target=True),
})

return {
string: StringValue(translation['text'])
for string, translation in zip(strings, response.json()['translations'])
}

def can_translate(self, source_locale, target_locale):
return language_code(source_locale.language_code) != language_code(target_locale.language_code, is_target=True)
def can_translate(self, source_language_code, target_language_code):
return language_code(source_language_code) != language_code(target_language_code, is_target=True)
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def test_translate_text(self):
def test_translate_html(self):
string, attrs = StringValue.from_source_html('<a href="https://en.wikipedia.org/wiki/World">Hello world!</a>. <b>This is a test</b>.')

translations = DummyTranslator({}).translate(self.english_locale, self.french_locale, [
translations = DummyTranslator({}).translate(self.english_locale.language_code, self.french_locale.language_code, [
string
])

Expand All @@ -33,11 +33,11 @@ def test_translate_html(self):
def test_can_translate(self):
canada_french_locale = Locale.objects.create(language_code="fr-CA")

self.assertTrue(DummyTranslator({}).can_translate(self.english_locale, self.french_locale))
self.assertTrue(DummyTranslator({}).can_translate(self.english_locale, canada_french_locale))
self.assertTrue(DummyTranslator({}).can_translate(self.english_locale.language_code, self.french_locale.language_code))
self.assertTrue(DummyTranslator({}).can_translate(self.english_locale.language_code, canada_french_locale.language_code))

# Can't translate the same language
self.assertFalse(DummyTranslator({}).can_translate(self.english_locale, self.english_locale))
self.assertFalse(DummyTranslator({}).can_translate(self.english_locale.language_code, self.english_locale.language_code))

# Can't translate two variants of the same language
self.assertFalse(DummyTranslator({}).can_translate(self.french_locale, canada_french_locale))
self.assertFalse(DummyTranslator({}).can_translate(self.french_locale.language_code, canada_french_locale.language_code))
30 changes: 30 additions & 0 deletions wagtail_localize/management/commands/machine_translate_pofile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from pathlib import Path

import polib
from django.core.management.base import BaseCommand

from wagtail_localize.machine_translators import get_machine_translator
from wagtail_localize.strings import StringValue


class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument('source_language_code', type=str)
parser.add_argument('target_language_code', type=str)
parser.add_argument('pofile', type=Path)

def handle(self, source_language_code, target_language_code, pofile, **options):
po = polib.pofile(str(pofile))

messages = set(StringValue(entry.msgid) for entry in po)

translator = get_machine_translator()
translations = translator.translate(source_language_code, target_language_code, list(messages))

new_po = polib.POFile()

for entry in po:
entry.msgstr = translations[StringValue(entry.msgid)].data
new_po.append(entry)

print(str(new_po))
2 changes: 1 addition & 1 deletion wagtail_localize/views/edit_translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,7 @@ def machine_translate(request, translation_id):
segments[segment.string].append((string_segment.string_id, string_segment.context_id))

if segments:
translations = translator.translate(translation.source.locale, translation.target_locale, segments.keys())
translations = translator.translate(translation.source.locale.language_code, translation.target_locale.language_code, segments.keys())

with transaction.atomic():
for string, contexts in segments.items():
Expand Down