Skip to content

Commit

Permalink
Fix rich text handling in POFile and Google translate engines
Browse files Browse the repository at this point in the history
  • Loading branch information
kaedroho committed Jan 8, 2020
1 parent 185656d commit fd6c1c9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
15 changes: 13 additions & 2 deletions wagtail_localize/translation/engines/google_translate/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def test_translate(self, Translator):
title="Test page",
slug="test-page",
test_charfield="Some translatable content",
test_richtextfield="<p>Translatable <b>rich text</b></p>",
)
request_page = self.add_page_to_request(page)

Expand All @@ -63,7 +64,14 @@ def test_translate(self, Translator):
"Some translatable content",
"Certains contenus traduisibles",
"Certains contenus traduisibles",
)
),
Translated(
"en",
"fr",
"Translatable rich text",
"Texte riche traduisible",
"Texte riche traduisible",
),
]

response = self.client.post(
Expand All @@ -83,7 +91,7 @@ def test_translate(self, Translator):
)

Translator().translate.assert_called_with(
["Some translatable content"], dest="fr", src="en"
["Some translatable content", "Translatable rich text"], dest="fr", src="en"
)

request_page.refresh_from_db()
Expand All @@ -94,6 +102,9 @@ def test_translate(self, Translator):
self.assertEqual(
translated_page.test_charfield, "Certains contenus traduisibles"
)
self.assertEqual(
translated_page.test_richtextfield, "<p>Texte riche traduisible</p>"
)

def test_translate_without_publishing(self, Translator):
page = create_test_page(
Expand Down
29 changes: 23 additions & 6 deletions wagtail_localize/translation/engines/pofile/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def test_download(self):
title="Test page",
slug="test-page",
test_charfield="Some translatable content",
test_richtextfield="<p>Translatable <b>rich text</b></p>",
)
self.add_page_to_request(page)

Expand All @@ -69,6 +70,11 @@ def test_download(self):
response.content,
)

self.assertIn(
b'#: pages/test-page/:test_richtextfield\nmsgid "Translatable rich text"\nmsgstr ""\n',
response.content,
)

def test_download_with_nested_snippet(self):
snippet = TestSnippet.objects.create(field="Some test snippet content")
page = create_test_page(
Expand Down Expand Up @@ -97,6 +103,7 @@ def test_upload(self):
title="Test page",
slug="test-page",
test_charfield="Some translatable content",
test_richtextfield="<p>Translatable <b>rich text</b></p>",
)
request_page = self.add_page_to_request(page)

Expand All @@ -107,12 +114,19 @@ def test_upload(self):
"Content-Type": "text/plain; charset=utf-8",
}

po.append(
polib.POEntry(
msgid="Some translatable content",
msgstr="Du contenu traduisible",
occurrences="",
)
po.extend(
[
polib.POEntry(
msgid="Some translatable content",
msgstr="Du contenu traduisible",
occurrences="",
),
polib.POEntry(
msgid="Translatable rich text",
msgstr="Texte riche traduisible",
occurrences="",
),
]
)

response = self.client.post(
Expand All @@ -137,6 +151,9 @@ def test_upload(self):
self.assertEqual(completed_page.locale, self.translation_request.target_locale)
self.assertEqual(completed_page.translation_key, page.translation_key)
self.assertEqual(completed_page.test_charfield, "Du contenu traduisible")
self.assertEqual(
completed_page.test_richtextfield, "<p>Texte riche traduisible</p>"
)

def test_upload_with_nested_snippet(self):
snippet = TestSnippet.objects.create(field="Some test snippet content")
Expand Down
8 changes: 7 additions & 1 deletion wagtail_localize/translation/engines/pofile/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,20 @@ def ingest_messages(self, instance):
for segment in text_segments:
if segment.text in self.translations:
translated_segments.append(
SegmentValue(segment.path, self.translations[segment.text])
SegmentValue(
segment.path,
self.translations[segment.text],
order=segment.order,
)
)
else:
missing_segments += 1

if missing_segments:
raise MissingSegmentsException(instance, missing_segments)

translated_segments.sort(key=lambda segment: segment.order)

try:
translation = instance.get_translation(self.target_locale)
except instance.__class__.DoesNotExist:
Expand Down

0 comments on commit fd6c1c9

Please sign in to comment.