translate.convert.mozlang2po

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

# Source Code for Module translate.convert.mozlang2po

     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 Java/Mozilla .properties files to Gettext PO localization files
    26
    27  See: http://translate.sourceforge.net/wiki/toolkit/prop2po 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 lang2po:
    36
    37 -    def __init__(self, duplicatestyle="msgctxt"):
    38          self.duplicatestyle = duplicatestyle
    39
    40 -    def convertstore(self, thelangfile):
    41          """converts a file to .po format"""
    42          thetargetfile = po.pofile()
    43
    44          # Set up the header
    45          targetheader = thetargetfile.init_headers(charset="UTF-8",
    46                                                   encoding="8bit")
    47          targetheader.addnote("extracted from %s" %
    48                               thelangfile.filename, "developer")
    49
    50          # For each lang unit, make the new po unit accordingly
    51          for langunit in thelangfile.units:
    52              newunit = thetargetfile.addsourceunit(langunit.source)
    53              newunit.settarget(langunit.target)
    54              newunit.addlocations(langunit.getlocations())
    55
    56          # Remove duplicates, because we can
    57          thetargetfile.removeduplicates(self.duplicatestyle)
    58          return thetargetfile
    59
    60
    61 -def convertlang(inputfile, outputfile, templates, duplicatestyle="msgctxt",
    62                  encoding="utf-8"):
    63      """reads in stdin using fromfileclass, converts using convertorclass,
    64      writes to stdout"""
    65      inputstore = lang.LangStore(inputfile, encoding=encoding)
    66      convertor = lang2po(duplicatestyle=duplicatestyle)
    67      outputstore = convertor.convertstore(inputstore)
    68      if outputstore.isempty():
    69          return 0
    70      outputfile.write(str(outputstore))
    71      return 1
    72
    73
    74  formats = {
    75      "lang": ("po", convertlang)
    76  }
    77
    78
    79 -def main(argv=None):
    80      from translate.convert import convert
    81      from translate.misc import stdiotell
    82      import sys
    83      sys.stdout = stdiotell.StdIOWrapper(sys.stdout)
    84      parser = convert.ConvertOptionParser(formats, usepots=True,
    85                                             description=__doc__)
    86      parser.add_option("", "--encoding", dest="encoding", default='utf-8',
    87      help="The encoding of the input file (default: UTF-8)")
    88      parser.passthrough.append("encoding")
    89      parser.add_duplicates_option()
    90      parser.run(argv)
    91
    92
    93  if __name__ == '__main__':
    94      main()
    95