translate.convert.po2mozlang

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

# Source Code for Module translate.convert.po2mozlang

     1  #!/usr/bin/env python
     2  # -*- coding: utf-8 -*-
     3  #
     4  # Copyright 2008,2011 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  # Original Author: Dan Schafer <dschafer@mozilla.com>
    23  # Date: 10 Jun 2008
    24
    25  """convert Gettext PO localization files to Mozilla .lang files
    26
    27  see: http://translate.sourceforge.net/wiki/toolkit/po2mozlang for examples and
    28  usage instructions
    29  """
    30
    31  from translate.storage import mozilla_lang as lang
    32  from translate.storage import po
    33
    34
    35 -class po2lang:
    36
    37 -    def __init__(self, duplicatestyle="msgctxt"):
    38          self.duplicatestyle = duplicatestyle
    39
    40 -    def convertstore(self, inputstore, includefuzzy=False):
    41          """converts a file to .lang format"""
    42          thetargetfile = lang.LangStore()
    43
    44          # Run over the po units
    45          for pounit in inputstore.units:
    46              # Skip the header
    47              if pounit.isheader():
    48                  continue
    49              newunit = thetargetfile.addsourceunit(pounit.source)
    50              if includefuzzy or not pounit.isfuzzy():
    51                  newunit.settarget(pounit.target)
    52              else:
    53                  newunit.settarget(pounit.source)
    54          return thetargetfile
    55
    56
    57 -def convertlang(inputfile, outputfile, templates,  includefuzzy=False):
    58      """reads in stdin using fromfileclass, converts using convertorclass,
    59      writes to stdout"""
    60      inputstore = po.pofile(inputfile)
    61      if inputstore.isempty():
    62          return 0
    63      convertor = po2lang()
    64      outputstore = convertor.convertstore(inputstore,  includefuzzy)
    65      outputfile.write(str(outputstore))
    66      return 1
    67
    68
    69  formats = {
    70      "po": ("lang", convertlang),
    71      ("po", "lang"): ("lang", convertlang),
    72  }
    73
    74
    75 -def main(argv=None):
    76      from translate.convert import convert
    77      from translate.misc import stdiotell
    78      import sys
    79      sys.stdout = stdiotell.StdIOWrapper(sys.stdout)
    80      parser = convert.ConvertOptionParser(formats, usetemplates=True,
    81                                             description=__doc__)
    82      parser.add_fuzzy_option()
    83      parser.run(argv)
    84
    85
    86  if __name__ == '__main__':
    87      main()
    88