js_to_json.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. Blockscad.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. "Blockscad.SOME_KEY", "Some value",
  26. The file qqq.json would get:
  27. "Blockscad.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. modified by J. Yoder for Blockscad strings
  31. """
  32. import argparse
  33. import codecs
  34. import json
  35. import os
  36. import re
  37. from common import write_files
  38. _INPUT_DEF_PATTERN = re.compile("""Blockscad.Msg.(\w*)\s*=\s*'(.*)';""")
  39. _INPUT_SYN_PATTERN = re.compile(
  40. """Blockscad.Msg.(\w*)\s*=\s*Blockscad.Msg.(\w*);""")
  41. def main():
  42. # Set up argument parser.
  43. parser = argparse.ArgumentParser(description='Create translation files.')
  44. parser.add_argument(
  45. '--author',
  46. default='Jennie Yoder <jennie@einsteinsworkshop.com>',
  47. help='name and email address of contact for translators')
  48. parser.add_argument('--lang', default='en',
  49. help='ISO 639-1 source language code')
  50. parser.add_argument('--output_dir', default='json',
  51. help='relative directory for output files')
  52. parser.add_argument('--input_file', default='messages.js',
  53. help='input file')
  54. parser.add_argument('--quiet', action='store_true', default=False,
  55. help='only display warnings, not routine info')
  56. args = parser.parse_args()
  57. if (not args.output_dir.endswith(os.path.sep)):
  58. args.output_dir += os.path.sep
  59. # Read and parse input file.
  60. results = []
  61. synonyms = {}
  62. description = ''
  63. infile = codecs.open(args.input_file, 'r', 'utf-8')
  64. for line in infile:
  65. if line.startswith('///'):
  66. if description:
  67. description = description + ' ' + line[3:].strip()
  68. else:
  69. description = line[3:].strip()
  70. # print(description)
  71. else:
  72. match = _INPUT_DEF_PATTERN.match(line)
  73. # print(match)
  74. if match:
  75. # print('Found a match')
  76. result = {}
  77. result['meaning'] = match.group(1)
  78. result['source'] = match.group(2)
  79. if not description:
  80. print('Warning: No description for ' + result['meaning'])
  81. result['description'] = description
  82. description = ''
  83. results.append(result)
  84. else:
  85. match = _INPUT_SYN_PATTERN.match(line)
  86. if match:
  87. if description:
  88. print('Warning: Description preceding definition of synonym {0}.'.
  89. format(match.group(1)))
  90. description = ''
  91. synonyms[match.group(1)] = match.group(2)
  92. infile.close()
  93. # Create <lang_file>.json, keys.json, and qqq.json.
  94. write_files(args.author, args.lang, args.output_dir, results, False)
  95. # Create synonyms.json.
  96. synonym_file_name = os.path.join(os.curdir, args.output_dir, 'synonyms.json')
  97. with open(synonym_file_name, 'w') as outfile:
  98. json.dump(synonyms, outfile)
  99. if not args.quiet:
  100. print("Wrote {0} synonym pairs to {1}.".format(
  101. len(synonyms), synonym_file_name))
  102. if __name__ == '__main__':
  103. main()