2 # -*- coding: utf-8 -*-
4 # Copyright 2005, 2006 Zuza Software Foundation
6 # This file is part of translate.
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.
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.
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
23 """convert Gettext PO localization files to a TMX (Translation Memory eXchange) file
25 see: http://translate.sourceforge.net/wiki/toolkit/po2tmx for examples and
29 from translate
.storage
import po
30 from translate
.storage
import tmx
31 from translate
.convert
import convert
32 from translate
.misc
import wStringIO
36 def convertfile(self
, inputfile
, sourcelanguage
='en', targetlanguage
=None):
37 """converts a .po file to TMX file"""
38 # TODO: This seems to not be used... remove it
39 inputstore
= inputfile
40 for inunit
in inputstore
.units
:
41 if inunit
.isheader() or inunit
.isblank() or not inunit
.istranslated() or inunit
.isfuzzy():
43 source
= inunit
.source
44 translation
= inunit
.target
45 # TODO place source location in comments
46 tmxfile
.addtranslation(source
, sourcelanguage
, translation
, targetlanguage
)
49 def convertfiles(self
, inputfile
, tmxfile
, sourcelanguage
='en', targetlanguage
=None):
50 """converts a .po file (possibly many) to TMX file"""
51 inputstore
= po
.pofile(inputfile
)
52 for inunit
in inputstore
.units
:
53 if inunit
.isheader() or inunit
.isblank() or not inunit
.istranslated() or inunit
.isfuzzy():
55 source
= inunit
.source
56 translation
= inunit
.target
57 # TODO place source location in comments
58 tmxfile
.addtranslation(source
, sourcelanguage
, translation
, targetlanguage
)
60 def convertpo(inputfile
, outputfile
, templatefile
, sourcelanguage
='en', targetlanguage
=None):
61 """reads in stdin using fromfileclass, converts using convertorclass, writes to stdout"""
63 convertor
.convertfiles(inputfile
, outputfile
.tmxfile
, sourcelanguage
, targetlanguage
)
67 def __init__(self
, filename
, mode
=None):
68 """initialises tmxmultifile from a seekable inputfile or writable outputfile"""
69 self
.filename
= filename
71 if os
.path
.exists(filename
):
76 # self.multifilestyle = multifilestyle
77 self
.multifilename
= os
.path
.splitext(filename
)[0]
78 # self.multifile = open(filename, mode)
79 self
.tmxfile
= tmx
.tmxfile()
81 def openoutputfile(self
, subfile
):
82 """returns a pseudo-file object for the given subfile"""
83 def onclose(contents
):
85 outputfile
= wStringIO
.CatchStringOutput(onclose
)
86 outputfile
.filename
= subfile
87 outputfile
.tmxfile
= self
.tmxfile
91 class TmxOptionParser(convert
.ArchiveConvertOptionParser
):
92 def recursiveprocess(self
, options
):
93 if not options
.targetlanguage
:
94 raise ValueError("You must specify the target language")
95 super(TmxOptionParser
, self
).recursiveprocess(options
)
96 self
.output
= open(options
.output
, 'w')
97 options
.outputarchive
.tmxfile
.setsourcelanguage(options
.sourcelanguage
)
98 self
.output
.write(str(options
.outputarchive
.tmxfile
))
101 formats
= {"po": ("tmx", convertpo
), ("po", "tmx"): ("tmx", convertpo
)}
102 archiveformats
= {(None, "output"): tmxmultifile
, (None, "template"): tmxmultifile
}
103 parser
= TmxOptionParser(formats
, usepots
=False, usetemplates
=False, description
=__doc__
, archiveformats
=archiveformats
)
104 parser
.add_option("-l", "--language", dest
="targetlanguage", default
=None,
105 help="set target language code (e.g. af-ZA) [required]", metavar
="LANG")
106 parser
.add_option("", "--source-language", dest
="sourcelanguage", default
='en',
107 help="set source language code (default: en)", metavar
="LANG")
108 parser
.passthrough
.append("sourcelanguage")
109 parser
.passthrough
.append("targetlanguage")
113 if __name__
== '__main__':