js_to_json.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #!/usr/bin/python
  2. # Gives the translation status of the specified apps and languages.
  3. #
  4. # Copyright 2013 Google Inc.
  5. # https://developers.google.com/blockly/
  6. #
  7. # Licensed under the Apache License, Version 2.0 (the "License");
  8. # you may not use this file except in compliance with the License.
  9. # You may obtain a copy of the License at
  10. #
  11. # http://www.apache.org/licenses/LICENSE-2.0
  12. #
  13. # Unless required by applicable law or agreed to in writing, software
  14. # distributed under the License is distributed on an "AS IS" BASIS,
  15. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. # See the License for the specific language governing permissions and
  17. # limitations under the License.
  18. """Extracts messages from .js files into .json files for translation.
  19. Specifically, lines with the following formats are extracted:
  20. /// Here is a description of the following message.
  21. Blockly.SOME_KEY = 'Some value';
  22. Adjacent "///" lines are concatenated.
  23. There are two output files, each of which is proper JSON. For each key, the
  24. file en.json would get an entry of the form:
  25. "Blockly.SOME_KEY", "Some value",
  26. The file qqq.json would get:
  27. "Blockly.SOME_KEY", "Here is a description of the following message.",
  28. Commas would of course be omitted for the final entry of each value.
  29. @author Ellen Spertus (ellen.spertus@gmail.com)
  30. """
  31. import argparse
  32. import codecs
  33. import json
  34. import os
  35. import re
  36. from common import write_files
  37. _INPUT_DEF_PATTERN = re.compile("""Blockly.Msg.(\w*)\s*=\s*'([^']*)';?$""")
  38. _INPUT_SYN_PATTERN = re.compile(
  39. """Blockly.Msg.(\w*)\s*=\s*Blockly.Msg.(\w*);""")
  40. def main():
  41. # Set up argument parser.
  42. parser = argparse.ArgumentParser(description='Create translation files.')
  43. parser.add_argument(
  44. '--author',
  45. default='Ellen Spertus <ellen.spertus@gmail.com>',
  46. help='name and email address of contact for translators')
  47. parser.add_argument('--lang', default='en',
  48. help='ISO 639-1 source language code')
  49. parser.add_argument('--output_dir', default='json',
  50. help='relative directory for output files')
  51. parser.add_argument('--input_file', default='messages.js',
  52. help='input file')
  53. parser.add_argument('--quiet', action='store_true', default=False,
  54. help='only display warnings, not routine info')
  55. args = parser.parse_args()
  56. if (not args.output_dir.endswith(os.path.sep)):
  57. args.output_dir += os.path.sep
  58. # Read and parse input file.
  59. results = []
  60. synonyms = {}
  61. description = ''
  62. infile = codecs.open(args.input_file, 'r', 'utf-8')
  63. for line in infile:
  64. if line.startswith('///'):
  65. if description:
  66. description = description + ' ' + line[3:].strip()
  67. else:
  68. description = line[3:].strip()
  69. else:
  70. match = _INPUT_DEF_PATTERN.match(line)
  71. if match:
  72. result = {}
  73. result['meaning'] = match.group(1)
  74. result['source'] = match.group(2)
  75. if not description:
  76. print('Warning: No description for ' + result['meaning'])
  77. result['description'] = description
  78. description = ''
  79. results.append(result)
  80. else:
  81. match = _INPUT_SYN_PATTERN.match(line)
  82. if match:
  83. if description:
  84. print('Warning: Description preceding definition of synonym {0}.'.
  85. format(match.group(1)))
  86. description = ''
  87. synonyms[match.group(1)] = match.group(2)
  88. infile.close()
  89. # Create <lang_file>.json, keys.json, and qqq.json.
  90. write_files(args.author, args.lang, args.output_dir, results, False)
  91. # Create synonyms.json.
  92. synonym_file_name = os.path.join(os.curdir, args.output_dir, 'synonyms.json')
  93. with open(synonym_file_name, 'w') as outfile:
  94. json.dump(synonyms, outfile)
  95. if not args.quiet:
  96. print("Wrote {0} synonym pairs to {1}.".format(
  97. len(synonyms), synonym_file_name))
  98. if __name__ == '__main__':
  99. main()