Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 13 additions & 16 deletions pontoon/pretranslation/pretranslate.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,26 +197,23 @@ def pattern(self, pattern: Pattern) -> Pattern:

if self.locale.google_translate_code:
# Try to fetch from Google Translate
gt_response = get_google_translate_data(
gt_translation = get_google_translate_data(
text=gt_source,
locale=self.locale,
preserve_placeables=self.preserve_placeables,
)
if gt_response["status"]:
self.services.append("gt")
return [
el
if idx % 2 == 0
else (
placeholders[int(el)]
if int(el) < len(placeholders)
else "{$" + el + "}"
)
for idx, el in enumerate(
pt_placeholder.split(gt_response["translation"])
)
if el != ""
]
self.services.append("gt")
return [
el
if idx % 2 == 0
else (
placeholders[int(el)]
if int(el) < len(placeholders)
else "{$" + el + "}"
)
for idx, el in enumerate(pt_placeholder.split(gt_translation))
if el != ""
]

raise ValueError(
f"Pretranslation for `{self.source}` to `{self.locale.code}` not available"
Expand Down
82 changes: 17 additions & 65 deletions pontoon/pretranslation/tests/test_pretranslate.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,7 @@ def test_get_pretranslations_tm_match(entity_a, entity_b, locale_b):
@pytest.mark.django_db
def test_get_pretranslations_gt_match(gt_mock, entity_a, google_translate_locale):
# 100% TM match does not exist and locale.google_translate_code is not None
gt_mock.return_value = {
"status": True,
"translation": "gt_translation",
}
gt_mock.return_value = "gt_translation"

response = get_pretranslation(entity_a, google_translate_locale)
assert response == ("gt_translation", "gt")
Expand All @@ -96,10 +93,7 @@ def test_get_pretranslations_fluent_simple(
gt_mock, fluent_resource, google_translate_locale
):
# Entity.string is a simple Fluent string
gt_mock.return_value = {
"status": True,
"translation": "gt_translation",
}
gt_mock.return_value = "gt_translation"

fluent_string = "hello-world = Hello World!"
fluent_entity = EntityFactory(resource=fluent_resource, string=fluent_string)
Expand All @@ -115,10 +109,7 @@ def test_get_pretranslations_fluent_empty(
gt_mock, fluent_resource, google_translate_locale
):
# Entity.string is an empty Fluent string
gt_mock.return_value = {
"status": True,
"translation": "gt_translation",
}
gt_mock.return_value = "gt_translation"

fluent_string = 'hello-world = { "" }\n'
fluent_entity = EntityFactory(resource=fluent_resource, string=fluent_string)
Expand All @@ -142,10 +133,7 @@ def test_get_pretranslations_fluent_accesskeys_no_attribute_source(
)
fluent_entity = EntityFactory(resource=fluent_resource, string=input_string)

gt_mock.return_value = {
"status": True,
"translation": "gt_translation",
}
gt_mock.return_value = "gt_translation"

expected = dedent(
"""
Expand Down Expand Up @@ -178,10 +166,7 @@ def test_get_pretranslations_fluent_accesskeys_opt_out(
)
fluent_entity = EntityFactory(resource=fluent_resource, string=input_string)

gt_mock.return_value = {
"status": True,
"translation": "gt_translation",
}
gt_mock.return_value = "gt_translation"

expected = dedent(
"""
Expand Down Expand Up @@ -212,10 +197,7 @@ def test_get_pretranslations_fluent_accesskeys_value(
)
fluent_entity = EntityFactory(resource=fluent_resource, string=input_string)

gt_mock.return_value = {
"status": True,
"translation": "gt_translation",
}
gt_mock.return_value = "gt_translation"

expected = dedent(
"""
Expand Down Expand Up @@ -248,10 +230,7 @@ def test_get_pretranslations_fluent_accesskeys_label_attribute(
)
fluent_entity = EntityFactory(resource=fluent_resource, string=input_string)

gt_mock.return_value = {
"status": True,
"translation": "gt_translation",
}
gt_mock.return_value = "gt_translation"

expected = dedent(
"""
Expand Down Expand Up @@ -286,10 +265,7 @@ def test_get_pretranslations_fluent_accesskeys_value_attribute(
)
fluent_entity = EntityFactory(resource=fluent_resource, string=input_string)

gt_mock.return_value = {
"status": True,
"translation": "gt_translation",
}
gt_mock.return_value = "gt_translation"

expected = dedent(
"""
Expand Down Expand Up @@ -322,10 +298,7 @@ def test_get_pretranslations_fluent_accesskeys_aria_label_attribute(
)
fluent_entity = EntityFactory(resource=fluent_resource, string=input_string)

gt_mock.return_value = {
"status": True,
"translation": "gt_translation",
}
gt_mock.return_value = "gt_translation"

expected = dedent(
"""
Expand Down Expand Up @@ -357,10 +330,7 @@ def test_get_pretranslations_fluent_accesskeys_prefixed_label_attribute(
)
fluent_entity = EntityFactory(resource=fluent_resource, string=input_string)

gt_mock.return_value = {
"status": True,
"translation": "gt_translation",
}
gt_mock.return_value = "gt_translation"

expected = dedent(
"""
Expand Down Expand Up @@ -392,10 +362,7 @@ def test_get_pretranslations_fluent_accesskeys_ignore_placeables(
)
fluent_entity = EntityFactory(resource=fluent_resource, string=input_string)

gt_mock.return_value = {
"status": True,
"translation": "{ $0 } gt_translation",
}
gt_mock.return_value = "{ $0 } gt_translation"

# The `title` value here is hacky, but demonstrates that
# a Google Translate response that includes an unexpected `{$0}`
Expand Down Expand Up @@ -436,10 +403,7 @@ def test_get_pretranslations_fluent_accesskeys_select_expression_source(
)
fluent_entity = EntityFactory(resource=fluent_resource, string=input_string)

gt_mock.return_value = {
"status": True,
"translation": "gt_translation",
}
gt_mock.return_value = "gt_translation"

expected = dedent(
"""
Expand Down Expand Up @@ -479,10 +443,7 @@ def test_get_pretranslations_fluent_accesskeys_select_expression_accesskey(
)
fluent_entity = EntityFactory(resource=fluent_resource, string=input_string)

gt_mock.return_value = {
"status": True,
"translation": "gt_translation",
}
gt_mock.return_value = "gt_translation"

expected = dedent(
"""
Expand Down Expand Up @@ -580,10 +541,7 @@ def test_get_pretranslations_fluent_accesskeys_number_literal_source(
)
fluent_entity = EntityFactory(resource=fluent_resource, string=input_string)

gt_mock.return_value = {
"status": True,
"translation": "gt_translation",
}
gt_mock.return_value = "gt_translation"

google_translate_locale.cldr_plurals = "1,5"

Expand Down Expand Up @@ -642,10 +600,7 @@ def test_get_pretranslations_fluent_plural(
gt_mock, fluent_resource, google_translate_locale
):
# Various types of whitespace should be preserved
gt_mock.return_value = {
"status": True,
"translation": "GT",
}
gt_mock.return_value = "GT"

google_translate_locale.cldr_plurals = "1,2,3,5"

Expand Down Expand Up @@ -690,10 +645,7 @@ def test_get_pretranslations_fluent_complex(
# - Uplift selector and repeat shared parts within variants.
# - Only translate text nodes.
# - Service with most translated nodes is picked as translation author.
gt_mock.return_value = {
"status": True,
"translation": "GT: Open Options and select Search.",
}
gt_mock.return_value = "GT: Open Options and select Search."

TranslationMemoryFactory.create(
entity=entity_a,
Expand Down Expand Up @@ -747,7 +699,7 @@ def test_get_pretranslations_fluent_placeholders(
# - Placeholders are replaced by {$0}, {$1}, ... when passing to Google Translate.
# - Original placeholders are retained in final result.
def gt_mock_fn(**kwargs):
return {"status": True, "translation": f"GT: {kwargs['text']}"}
return f"GT: {kwargs['text']}"

gt_mock.side_effect = gt_mock_fn

Expand Down
Loading