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
39 changes: 39 additions & 0 deletions pontoon/machinery/tests/test_composed.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,45 @@ def test_composed_expands_source_plural_for_target_locale(
}


@pytest.mark.django_db
def test_composed_names_catchall_for_target_locale(client, fluent_resource, entity_a):
"""The catchall is named after the target locale's own last category.

en-US declares `*[other]`, but a locale with one/few/many needs `*[many]`.
"""
locale = LocaleFactory(code="uk-test", name="Plural many", cldr_plurals="1,3,4")

fluent_string = dedent(
"""\
popup =
{ $count ->
*[other] Many popups.
}
"""
)
fluent_entity = EntityFactory(resource=fluent_resource, string=fluent_string)

TranslationMemoryFactory.create(
entity=entity_a, source="Many popups.", target="TM_popups", locale=locale
)

url = reverse("pontoon.machinery_composed")
response = client.get(
url,
{
"entity": str(fluent_entity.pk),
"locale": locale.code,
"service": "translation-memory",
},
)
assert response.status_code == 200
assert json.loads(response.content)["value"]["alt"] == [
{"keys": ["one"], "pat": ["TM_popups"]},
{"keys": ["few"], "pat": ["TM_popups"]},
{"keys": [{"*": "many"}], "pat": ["TM_popups"]},
]


@pytest.mark.django_db
def test_composed_tm_only_partial_returns_empty(
client, fluent_resource, entity_a, locale_a
Expand Down
13 changes: 12 additions & 1 deletion pontoon/pretranslation/pretranslate.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ def message(self, msg: Message) -> None:
msg.pattern = self.pattern(msg.pattern)
else:
# catchall category is always last, and is always included
plurals = self.locale.cldr_plurals_list()[:-1]
locale_plurals = self.locale.cldr_plurals_list()
plurals = locale_plurals[:-1]

# Plural selectors need special attention
plural_selectors = [
Expand Down Expand Up @@ -226,6 +227,16 @@ def message(self, msg: Message) -> None:
tgt_variants[keys] = pattern
msg.variants = tgt_variants

# The catchall carries the source's category name, e.g. `other` from
# en-US. Name it after the target locale's own catchall category.
catchall = locale_plurals[-1] if locale_plurals else None
if catchall:
for idx in plural_selectors:
for keys in msg.variants:
key = keys[idx]
if isinstance(key, CatchallKey):
key.value = catchall

def pattern(self, pattern: Pattern) -> Pattern:
# First try to get a 100% match from Translation Memory
tm_source = (
Expand Down
39 changes: 39 additions & 0 deletions pontoon/pretranslation/tests/test_pretranslate.py
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,45 @@ def test_get_pretranslations_fluent_plural(
assert response == (pretranslated_string, "gt")


@patch("pontoon.pretranslation.pretranslate.get_google_translate_data")
@pytest.mark.django_db
def test_get_pretranslations_fluent_plural_catchall_named_for_locale(
gt_mock, fluent_resource, google_translate_locale
):
gt_mock.return_value = "GT"

# one, few, many — as in uk, ru, pl, be
google_translate_locale.cldr_plurals = "1,3,4"

input_string = dedent(
"""
plural =
{ $count ->
[one] One match found.
*[other] { $count } matches found.
}
"""
)
fluent_entity = EntityFactory(resource=fluent_resource, string=input_string)

expected = dedent(
"""
plural =
{ $count ->
[one] GT
[few] GT
*[many] GT
}
"""
)

# Re-serialize to match whitespace
pretranslated_string = serializer.serialize_entry(parser.parse_entry(expected))

response = get_pretranslation(fluent_entity, google_translate_locale)
assert response == (pretranslated_string, "gt")


@patch("pontoon.pretranslation.pretranslate.get_google_translate_data")
@pytest.mark.django_db
def test_get_pretranslations_fluent_complex(
Expand Down
Loading