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 -class txt2po:
33 - def __init__(self, duplicatestyle="msgctxt"):
34 self.duplicatestyle = duplicatestyle
35
36 - def convertstore(self, thetxtfile):
37 """converts a file to .po format""" 38 thetargetfile = po.pofile() 39 targetheader = thetargetfile.makeheader(charset="UTF-8", encoding="8bit") 40 targetheader.addnote("extracted from %s" % thetxtfile.filename, "developer") 41 thetargetfile.addunit(targetheader) 42 for txtunit in thetxtfile.units: 43 newunit = thetargetfile.addsourceunit(txtunit.source) 44 newunit.addlocations(txtunit.getlocations()) 45 thetargetfile.removeduplicates(self.duplicatestyle) 46 return thetargetfile
47
48 -def converttxt(inputfile, outputfile, templates, duplicatestyle="msgctxt", encoding="utf-8", flavour=None):
49 """reads in stdin using fromfileclass, converts using convertorclass, writes to stdout""" 50 inputstore = txt.TxtFile(inputfile, encoding=encoding, flavour=flavour) 51 convertor = txt2po(duplicatestyle=duplicatestyle) 52 outputstore = convertor.convertstore(inputstore) 53 if outputstore.isempty(): 54 return 0 55 outputfile.write(str(outputstore)) 56 return 1
57
58 -def main(argv=None):
59 from translate.convert import convert 60 from translate.misc import stdiotell 61 import sys 62 sys.stdout = stdiotell.StdIOWrapper(sys.stdout) 63 formats = {"txt":("po", converttxt), "*":("po", converttxt)} 64 parser = convert.ConvertOptionParser(formats, usepots=True, description=__doc__) 65 parser.add_option("", "--encoding", dest="encoding", default='utf-8', type="string", 66 help="The encoding of the input file (default: UTF-8)") 67 parser.passthrough.append("encoding") 68 parser.add_option("", "--flavour", dest="flavour", default="plain", 69 type="choice", choices=["plain", "dokuwiki", "mediawiki"], 70 help="The flavour of text file: plain (default), dokuwiki, mediawiki", 71 metavar="FLAVOUR") 72 parser.passthrough.append("flavour") 73 parser.add_duplicates_option() 74 parser.run(argv)
75