translate.convert.json2po

Package translate :: Package convert :: Module json2po |
[hide private]
[frames] | no frames]

# Source Code for Module translate.convert.json2po

      1  #!/usr/bin/env python
      2  # -*- coding: utf-8 -*-
      3  #
      4  # Copyright 2007, 2010 Zuza Software Foundation
      5  #
      6  # This file is part of translate.
      7  #
      8  # translate is free software; you can redistribute it and/or modify
      9  # it under the terms of the GNU General Public License as published by
     10  # the Free Software Foundation; either version 2 of the License, or
     11  # (at your option) any later version.
     12  #
     13  # translate is distributed in the hope that it will be useful,
     14  # but WITHOUT ANY WARRANTY; without even the implied warranty of
     15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     16  # GNU General Public License for more details.
     17  #
     18  # You should have received a copy of the GNU General Public License
     19  # along with translate; if not, write to the Free Software
     20  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
     21
     22  """Convert JSON files to Gettext PO localization files"""
     23
     24  import sys
     25
     26  from translate.storage import po
     27
     28
     29 -class json2po:
     30      """Convert a JSON file to a PO file"""
     31
     32 -    def convert_store(self, input_store, duplicatestyle="msgctxt"):
     33          """Converts a JSON file to a PO file"""
     34          output_store = po.pofile()
     35          output_header = output_store.init_headers(charset="UTF-8",
     36                                                    encoding="8bit")
     37          output_header.addnote("extracted from %s" % input_store.filename,
     38                                "developer")
     39          for input_unit in input_store.units:
     40              output_unit = self.convert_unit(input_unit, "developer")
     41              if output_unit is not None:
     42                  output_store.addunit(output_unit)
     43          output_store.removeduplicates(duplicatestyle)
     44          return output_store
     45
     46 -    def merge_store(self, template_store, input_store, blankmsgstr=False,
     47                      duplicatestyle="msgctxt"):
     48          """Converts two JSON files to a PO file"""
     49          output_store = po.pofile()
     50          output_header = output_store.init_headers(charset="UTF-8",
     51                                                    encoding="8bit")
     52          output_header.addnote("extracted from %s, %s" % (template_store.filename,
     53                                                           input_store.filename),
     54                                "developer")
     55
     56          input_store.makeindex()
     57          for template_unit in template_store.units:
     58              origpo = self.convert_unit(template_unit, "developer")
     59              # try and find a translation of the same name...
     60              template_unit_name = "".join(template_unit.getlocations())
     61              if template_unit_name in input_store.locationindex:
     62                  translatedjson = input_store.locationindex[template_unit_name]
     63                  translatedpo = self.convert_unit(translatedjson, "translator")
     64              else:
     65                  translatedpo = None
     66              # if we have a valid po unit, get the translation and add it...
     67              if origpo is not None:
     68                  if translatedpo is not None and not blankmsgstr:
     69                      origpo.target = translatedpo.source
     70                  output_store.addunit(origpo)
     71              elif translatedpo is not None:
     72                  print >> sys.stderr, "Error converting original JSON definition %s" % origpo.name
     73          output_store.removeduplicates(duplicatestyle)
     74          return output_store
     75
     76 -    def convert_unit(self, input_unit, commenttype):
     77          """Converts a JSON unit to a PO unit
     78
     79          @return: None if empty or not for translation
     80          """
     81          if input_unit is None:
     82              return None
     83          # escape unicode
     84          output_unit = po.pounit(encoding="UTF-8")
     85          output_unit.addlocation(input_unit.getid())
     86          output_unit.source = input_unit.source
     87          output_unit.target = ""
     88          return output_unit
     89
     90
     91 -def convertjson(input_file, output_file, template_file, pot=False,
     92                  duplicatestyle="msgctxt", dialect="default", filter=None):
     93      """Reads in L{input_file} using jsonl10n, converts using L{json2po},
     94      writes to L{output_file}"""
     95      from translate.storage import jsonl10n
     96      if filter is not None:
     97          filter = filter.split(',')
     98      input_store = jsonl10n.JsonFile(input_file, filter=filter)
     99      convertor = json2po()
    100      if template_file is None:
    101          output_store = convertor.convert_store(input_store,
    102                                                 duplicatestyle=duplicatestyle)
    103      else:
    104          template_store = jsonl10n.JsonFile(template_file, dialect=dialect)
    105          output_store = convertor.merge_store(template_store, input_store,
    106                                               blankmsgstr=pot,
    107                                               duplicatestyle=duplicatestyle)
    108      if output_store.isempty():
    109          return 0
    110      output_file.write(str(output_store))
    111      return 1
    112
    113
    114 -def main(argv=None):
    115      from translate.convert import convert
    116      formats = {
    117                 "json": ("po", convertjson),
    118                 ("json", "json"): ("po", convertjson),
    119                }
    120      parser = convert.ConvertOptionParser(formats, usetemplates=True,
    121                                           usepots=True, description=__doc__)
    122      parser.add_option("", "--filter", dest="filter", default=None,
    123              help="leaves to extract e.g. 'name,desc': (default: extract everything)",
    124              metavar="FILTER")
    125      parser.add_duplicates_option()
    126      parser.passthrough.append("pot")
    127      parser.passthrough.append("filter")
    128      parser.run(argv)
    129
    130
    131  if __name__ == '__main__':
    132      main()
    133