1# 2# Copyright 2005-2007 Zuza Software Foundation 3# 4# This file is part of translate. 5# 6# translate is free software; you can redistribute it and/or modify 7# it under the terms of the GNU General Public License as published by 8# the Free Software Foundation; either version 2 of the License, or 9# (at your option) any later version. 10# 11# translate is distributed in the hope that it will be useful, 12# but WITHOUT ANY WARRANTY; without even the implied warranty of 13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14# GNU General Public License for more details. 15# 16# You should have received a copy of the GNU General Public License 17# along with this program; if not, see <http://www.gnu.org/licenses/>. 18 19"""Convert Gettext PO localization files to a Wordfast translation memory file. 20 21See: http://docs.translatehouse.org/projects/translate-toolkit/en/latest/commands/po2wordfast.html 22for examples and usage instructions. 23""" 24 25import os 26 27from translate.convert import convert 28from translate.misc import wStringIO 29from translate.storage import po, wordfast 30 31 32class po2wordfast: 33 def convertfiles(self, inputfile, wffile, sourcelanguage="en", targetlanguage=None): 34 """converts a .po file (possibly many) to a Wordfast TM file""" 35 inputstore = po.pofile(inputfile) 36 for inunit in inputstore.units: 37 if inunit.isheader() or inunit.isblank() or not inunit.istranslated(): 38 continue 39 source = inunit.source 40 target = inunit.target 41 newunit = wffile.addsourceunit(source) 42 newunit.target = target 43 newunit.targetlang = targetlanguage 44 45 46def convertpo( 47 inputfile, outputfile, templatefile, sourcelanguage="en", targetlanguage=None 48): 49 """reads in stdin using fromfileclass, converts using convertorclass, writes to stdout""" 50 convertor = po2wordfast() 51 outputfile.wffile.header.targetlang = targetlanguage 52 convertor.convertfiles(inputfile, outputfile.wffile, sourcelanguage, targetlanguage) 53 return 1 54 55 56class wfmultifile: 57 def __init__(self, filename, mode=None): 58 """initialises wfmultifile from a seekable inputfile or writable outputfile""" 59 self.filename = filename 60 if mode is None: 61 if os.path.exists(filename): 62 mode = "r" 63 else: 64 mode = "w" 65 self.mode = mode 66 self.multifilename = os.path.splitext(filename)[0] 67 self.wffile = wordfast.WordfastTMFile() 68 69 def openoutputfile(self, subfile): 70 """returns a pseudo-file object for the given subfile""" 71 72 def onclose(contents): 73 pass 74 75 outputfile = wStringIO.CatchStringOutput(onclose) 76 outputfile.filename = subfile 77 outputfile.wffile = self.wffile 78 return outputfile 79 80 81class WfOptionParser(convert.ArchiveConvertOptionParser): 82 def recursiveprocess(self, options): 83 if not options.targetlanguage: 84 raise ValueError("You must specify the target language") 85 super().recursiveprocess(options) 86 with open(options.output, "wb") as self.output: 87 # self.outputarchive.wffile.setsourcelanguage(options.sourcelanguage) 88 self.outputarchive.wffile.serialize(self.output) 89 90 91def main(argv=None): 92 formats = {"po": ("txt", convertpo), ("po", "txt"): ("txt", convertpo)} 93 archiveformats = {(None, "output"): wfmultifile, (None, "template"): wfmultifile} 94 parser = WfOptionParser( 95 formats, 96 usepots=False, 97 usetemplates=False, 98 description=__doc__, 99 archiveformats=archiveformats, 100 ) 101 parser.add_option( 102 "-l", 103 "--language", 104 dest="targetlanguage", 105 default=None, 106 help="set target language code (e.g. af-ZA) [required]", 107 metavar="LANG", 108 ) 109 parser.add_option( 110 "", 111 "--source-language", 112 dest="sourcelanguage", 113 default="en", 114 help="set source language code (default: en)", 115 metavar="LANG", 116 ) 117 parser.passthrough.append("sourcelanguage") 118 parser.passthrough.append("targetlanguage") 119 parser.run(argv) 120 121 122if __name__ == "__main__": 123 main() 124