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