| OLD | NEW |
| 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 """The 'grit android2grd' tool.""" | 6 """The 'grit android2grd' tool.""" |
| 7 | 7 |
| 8 | 8 |
| 9 import getopt | 9 import getopt |
| 10 import os.path | 10 import os.path |
| (...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 236 for child in resources.childNodes: | 236 for child in resources.childNodes: |
| 237 if child.nodeType == Node.COMMENT_NODE: | 237 if child.nodeType == Node.COMMENT_NODE: |
| 238 # Remove leading/trailing whitespace; collapse consecutive whitespaces. | 238 # Remove leading/trailing whitespace; collapse consecutive whitespaces. |
| 239 description = ' '.join(child.data.split()) | 239 description = ' '.join(child.data.split()) |
| 240 elif child.nodeType == Node.ELEMENT_NODE: | 240 elif child.nodeType == Node.ELEMENT_NODE: |
| 241 if child.tagName != 'string': | 241 if child.tagName != 'string': |
| 242 print 'Warning: ignoring unknown tag <%s>' % child.tagName | 242 print 'Warning: ignoring unknown tag <%s>' % child.tagName |
| 243 else: | 243 else: |
| 244 translatable = self.IsTranslatable(child) | 244 translatable = self.IsTranslatable(child) |
| 245 raw_name = child.getAttribute('name') | 245 raw_name = child.getAttribute('name') |
| 246 product = child.getAttribute('product') or None | 246 if not _STRING_NAME.match(raw_name): |
| 247 grd_name = self.__FormatName(raw_name, product) | 247 print 'Error: illegal string name: %s' % raw_name |
| 248 grd_name = 'IDS_' + raw_name.upper() |
| 248 # Transform the <string> node contents into a tclib.Message, taking | 249 # Transform the <string> node contents into a tclib.Message, taking |
| 249 # care to handle whitespace transformations and escaped characters, | 250 # care to handle whitespace transformations and escaped characters, |
| 250 # and coverting <xliff:g> placeholders into <ph> placeholders. | 251 # and coverting <xliff:g> placeholders into <ph> placeholders. |
| 251 msg = self.CreateTclibMessage(child) | 252 msg = self.CreateTclibMessage(child) |
| 252 msg_node = self.__CreateMessageNode(messages, grd_name, description, | 253 msg_node = self.__CreateMessageNode(messages, grd_name, description, |
| 253 msg, translatable) | 254 msg, translatable) |
| 254 messages.AddChild(msg_node) | 255 messages.AddChild(msg_node) |
| 255 # Reset the description once a message has been parsed. | 256 # Reset the description once a message has been parsed. |
| 256 description = '' | 257 description = '' |
| 257 | 258 |
| 258 def __FormatName(self, name, product=None): | |
| 259 """Formats the message name. | |
| 260 | |
| 261 Names in the strings.xml files should be lowercase with underscores. In grd | |
| 262 files message names should be mostly uppercase with a IDS prefix. We also | |
| 263 will annotate names with product information (lowercase) where appropriate. | |
| 264 | |
| 265 Args: | |
| 266 name: The message name as found in the string.xml file. | |
| 267 product: An optional product annotation. | |
| 268 | |
| 269 Returns: | |
| 270 String containing the grd style name that will be used in the translation | |
| 271 console. | |
| 272 """ | |
| 273 if not _STRING_NAME.match(name): | |
| 274 print 'Error: string name contains illegal characters: %s' % name | |
| 275 grd_name = 'IDS_%s' % name.upper() | |
| 276 product_suffix = ('_product_%s' % product.lower()) if product else '' | |
| 277 return grd_name + product_suffix | |
| 278 | |
| 279 def CreateTclibMessage(self, android_string): | 259 def CreateTclibMessage(self, android_string): |
| 280 """Transforms a <string/> element from strings.xml into a tclib.Message. | 260 """Transforms a <string/> element from strings.xml into a tclib.Message. |
| 281 | 261 |
| 282 Interprets whitespace, quotes, and escaped characters in the android_string | 262 Interprets whitespace, quotes, and escaped characters in the android_string |
| 283 according to Android's formatting and styling rules for strings. Also | 263 according to Android's formatting and styling rules for strings. Also |
| 284 converts <xliff:g> placeholders into <ph> placeholders, e.g.: | 264 converts <xliff:g> placeholders into <ph> placeholders, e.g.: |
| 285 | 265 |
| 286 <xliff:g id="website" example="google.com">%s</xliff:g> | 266 <xliff:g id="website" example="google.com">%s</xliff:g> |
| 287 becomes | 267 becomes |
| 288 <ph name="website"><ex>google.com</ex>%s</ph> | 268 <ph name="website"><ex>google.com</ex>%s</ph> |
| (...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 490 A <string> element is by default translatable unless otherwise marked. | 470 A <string> element is by default translatable unless otherwise marked. |
| 491 """ | 471 """ |
| 492 if android_string.hasAttribute('translatable'): | 472 if android_string.hasAttribute('translatable'): |
| 493 value = android_string.getAttribute('translatable').lower() | 473 value = android_string.getAttribute('translatable').lower() |
| 494 if value not in ('true', 'false'): | 474 if value not in ('true', 'false'): |
| 495 print 'Warning: translatable attribute has invalid value: %s' % value | 475 print 'Warning: translatable attribute has invalid value: %s' % value |
| 496 return value == 'true' | 476 return value == 'true' |
| 497 else: | 477 else: |
| 498 return True | 478 return True |
| 499 | 479 |
| OLD | NEW |