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

Source Code for Module translate.convert.oo2xliff

  1  #!/usr/bin/env python 
  2  # -*- coding: utf-8 -*- 
  3  # 
  4  # Copyright 2003-2008 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 an OpenOffice.org (SDF) localization file to XLIFF localization files 
 24   
 25  User documentation: http://translate.sourceforge.net/wiki/toolkit/oo2po 
 26  """ 
 27   
 28  import os 
 29  import sys 
 30  from translate.storage import xliff 
 31  from translate.storage import oo 
 32   
 33  # TODO: support using one GSI file as template, another as input (for when English is in one and translation in another) 
 34   
35 -class oo2xliff:
36 - def __init__(self, sourcelanguage, targetlanguage, blankmsgstr=False, long_keys=False):
37 """construct an oo2xliff converter for the specified languages""" 38 self.sourcelanguage = sourcelanguage 39 self.targetlanguage = targetlanguage 40 self.blankmsgstr = blankmsgstr 41 self.long_keys = long_keys
42
43 - def maketargetunit(self, part1, part2, translators_comment, key, subkey):
44 """makes a base unit (.po or XLIFF) out of a subkey of two parts""" 45 #TODO: Do better 46 text1 = getattr(part1, subkey) 47 if text1 == "": 48 return None 49 text2 = getattr(part2, subkey) 50 51 unit = xliff.xliffunit(text1) 52 unit.target = text2 53 unit.markfuzzy(False) 54 unit.addlocation(key + "." + subkey) 55 if getattr(translators_comment, subkey).strip() != "": 56 unit.addnote(getattr(translators_comment, subkey), origin="developer") 57 return unit
58
59 - def makekey(self, ookey):
60 """converts an oo key tuple into a key identifier for the base class file (.po or XLIFF)""" 61 project, sourcefile, resourcetype, groupid, localid, platform = ookey 62 sourcefile = sourcefile.replace('\\','/') 63 if self.long_keys: 64 sourcebase = os.path.join(project, sourcefile) 65 else: 66 sourceparts = sourcefile.split('/') 67 sourcebase = "".join(sourceparts[-1:]) 68 if (groupid) == 0 or len(localid) == 0: 69 ooid = groupid + localid 70 else: 71 ooid = groupid + "." + localid 72 if resourcetype: 73 ooid = ooid + "." + resourcetype 74 key = "%s#%s" % (sourcebase, ooid) 75 return oo.normalizefilename(key)
76
77 - def convertelement(self, theoo):
78 """convert an oo element into a list of base units (.po or XLIFF)""" 79 if self.sourcelanguage in theoo.languages: 80 part1 = theoo.languages[self.sourcelanguage] 81 else: 82 print >> sys.stderr, "/".join(theoo.lines[0].getkey()), "language not found: %s" % (self.sourcelanguage) 83 return [] 84 if self.blankmsgstr: 85 # use a blank part2 86 part2 = oo.ooline() 87 else: 88 if self.targetlanguage in theoo.languages: 89 part2 = theoo.languages[self.targetlanguage] 90 else: 91 # if the language doesn't exist, the translation is missing ... so make it blank 92 part2 = oo.ooline() 93 if "x-comment" in theoo.languages: 94 translators_comment = theoo.languages["x-comment"] 95 else: 96 translators_comment = oo.ooline() 97 key = self.makekey(part1.getkey()) 98 unitlist = [] 99 for subkey in ("text", "quickhelptext", "title"): 100 unit = self.maketargetunit(part1, part2, translators_comment, key, subkey) 101 if unit is not None: 102 unitlist.append(unit) 103 return unitlist
104
105 - def convertstore(self, theoofile, duplicatestyle="msgctxt"):
106 """converts an entire oo file to a base class format (.po or XLIFF)""" 107 thetargetfile = xliff.xlifffile() 108 # create a header for the file 109 bug_url = 'http://qa.openoffice.org/issues/enter_bug.cgi' + ('''?subcomponent=ui&comment=&short_desc=Localization issue in file: %(filename)s&component=l10n&form_name=enter_issue''' % {"filename": theoofile.filename}).replace(" ", "%20").replace(":", "%3A") 110 # go through the oo and convert each element 111 for theoo in theoofile.units: 112 unitlist = self.convertelement(theoo) 113 for unit in unitlist: 114 thetargetfile.addunit(unit) 115 return thetargetfile
116
117 -def verifyoptions(options):
118 """verifies the commandline options""" 119 if not options.targetlanguage: 120 raise ValueError("You must specify the target language.")
121
122 -def convertoo(inputfile, outputfile, templates, pot=False, sourcelanguage=None, targetlanguage=None, duplicatestyle="msgid_comment", multifilestyle="single"):
123 """reads in stdin using inputstore class, converts using convertorclass, writes to stdout""" 124 inputstore = oo.oofile() 125 if hasattr(inputfile, "filename"): 126 inputstore.filename = inputfile.filename 127 inputstore.parse(inputfile.read()) 128 if not sourcelanguage: 129 testlangtype = targetlanguage or (inputstore and inputstore.languages[0]) or "" 130 if testlangtype.isdigit(): 131 sourcelanguage = "01" 132 else: 133 sourcelanguage = "en-US" 134 if not sourcelanguage in inputstore.languages: 135 print >> sys.stderr, "Warning: sourcelanguage %s not found in inputfile (contains %s)" % (sourcelanguage, ", ".join(inputstore.languages)) 136 if not pot and targetlanguage and targetlanguage not in inputstore.languages: 137 print >> sys.stderr, "Warning: targetlanguage %s not found in inputfile (contains %s)" % (targetlanguage, ", ".join(inputstore.languages)) 138 convertor = oo2xliff(sourcelanguage, targetlanguage, blankmsgstr=pot, long_keys=multifilestyle!="single") 139 outputstore = convertor.convertstore(inputstore, duplicatestyle) 140 if outputstore.isempty(): 141 return 0 142 outputfile.write(str(outputstore)) 143 return 1
144
145 -def main(argv=None):
146 from translate.convert import convert 147 formats = {"oo":("xlf",convertoo), "sdf":("xlf",convertoo)} 148 # always treat the input as an archive unless it is a directory 149 archiveformats = {(None, "input"): oo.oomultifile} 150 parser = convert.ArchiveConvertOptionParser(formats, usepots=False, description=__doc__, archiveformats=archiveformats) 151 parser.add_option("-l", "--language", dest="targetlanguage", default=None, 152 help="set target language to extract from oo file (e.g. af-ZA)", metavar="LANG") 153 parser.add_option("", "--source-language", dest="sourcelanguage", default=None, 154 help="set source language code (default en-US)", metavar="LANG") 155 parser.add_option("", "--nonrecursiveinput", dest="allowrecursiveinput", default=True, action="store_false", help="don't treat the input oo as a recursive store") 156 parser.add_duplicates_option() 157 parser.add_multifile_option() 158 parser.passthrough.append("sourcelanguage") 159 parser.passthrough.append("targetlanguage") 160 parser.verifyoptions = verifyoptions 161 parser.run(argv)
162 163 if __name__ == '__main__': 164 main() 165