OLD | NEW |
1 #!/usr/bin/env python | |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 3 # found in the LICENSE file. |
5 | 4 |
6 """Formats as a .json file that can be used to localize Google Chrome | 5 """Formats as a .json file that can be used to localize Google Chrome |
7 extensions.""" | 6 extensions.""" |
8 | 7 |
9 from json import JSONEncoder | 8 from json import JSONEncoder |
10 import re | 9 import re |
11 import types | 10 import types |
12 | 11 |
| 12 from grit import constants |
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%s\n' | 22 ' "message": %s%s\n' |
23 ' }') | 23 ' }') |
24 placeholder_format = (' "%i": {\n' | 24 placeholder_format = (' "%i": {\n' |
25 ' "content": "$%i"\n' | 25 ' "content": "$%i"\n' |
26 ' }') | 26 ' }') |
27 first = True | 27 first = True |
28 for child in root.ActiveDescendants(): | 28 for child in root.ActiveDescendants(): |
29 if isinstance(child, message.MessageNode): | 29 if isinstance(child, message.MessageNode): |
30 id = child.attrs['name'] | 30 id = child.attrs['name'] |
31 if id.startswith('IDR_') or id.startswith('IDS_'): | 31 if id.startswith('IDR_') or id.startswith('IDS_'): |
32 id = id[4:] | 32 id = id[4:] |
33 | 33 |
| 34 translation_missing = child.GetCliques()[0].clique.get(lang) is None; |
| 35 if (child.ShouldFallbackToEnglish() and translation_missing and |
| 36 lang != constants.FAKE_BIDI): |
| 37 # Skip the string if it's not translated. Chrome will fallback |
| 38 # to English automatically. |
| 39 continue |
| 40 |
34 loc_message = encoder.encode(child.ws_at_start + child.Translate(lang) + | 41 loc_message = encoder.encode(child.ws_at_start + child.Translate(lang) + |
35 child.ws_at_end) | 42 child.ws_at_end) |
36 | 43 |
37 # Replace $n place-holders with $n$ and add an appropriate "placeholders" | 44 # Replace $n place-holders with $n$ and add an appropriate "placeholders" |
38 # entry. Note that chrome.i18n.getMessage only supports 9 placeholders: | 45 # entry. Note that chrome.i18n.getMessage only supports 9 placeholders: |
39 # https://developer.chrome.com/extensions/i18n#method-getMessage | 46 # https://developer.chrome.com/extensions/i18n#method-getMessage |
40 placeholders = '' | 47 placeholders = '' |
41 for i in range(1, 10): | 48 for i in range(1, 10): |
42 if loc_message.find('$%d' % i) == -1: | 49 if loc_message.find('$%d' % i) == -1: |
43 break | 50 break |
44 loc_message = loc_message.replace('$%d' % i, '$%d$' % i) | 51 loc_message = loc_message.replace('$%d' % i, '$%d$' % i) |
45 if placeholders: | 52 if placeholders: |
46 placeholders += ',\n' | 53 placeholders += ',\n' |
47 placeholders += placeholder_format % (i, i) | 54 placeholders += placeholder_format % (i, i) |
48 | 55 |
49 if not first: | 56 if not first: |
50 yield ',\n' | 57 yield ',\n' |
51 first = False | 58 first = False |
52 | 59 |
53 if placeholders: | 60 if placeholders: |
54 placeholders = ',\n "placeholders": {\n%s\n }' % placeholders | 61 placeholders = ',\n "placeholders": {\n%s\n }' % placeholders |
55 yield format % (id, loc_message, placeholders) | 62 yield format % (id, loc_message, placeholders) |
56 | 63 |
57 yield '\n}\n' | 64 yield '\n}\n' |
OLD | NEW |