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

# Source Code for Module translate.convert.txt2po

     1  #!/usr/bin/env python
     2  # -*- coding: utf-8 -*-
     3  #
     4  # Copyright 2004-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 plain text (.txt) files to Gettext PO localization files
    24
    25  See: http://translate.sourceforge.net/wiki/toolkit/txt2po for examples and
    26  usage instructions
    27  """
    28
    29  from translate.storage import txt
    30  from translate.storage import po
    31
    32
    33 -class txt2po:
    34
    35 -    def __init__(self, duplicatestyle="msgctxt"):
    36          self.duplicatestyle = duplicatestyle
    37
    38 -    def convertstore(self, thetxtfile):
    39          """converts a file to .po format"""
    40          thetargetfile = po.pofile()
    41          targetheader = thetargetfile.init_headers(charset="UTF-8", encoding="8bit")
    42          targetheader.addnote("extracted from %s" % thetxtfile.filename, "developer")
    43
    44          for txtunit in thetxtfile.units:
    45              newunit = thetargetfile.addsourceunit(txtunit.source)
    46              newunit.addlocations(txtunit.getlocations())
    47          thetargetfile.removeduplicates(self.duplicatestyle)
    48          return thetargetfile
    49
    50
    51 -def converttxt(inputfile, outputfile, templates, duplicatestyle="msgctxt", encoding="utf-8", flavour=None):
    52      """reads in stdin using fromfileclass, converts using convertorclass, writes to stdout"""
    53      inputstore = txt.TxtFile(inputfile, encoding=encoding, flavour=flavour)
    54      convertor = txt2po(duplicatestyle=duplicatestyle)
    55      outputstore = convertor.convertstore(inputstore)
    56      if outputstore.isempty():
    57          return 0
    58      outputfile.write(str(outputstore))
    59      return 1
    60
    61
    62 -def main(argv=None):
    63      from translate.convert import convert
    64      from translate.misc import stdiotell
    65      import sys
    66      sys.stdout = stdiotell.StdIOWrapper(sys.stdout)
    67      formats = {"txt": ("po", converttxt), "*": ("po", converttxt)}
    68      parser = convert.ConvertOptionParser(formats, usepots=True, description=__doc__)
    69      parser.add_option("", "--encoding", dest="encoding", default='utf-8', type="string",
    70              help="The encoding of the input file (default: UTF-8)")
    71      parser.passthrough.append("encoding")
    72      parser.add_option("", "--flavour", dest="flavour", default="plain",
    73              type="choice", choices=["plain", "dokuwiki", "mediawiki"],
    74              help="The flavour of text file: plain (default), dokuwiki, mediawiki",
    75              metavar="FLAVOUR")
    76      parser.passthrough.append("flavour")
    77      parser.add_duplicates_option()
    78      parser.run(argv)
    79