Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
|
kelvinp
2015/11/04 19:02:10
Will this affect other extensions in Chrome?
kelvinp
2015/11/04 19:09:04
Spoke to Jamie, looks like the UT passes so the ot
Jamie
2015/11/04 19:13:55
Yes. It will affect them all, but the net effect s
| |
| 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\n%s' |
| 23 ' }') | 23 ' }') |
| 24 placeholder_format = (' "%i": {\n' | |
|
kelvinp
2015/11/04 19:02:10
According to the documentation, the placeholder is
kelvinp
2015/11/04 19:09:04
Spoke with Jamie offline. We want to minimize tex
Jamie
2015/11/04 19:13:55
It's only optional insofar as you don't need it fo
| |
| 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. | |
| 39 placeholders = '' | |
| 40 for i in range(1, 10): | |
|
kelvinp
2015/11/04 19:02:10
Is 10 the maximum number of placeholders?
Nevermin
kelvinp
2015/11/04 19:09:04
May be good to include the link about there can on
Jamie
2015/11/04 19:13:55
Acknowledged.
| |
| 41 if loc_message.find('$%d' % i) == -1: | |
| 42 break | |
| 43 loc_message = loc_message.replace('$%d' % i, '$%d$' % i) | |
| 44 if placeholders: | |
| 45 placeholders += ',\n' | |
| 46 placeholders += placeholder_format % (i, i) | |
| 47 | |
| 34 if not first: | 48 if not first: |
| 35 yield ',\n' | 49 yield ',\n' |
| 36 first = False | 50 first = False |
| 37 yield format % (id, loc_message) | 51 |
| 52 if placeholders: | |
| 53 placeholders = ',\n "placeholders": {\n%s\n }' % placeholders | |
| 54 yield format % (id, loc_message, placeholders) | |
| 38 | 55 |
| 39 yield '\n}\n' | 56 yield '\n}\n' |
| OLD | NEW |