Package translate :: Package convert :: Module po2json

[hide private]
[frames] | no frames]

# Source Code for Module translate.convert.po2json

     1  #!/usr/bin/env python
     2  # -*- coding: utf-8 -*-
     3  #
     4  # Copyright 2002-2006 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
    23  """Convert Gettext PO localization files to JSON files"""
    24
    25  from translate.storage import factory
    26
    27
    28 -class rejson:
    29
    30 -    def __init__(self, templatefile, inputstore):
    31          from translate.storage import jsonl10n
    32          self.templatefile = templatefile
    33          self.templatestore = jsonl10n.JsonFile(templatefile)
    34          self.inputstore = inputstore
    35
    36 -    def convertstore(self, includefuzzy=False):
    37          self.includefuzzy = includefuzzy
    38          self.inputstore.makeindex()
    39          for unit in self.templatestore.units:
    40              inputunit = self.inputstore.locationindex.get(unit.getid())
    41              if inputunit is not None:
    42                  if inputunit.isfuzzy() and not self.includefuzzy:
    43                      unit.target = unit.source
    44                  else:
    45                      unit.target = inputunit.target
    46              else:
    47                  unit.target = unit.source
    48          return str(self.templatestore)
    49
    50
    51 -def convertjson(inputfile, outputfile, templatefile, includefuzzy=False):
    52      inputstore = factory.getobject(inputfile)
    53      if templatefile is None:
    54          raise ValueError("Must have template file for JSON files")
    55      else:
    56          convertor = rejson(templatefile, inputstore)
    57      outputstring = convertor.convertstore(includefuzzy)
    58      outputfile.write(outputstring)
    59      return 1
    60
    61
    62 -def main(argv=None):
    63      # handle command line options
    64      from translate.convert import convert
    65      formats = {
    66                 ("po", "json"): ("json", convertjson),
    67                }
    68      parser = convert.ConvertOptionParser(formats, usetemplates=True,
    69                                           description=__doc__)
    70      parser.add_fuzzy_option()
    71      parser.run(argv)
    72
    73
    74  if __name__ == '__main__':
    75      main()
    76