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

Side by Side Diff: grit/format/chrome_messages_json.py

Issue 1425693007: Fix placeholder syntax for Chrome extensions. (Closed) Base URL: https://chromium.googlesource.com/external/grit-i18n.git@master
Patch Set: Added unit test. Created 5 years, 1 month 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 unified diff | Download patch
« no previous file with comments | « no previous file | grit/format/chrome_messages_json_unittest.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be 3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file. 4 # found in the LICENSE file.
5 5
6 """Formats as a .json file that can be used to localize Google Chrome 6 """Formats as a .json file that can be used to localize Google Chrome
7 extensions.""" 7 extensions."""
8 8
9 from json import JSONEncoder 9 from json import JSONEncoder
10 import re 10 import re
11 import types 11 import types
12 12
13 from grit import util 13 from grit import util
14 from grit.node import message 14 from grit.node import message
15 15
16 def Format(root, lang='en', output_dir='.'): 16 def Format(root, lang='en', output_dir='.'):
17 """Format the messages as JSON.""" 17 """Format the messages as JSON."""
18 yield '{\n' 18 yield '{\n'
19 19
20 encoder = JSONEncoder(); 20 encoder = JSONEncoder();
21 format = (' "%s": {\n' 21 format = (' "%s": {\n'
22 ' "message": %s\n' 22 ' "message": %s%s\n'
23 ' }') 23 ' }')
24 placeholder_format = (' "%i": {\n'
25 ' "content": "$%i"\n'
26 ' }')
24 first = True 27 first = True
25 for child in root.ActiveDescendants(): 28 for child in root.ActiveDescendants():
26 if isinstance(child, message.MessageNode): 29 if isinstance(child, message.MessageNode):
27 id = child.attrs['name'] 30 id = child.attrs['name']
28 if id.startswith('IDR_') or id.startswith('IDS_'): 31 if id.startswith('IDR_') or id.startswith('IDS_'):
29 id = id[4:] 32 id = id[4:]
30 33
31 loc_message = encoder.encode(child.ws_at_start + child.Translate(lang) + 34 loc_message = encoder.encode(child.ws_at_start + child.Translate(lang) +
32 child.ws_at_end) 35 child.ws_at_end)
33 36
37 # Replace $n place-holders with $n$ and add an appropriate "placeholders"
38 # entry. Note that chrome.i18n.getMessage only supports 9 placeholders:
39 # https://developer.chrome.com/extensions/i18n#method-getMessage
40 placeholders = ''
41 for i in range(1, 10):
42 if loc_message.find('$%d' % i) == -1:
43 break
44 loc_message = loc_message.replace('$%d' % i, '$%d$' % i)
45 if placeholders:
46 placeholders += ',\n'
47 placeholders += placeholder_format % (i, i)
48
34 if not first: 49 if not first:
35 yield ',\n' 50 yield ',\n'
36 first = False 51 first = False
37 yield format % (id, loc_message) 52
53 if placeholders:
54 placeholders = ',\n "placeholders": {\n%s\n }' % placeholders
55 yield format % (id, loc_message, placeholders)
38 56
39 yield '\n}\n' 57 yield '\n}\n'
OLDNEW
« no previous file with comments | « no previous file | grit/format/chrome_messages_json_unittest.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698