Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(252)

Unified Diff: grit/format/android_xml.py

Issue 1258833004: Compile plural strings to android <plurals> elem (Closed) Base URL: https://chromium.googlesource.com/external/grit-i18n.git@master
Patch Set: Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: grit/format/android_xml.py
diff --git a/grit/format/android_xml.py b/grit/format/android_xml.py
index d960bf49ddb8f009575b36f3a146cd25edbef8f8..6b844eb109c22451ddd519575ddb2968e96069b0 100644
--- a/grit/format/android_xml.py
+++ b/grit/format/android_xml.py
@@ -96,7 +96,14 @@ _SIMPLE_TEMPLATE = u'<string name="%s">%s</string>\n'
_PRODUCT_TEMPLATE = u'<string name="%s" product="%s">%s</string>\n'
+# Some strings have a plural equivalent
+_PLURALS_TEMPLATE = '<plurals name="%s">\n%s</plurals>\n'
+_PLURALS_ITEM_TEMPLATE = ' <item quantity="%s">%s</item>\n'
+
+
def Format(root, lang='en', output_dir='.'):
+ import sys
+ sys.stderr.write('outputting to %s\n' % output_dir)
newt (away) 2015/08/03 23:06:40 Looks like debugging code. Remove this?
conleyo 2015/08/04 00:27:52 Done.
yield ('<?xml version="1.0" encoding="utf-8"?>\n'
'<resources '
'xmlns:android="http://schemas.android.com/apk/res/android">\n')
@@ -131,10 +138,37 @@ def ShouldOutputNode(node, tagged_only):
(not tagged_only or _EMIT_TAG in node.formatter_data))
+def _LoadPluralMessage(message):
newt (away) 2015/08/03 23:06:40 Please add a docstring comment. This method is far
conleyo 2015/08/04 00:27:52 Done.
+ import re
newt (away) 2015/08/03 23:06:40 move import to top of file
conleyo 2015/08/04 00:27:52 Done.
+ ret = {}
+ plural_match = re.match(r'\{[A-Z_]+,\s*plural,(.*)\}$', message, re.S)
newt (away) 2015/08/03 23:06:40 Pre-compile these patterns, since they're used so
conleyo 2015/08/04 00:27:52 Done.
+ if not plural_match:
+ return None
+ body = plural_match.group(1).strip()
newt (away) 2015/08/03 23:06:40 To make this more readable, I'd suggest naming the
conleyo 2015/08/04 00:27:53 Done.
+ for item_match in re.finditer(r'(.*)\s*\{(.*?)\}', body):
newt (away) 2015/08/03 23:06:40 I'd replace the initial .* with \S+ since you want
conleyo 2015/08/04 00:27:52 Done.
+ quantity = item_match.group(1)
+ value = item_match.group(2).replace('#', '%d')
+ number_match = re.match(r'=(.*)', quantity)
newt (away) 2015/08/03 23:06:40 Explicit checks here would be better, e.g. if q
conleyo 2015/08/04 00:27:52 Done.
+ if number_match:
+ number = number_match.group(1).strip()
+ if number == '0':
+ ret['zero'] = value
+ elif number == '1':
+ ret['one'] = value
+ elif number == '2':
+ ret['two'] = value
+ else:
+ ret[number] = value
+ else:
+ ret['other'] = value
+ return ret
+
+
def _FormatMessage(item, lang):
"""Writes out a single string as a <resource/> element."""
value = item.ws_at_start + item.Translate(lang) + item.ws_at_end
+ plurals = _LoadPluralMessage(value)
newt (away) 2015/08/03 23:06:40 How about calling this _FormatPluralMessage() and
conleyo 2015/08/04 00:27:52 Done.
# Replace < > & with &lt; &gt; &amp; to ensure we generate valid XML and
# replace ' " with \' \" to conform to Android's string formatting rules.
value = xml.sax.saxutils.escape(value, {"'": "\\'", '"': '\\"'})
newt (away) 2015/08/03 23:06:40 these replacements need to happen even for plural
conleyo 2015/08/04 00:27:52 Done.
@@ -152,7 +186,13 @@ def _FormatMessage(item, lang):
product = item.formatter_data.get(_PRODUCT_TAG, product)
name = item.formatter_data.get(_NAME_TAG, name)
- if product:
+
+ if plurals:
+ items = ''
+ for quantity, item_value in plurals.items():
+ items += _PLURALS_ITEM_TEMPLATE % (quantity, item_value)
+ return _PLURALS_TEMPLATE % (name, items)
+ elif product:
return _PRODUCT_TEMPLATE % (name, product, value)
else:
return _SIMPLE_TEMPLATE % (name, value)
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698