Commit 7522004e authored by Mohamed Heikal's avatar Mohamed Heikal Committed by Commit Bot

Grit: Fixes android_xml.py where translations have multiple same key plurals

Due to some bug within the translation pipeline, translated grd files
sometimes end up with multiple plural entries that have the same/similar
key, eg: '=1' and 'one'. This change makes sure that the xml output only
has one entry in per key. This is required by aapt2, which fails with an error
otherwise.

Bug: 787488
Change-Id: I6d98c1739a7dbd3872c31cbfca63b0bd5612c79d
Reviewed-on: https://chromium-review.googlesource.com/807148
Commit-Queue: Mohamed Heikal <mheikal@chromium.org>
Reviewed-by: default avatarRobert Flack <flackr@chromium.org>
Reviewed-by: default avataragrieve <agrieve@chromium.org>
Cr-Commit-Position: refs/heads/master@{#521888}
parent 1539faa0
...@@ -174,13 +174,17 @@ def _FormatPluralMessage(message): ...@@ -174,13 +174,17 @@ def _FormatPluralMessage(message):
return None return None
body_in = plural_match.group('items').strip() body_in = plural_match.group('items').strip()
lines = [] lines = []
quantities_so_far = set()
for item_match in _PLURALS_ITEM_PATTERN.finditer(body_in): for item_match in _PLURALS_ITEM_PATTERN.finditer(body_in):
quantity_in = item_match.group('quantity') quantity_in = item_match.group('quantity')
quantity_out = _PLURALS_QUANTITY_MAP.get(quantity_in) quantity_out = _PLURALS_QUANTITY_MAP.get(quantity_in)
value_in = item_match.group('value') value_in = item_match.group('value')
value_out = '"' + value_in.replace('#', '%d') + '"' value_out = '"' + value_in.replace('#', '%d') + '"'
if quantity_out: if quantity_out:
lines.append(_PLURALS_ITEM_TEMPLATE % (quantity_out, value_out)) # only one line per quantity out (https://crbug.com/787488)
if quantity_out not in quantities_so_far:
quantities_so_far.add(quantity_out)
lines.append(_PLURALS_ITEM_TEMPLATE % (quantity_out, value_out))
else: else:
raise Exception('Unsupported plural quantity for android ' raise Exception('Unsupported plural quantity for android '
'strings.xml: %s' % quantity_in) 'strings.xml: %s' % quantity_in)
......
...@@ -80,6 +80,34 @@ a sledge hammer."</string> ...@@ -80,6 +80,34 @@ a sledge hammer."</string>
""" """
self.assertEqual(output.strip(), expected.strip()) self.assertEqual(output.strip(), expected.strip())
def testConflictingPlurals(self):
root = util.ParseGrdForUnittest(ur"""
<messages>
<message name="IDS_PLURALS" desc="A string using the ICU plural format">
{NUM_THINGS, plural,
=1 {Maybe I'll get one laser.}
one {Maybe I'll get one laser.}
other {Maybe I'll get # lasers.}}
</message>
</messages>
""")
buf = StringIO.StringIO()
build.RcBuilder.ProcessNode(root, DummyOutput('android', 'en'), buf)
output = buf.getvalue()
expected = ur"""
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<plurals name="plurals">
<item quantity="one">"Maybe I\'ll get one laser."</item>
<item quantity="other">"Maybe I\'ll get %d lasers."</item>
</plurals>
</resources>
"""
self.assertEqual(output.strip(), expected.strip())
def testTaggedOnly(self): def testTaggedOnly(self):
root = util.ParseGrdForUnittest(ur""" root = util.ParseGrdForUnittest(ur"""
<messages> <messages>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment