Skip to content

Commit

Permalink
fix(gwt): gracefully handle plurals without other form
Browse files Browse the repository at this point in the history
  • Loading branch information
nijel committed Dec 13, 2024
1 parent 98b3deb commit da8227d
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 3 deletions.
56 changes: 56 additions & 0 deletions tests/translate/storage/test_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,62 @@ def test_encoding(self):
propunit.source = "xZkouška"
assert bytes(propfile).decode() == "test=xZkou\\u0161ka\n"

def test_other_plurals(self):
propsource = r"""userItems.limit=Only X items can be added.
userItems.limit[one]=Only one item can be added.
userItems.limit[few]=Only {0} items can be added.
userItems.limit[many]=Only {0} items can be added.
"""
propfile = self.propparse(propsource, sourcelanguage="pl", targetlanguage="pl")
propsource_en = r"""
userItems.test={0} items can be added.
userItems.test[one]=One item can be added.
"""
propfile_en = self.propparse(
propsource_en, sourcelanguage="en", targetlanguage="en"
)

assert len(propfile_en.units) == 1
assert len(propfile.units) == 1
propunit = propfile.units[0]
assert propunit.name == "userItems.limit"
assert isinstance(propunit.target, multistring)
assert propunit.source.strings == [
"Only one item can be added.",
"Only {0} items can be added.",
"Only {0} items can be added.",
]
propunit.target = multistring(
[
"Only one item can be added.",
"Only {0} items can be added.",
"Only many {0} items can be added.",
]
)

unit = propfile_en.units[0]
propfile.addunit(unit)
unit.target = multistring(
[
"Only one item can be added.",
"Only {0} items can be added.",
"Only many {0} items can be added.",
]
)

assert (
bytes(propfile).decode()
== """userItems.limit=Only many {0} items can be added.
userItems.limit[one]=Only one item can be added.
userItems.limit[few]=Only {0} items can be added.
userItems.limit[many]=Only many {0} items can be added.
userItems.test=Only many {0} items can be added.
userItems.test[one]=Only one item can be added.
userItems.test[few]=Only {0} items can be added.
userItems.test[many]=Only many {0} items can be added.
"""
)


class TestProp(test_monolingual.TestMonolingualStore):
StoreClass = properties.propfile
Expand Down
20 changes: 17 additions & 3 deletions translate/storage/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,10 @@ def get_key_cldr_name(key):
def get_cldr_names_order():
return ["other"]

@staticmethod
def get_expand_output_target_mapping(names: list[str]) -> list[str]:
return names


@register_dialect
class DialectJava(Dialect):
Expand Down Expand Up @@ -480,7 +484,7 @@ def get_key_cldr_name(cls, key):

@classmethod
def get_cldr_names_order(cls):
return [y for x, y in cls.gwt_plural_categories]
return [y for _x, y in cls.gwt_plural_categories]

@classmethod
def get_key(cls, key, variant):
Expand All @@ -504,6 +508,12 @@ def decode(cls, string):
result = super().decode(string)
return result.replace("''", "'")

@staticmethod
def get_expand_output_target_mapping(names: list[str]) -> list[str]:
if "other" not in names:
return ["other", *names]
return names


@register_dialect
class DialectSkype(Dialect):
Expand Down Expand Up @@ -640,7 +650,9 @@ def _get_source_unit(self):

def _get_ordered_units(self):
# Used for str (GWT order)
mapping = self._get_target_mapping()
mapping = self.personality.get_expand_output_target_mapping(
self._get_target_mapping()
)
names = [
name for name in self.personality.get_cldr_names_order() if name in mapping
]
Expand All @@ -654,7 +666,7 @@ def hasplural(self, key=None):
def settarget(self, text):
mapping = None
if isinstance(text, multistring):
strings = text.strings
strings = [str(x) for x in text.strings]
elif isinstance(text, list):
strings = text
elif isinstance(text, dict):
Expand All @@ -670,6 +682,8 @@ def settarget(self, text):
raise ValueError(
f'Not same plural counts between "{strings}" and "{units}"'
)
if "other" not in mapping and "other" in self.units:
self.units["other"].target = strings[-1]

for a, b in zip(strings, units):
b.target = a
Expand Down

0 comments on commit da8227d

Please sign in to comment.