[grit-i18n] r202 committed - Fix placeholder syntax for Chrome extensions....

1 view
Skip to first unread message

grit...@googlecode.com

unread,
Nov 4, 2015, 4:31:07 PM11/4/15
to grit-de...@googlegroups.com
Revision: 202
Author: tha...@chromium.org
Date: Wed Nov 4 21:30:55 2015 UTC
Log: Fix placeholder syntax for Chrome extensions.

This changes the output format for placeholders in Chrome extensions from $n
(which is ambiguous in some circumstances) to $var$. For simplicity (and to
keep the file size small), |var| is always |n|; in other words, we use
placeholders named $1$, $2$, etc., regardless of the name given in the GRD
file.

BUG=551100
Review URL: https://codereview.chromium.org/1425693007/

Patch from Jamie Walch <jamie...@chromium.org>!

https://code.google.com/p/grit-i18n/source/detail?r=202

Modified:
/trunk/grit/format/chrome_messages_json.py
/trunk/grit/format/chrome_messages_json_unittest.py

=======================================
--- /trunk/grit/format/chrome_messages_json.py Fri Apr 18 18:10:32 2014 UTC
+++ /trunk/grit/format/chrome_messages_json.py Wed Nov 4 21:30:55 2015 UTC
@@ -19,8 +19,11 @@

encoder = JSONEncoder();
format = (' "%s": {\n'
- ' "message": %s\n'
+ ' "message": %s%s\n'
' }')
+ placeholder_format = (' "%i": {\n'
+ ' "content": "$%i"\n'
+ ' }')
first = True
for child in root.ActiveDescendants():
if isinstance(child, message.MessageNode):
@@ -31,9 +34,24 @@
loc_message = encoder.encode(child.ws_at_start +
child.Translate(lang) +
child.ws_at_end)

+ # Replace $n place-holders with $n$ and add an
appropriate "placeholders"
+ # entry. Note that chrome.i18n.getMessage only supports 9
placeholders:
+ # https://developer.chrome.com/extensions/i18n#method-getMessage
+ placeholders = ''
+ for i in range(1, 10):
+ if loc_message.find('$%d' % i) == -1:
+ break
+ loc_message = loc_message.replace('$%d' % i, '$%d$' % i)
+ if placeholders:
+ placeholders += ',\n'
+ placeholders += placeholder_format % (i, i)
+
if not first:
yield ',\n'
first = False
- yield format % (id, loc_message)
+
+ if placeholders:
+ placeholders = ',\n "placeholders": {\n%s\n }' % placeholders
+ yield format % (id, loc_message, placeholders)

yield '\n}\n'
=======================================
--- /trunk/grit/format/chrome_messages_json_unittest.py Fri Apr 18 18:10:32
2014 UTC
+++ /trunk/grit/format/chrome_messages_json_unittest.py Wed Nov 4 21:30:55
2015 UTC
@@ -32,6 +32,9 @@
<message name="IDS_PLACEHOLDERS">
<ph name="ERROR_COUNT">%1$d<ex>1</ex></ph> error, <ph
name="WARNING_COUNT">%2$d<ex>1</ex></ph> warning
</message>
+ <message name="IDS_PLACEHOLDERS_SUBSTITUTED_BY_GETMESSAGE">
+ <ph name="BEGIN">$1<ex>a</ex></ph>test<ph
name="END">$2<ex>b</ex></ph>
+ </message>
<message name="IDS_STARTS_WITH_SPACE">
''' (<ph name="COUNT">%d<ex>2</ex></ph>)
</message>
@@ -64,6 +67,17 @@
"PLACEHOLDERS": {
"message": "%1$d error, %2$d warning"
},
+ "PLACEHOLDERS_SUBSTITUTED_BY_GETMESSAGE": {
+ "message": "$1$test$2$",
+ "placeholders": {
+ "1": {
+ "content": "$1"
+ },
+ "2": {
+ "content": "$2"
+ }
+ }
+ },
"STARTS_WITH_SPACE": {
"message": " (%d)"
},
Reply all
Reply to author
Forward
0 new messages