[grit-i18n] r195 committed - Remove support for product attributes in Android xml files....

2 views
Skip to first unread message

grit...@googlecode.com

unread,
Aug 13, 2015, 8:23:33 PM8/13/15
to grit-de...@googlegroups.com
Revision: 195
Author: ne...@chromium.org
Date: Thu Aug 13 23:11:13 2015 UTC
Log: Remove support for product attributes in Android xml files.

The product attribute was never used, except in one place in Chrome that
wasn't actually needed (see https://codereview.chromium.org/1288243002).
Further, the product attribute has no compelling use case in typical
development and isn't even documented on developer.android.com (See:
http://developer.android.com/guide/topics/resources/string-resource.html)

This also updates the header comment in android_xml.py to explain the
new <plurals> feature added in https://codereview.chromium.org/1258833004

R=tha...@chromium.org

Review URL: https://codereview.chromium.org/1291173002 .
https://code.google.com/p/grit-i18n/source/detail?r=195

Modified:
/trunk/grit/format/android_xml.py
/trunk/grit/format/android_xml_unittest.py
/trunk/grit/testdata/android.xml
/trunk/grit/tool/android2grd.py
/trunk/grit/tool/android2grd_unittest.py

=======================================
--- /trunk/grit/format/android_xml.py Fri Aug 7 23:35:40 2015 UTC
+++ /trunk/grit/format/android_xml.py Thu Aug 13 23:11:13 2015 UTC
@@ -42,19 +42,22 @@

<message name="IDS_HELLO" formatter_data="android_java">Hello</message>

-To specify the product attribute to be added to a <string> element, add
-"android_java_product" to formatter_data. "android_java_name" can be used
to
-override the name in the <string> element. For example,
+To generate Android plurals (aka "quantity strings"), use the ICU plural
syntax
+in the grd file. This will automatically be transformed into a <purals>
element
+in the output xml file. For example:

- <message name="IDS_FOO_NOSDCARD"
formatter_data="android_java_product=nosdcard
- android_java_name=foo">no card</message>
- <message name="IDS_FOO_DEFAULT"
formatter_data="android_java_product=default
- android_java_name=foo">has card</message>
+ <message name="IDS_CATS">
+ {NUM_CATS, plural,
+ =1 {1 cat}
+ other {# cats}}
+ </message>

-would generate
+ will produce

- <string name="foo" product="nosdcard">"no card"</string>
- <string name="foo" product="default">"has card"</string>
+ <plurals name="cats">
+ <item quantity="one">1 Katze</item>
+ <item quantity="other">%d Katzen</item>
+ </plurals>
"""

import os
@@ -74,30 +77,13 @@
# In tagged-only mode, only messages with this tag will be ouputted.
_EMIT_TAG = 'android_java'

-# This tag controls the product attribute of the generated <string>
element.
-_PRODUCT_TAG = 'android_java_product'
+_NAME_PATTERN = lazy_re.compile('IDS_(?P<name>[A-Z0-9_]+)\Z')

-# This tag controls the name attribute of the generated <string> element.
-_NAME_TAG = 'android_java_name'
+# Most strings are output as a <string> element. Note the double quotes
+# around the value to preserve whitespace.
+_STRING_TEMPLATE = u'<string name="%s">"%s"</string>\n'

-# The Android resource name and optional product information are placed
-# in the grd string name because grd doesn't know about Android product
-# information.
-# TODO(newt): Don't allow product information in mangled names, since it
can now
-# be specified using "android_java_product" in formatter_data.
-_NAME_PATTERN = lazy_re.compile(
- 'IDS_(?P<name>[A-Z0-9_]+)(_product_(?P<product>[a-z]+))?\Z')
-
-
-# In most cases we only need a name attribute and string value.
-_SIMPLE_TEMPLATE = u'<string name="%s">%s</string>\n'
-
-
-# In a few cases a product attribute is needed.
-_PRODUCT_TEMPLATE = u'<string name="%s" product="%s">%s</string>\n'
-
-
-# Some strings have a plural equivalent
+# Some strings are output as a <plurals> element.
_PLURALS_TEMPLATE = '<plurals name="%s">\n%s</plurals>\n'
_PLURALS_ITEM_TEMPLATE = ' <item quantity="%s">%s</item>\n'
_PLURALS_PATTERN =
lazy_re.compile(r'\{[A-Z_]+,\s*plural,(?P<items>.*)\}$', flags=re.S)
@@ -196,28 +182,19 @@
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
- # 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, {"'": "\\'", '"': '\\"'})
- plurals = _FormatPluralMessage(value)
- # Wrap the string in double quotes to preserve whitespace.
- value = '"' + value + '"'
-
mangled_name = item.GetTextualIds()[0]
match = _NAME_PATTERN.match(mangled_name)
if not match:
raise Exception('Unexpected resource name: %s' % mangled_name)
name = match.group('name').lower()
- product = match.group('product')

- # Override product or name with values in formatter_data, if any.
- product = item.formatter_data.get(_PRODUCT_TAG, product)
- name = item.formatter_data.get(_NAME_TAG, name)
+ value = item.ws_at_start + item.Translate(lang) + item.ws_at_end
+ # 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, {"'": "\\'", '"': '\\"'})

+ plurals = _FormatPluralMessage(value)
if plurals:
return _PLURALS_TEMPLATE % (name, plurals)
- elif product:
- return _PRODUCT_TEMPLATE % (name, product, value)
else:
- return _SIMPLE_TEMPLATE % (name, value)
+ return _STRING_TEMPLATE % (name, value)
=======================================
--- /trunk/grit/format/android_xml_unittest.py Fri Aug 7 23:35:40 2015 UTC
+++ /trunk/grit/format/android_xml_unittest.py Thu Aug 13 23:11:13 2015 UTC
@@ -39,14 +39,6 @@
<message name="IDS_WHITESPACE" desc="A string with extra
whitespace.">
''' How old fashioned -- she thought. '''
</message>
- <message name="IDS_PRODUCT_SPECIFIC_product_nosdcard"
- desc="A string that only applies if there's no sdcard">
- Lasers will probably be helpful.
- </message>
- <message name="IDS_PRODUCT_DEFAULT" desc="New style product tag"
- formatter_data="android_java_product=default
android_java_name=custom_name">
- You have an SD card
- </message>
<message name="IDS_PLACEHOLDERS" desc="A string with
placeholders">
I'll buy a <ph name="WAVELENGTH">%d<ex>200</ex></ph> nm laser
at <ph name="STORE_NAME">%s<ex>the grocery store</ex></ph>.
</message>
@@ -71,8 +63,6 @@
wood, charcoal, and
a sledge hammer."</string>
<string name="whitespace">" How old fashioned -- she
thought. "</string>
-<string name="product_specific" product="nosdcard">"Lasers will probably
be helpful."</string>
-<string name="custom_name" product="default">"You have an SD card"</string>
<string name="placeholders">"I\'ll buy a %d nm laser at %s."</string>
<plurals name="plurals">
<item quantity="one">"Maybe I\'ll get one laser."</item>
=======================================
--- /trunk/grit/testdata/android.xml Wed Feb 13 17:36:29 2013 UTC
+++ /trunk/grit/testdata/android.xml Thu Aug 13 23:11:13 2015 UTC
@@ -11,7 +11,7 @@
</string>

<!-- A simple string. -->
- <string name="simple" product="nosdcard">A simple string.</string>
+ <string name="simple">A simple string.</string>

<!-- A string with a comment. -->
<string name="comment">Contains a <!-- ignore this --> comment. </string>
=======================================
--- /trunk/grit/tool/android2grd.py Tue Jan 7 15:08:36 2014 UTC
+++ /trunk/grit/tool/android2grd.py Thu Aug 13 23:11:13 2015 UTC
@@ -243,8 +243,9 @@
else:
translatable = self.IsTranslatable(child)
raw_name = child.getAttribute('name')
- product = child.getAttribute('product') or None
- grd_name = self.__FormatName(raw_name, product)
+ if not _STRING_NAME.match(raw_name):
+ print 'Error: illegal string name: %s' % raw_name
+ grd_name = 'IDS_' + raw_name.upper()
# Transform the <string> node contents into a tclib.Message,
taking
# care to handle whitespace transformations and escaped
characters,
# and coverting <xliff:g> placeholders into <ph> placeholders.
@@ -255,27 +256,6 @@
# Reset the description once a message has been parsed.
description = ''

- def __FormatName(self, name, product=None):
- """Formats the message name.
-
- Names in the strings.xml files should be lowercase with underscores.
In grd
- files message names should be mostly uppercase with a IDS prefix. We
also
- will annotate names with product information (lowercase) where
appropriate.
-
- Args:
- name: The message name as found in the string.xml file.
- product: An optional product annotation.
-
- Returns:
- String containing the grd style name that will be used in the
translation
- console.
- """
- if not _STRING_NAME.match(name):
- print 'Error: string name contains illegal characters: %s' % name
- grd_name = 'IDS_%s' % name.upper()
- product_suffix = ('_product_%s' % product.lower()) if product else ''
- return grd_name + product_suffix
-
def CreateTclibMessage(self, android_string):
"""Transforms a <string/> element from strings.xml into a
tclib.Message.

=======================================
--- /trunk/grit/tool/android2grd_unittest.py Wed Apr 17 11:01:19 2013 UTC
+++ /trunk/grit/tool/android2grd_unittest.py Thu Aug 13 23:11:13 2015 UTC
@@ -113,14 +113,6 @@
self.assertTrue(msg.IsTranslateable())
self.assertEqual(msg.attrs["desc"], "A string with placeholder.")

- def testProductAttribute(self):
- grd = self.__ParseAndroidXml([])
- messages = grd.GetChildrenOfType(message.MessageNode)
- msg = filter(lambda x: x.GetTextualIds()[0] ==
- "IDS_SIMPLE_product_nosdcard",
- messages)
- self.assertTrue(msg)
-
def testTranslatableAttribute(self):
grd = self.__ParseAndroidXml([])
messages = grd.GetChildrenOfType(message.MessageNode)
Reply all
Reply to author
Forward
0 new messages