| LEFT | RIGHT |
|---|---|
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # -*- coding: utf-8 -*- | 2 # -*- coding: utf-8 -*- |
| 3 # | 3 # |
| 4 # Copyright 2002-2006 Zuza Software Foundation | 4 # Copyright 2002-2006 Zuza Software Foundation |
| 5 # | 5 # |
| 6 # This file is part of translate. | 6 # This file is part of translate. |
| 7 # | 7 # |
| 8 # translate is free software; you can redistribute it and/or modify | 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 | 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 | 10 # the Free Software Foundation; either version 2 of the License, or |
| 11 # (at your option) any later version. | 11 # (at your option) any later version. |
| 12 # | 12 # |
| 13 # translate is distributed in the hope that it will be useful, | 13 # translate is distributed in the hope that it will be useful, |
| 14 # but WITHOUT ANY WARRANTY; without even the implied warranty of | 14 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 # GNU General Public License for more details. | 16 # GNU General Public License for more details. |
| 17 # | 17 # |
| 18 # You should have received a copy of the GNU General Public License | 18 # You should have received a copy of the GNU General Public License |
| 19 # along with translate; if not, write to the Free Software | 19 # along with translate; if not, write to the Free Software |
| 20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | 20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 # | 21 # |
| 22 | 22 |
| 23 """convert XLIFF localization files to Gettext PO localization files | 23 """convert XLIFF localization files to Gettext PO localization files |
| 24 | 24 |
| 25 see: http://translate.sourceforge.net/wiki/toolkit/xliff2po for examples and | 25 see: http://translate.sourceforge.net/wiki/toolkit/xliff2po for examples and |
| 26 usage instructions | 26 usage instructions |
| 27 """ | 27 """ |
| 28 | 28 |
| 29 from translate.storage import po | 29 from translate.storage import po |
| 30 from translate.storage import xliff | 30 from translate.storage import xliff |
| 31 from translate.misc import wStringIO | 31 from translate.misc import wStringIO |
| 32 | 32 |
| 33 class xliff2po: | 33 class xliff2po: |
| 34 def converttransunit(self, transunit): | 34 def converttransunit(self, transunit): |
| 35 """makes a pounit from the given transunit""" | 35 """makes a pounit from the given transunit""" |
| 36 thepo = po.pounit() | 36 thepo = po.pounit() |
| 37 | 37 |
| 38 #Header | 38 #Header |
| 39 if transunit.getrestype() == "x-gettext-domain-header": | 39 if transunit.getrestype() == "x-gettext-domain-header": |
| 40 thepo.source = "" | 40 thepo.source = "" |
| 41 else: | 41 else: |
| 42 thepo.source = transunit.source | 42 thepo.source = transunit.source |
| 43 thepo.target = transunit.target | 43 thepo.target = transunit.target |
| 44 | 44 |
| 45 #Context | 45 #Context |
| 46 context = transunit.getcontext_message() | 46 context = transunit.getcontextgroups('po-1025') |
|
georgeyk
2008/07/24 05:14:28
Here, the context should also be accessed though a
| |
| 47 if context: | 47 if context: |
| 48 thepo.setcontext(context) | 48 thepo.setcontext(context[0][0][1]) |
| 49 | 49 |
| 50 #Location comments | 50 #Location comments |
| 51 locations = transunit.getlocations() | 51 locations = transunit.getlocations() |
| 52 if locations: | 52 if locations: |
| 53 thepo.addlocation("%s" % " ".join(locations)) | 53 thepo.addlocation("%s" % " ".join(locations)) |
| 54 | 54 |
| 55 #NOTE: Supporting both <context> and <note> tags in xliff files for comm ents | 55 #NOTE: Supporting both <context> and <note> tags in xliff files for comm ents |
| 56 #Translator comments | 56 #Translator comments |
| 57 trancomments = transunit.getnotes("translator") | 57 trancomments = transunit.getnotes("translator") |
| 58 if trancomments: | 58 if trancomments: |
| 59 thepo.addnote(trancomments, origin="translator") | 59 thepo.addnote(trancomments, origin="translator") |
| 60 | 60 |
| 61 #Automatic and Developer comments | 61 #Automatic and Developer comments |
| 62 autocomments = transunit.getnotes("developer") | 62 autocomments = transunit.getnotes("developer") |
| 63 if autocomments: | 63 if autocomments: |
| 64 thepo.addnote(autocomments, origin="developer") | 64 thepo.addnote(autocomments, origin="developer") |
| 65 | 65 |
| 66 #See 5.6.1 of the spec. We should not check fuzzyness, but approved attr ibute | 66 #See 5.6.1 of the spec. We should not check fuzzyness, but approved attr ibute |
| 67 if transunit.isfuzzy(): | 67 if transunit.isfuzzy(): |
| 68 thepo.markfuzzy(True) | 68 thepo.markfuzzy(True) |
| 69 | 69 |
| 70 return thepo | 70 return thepo |
| 71 | 71 |
| 72 def convertstore(self, inputfile): | 72 def convertstore(self, inputfile): |
| 73 """converts a .xliff file to .po format""" | 73 """converts a .xliff file to .po format""" |
| 74 # XXX: The inputfile is converted to string because Pootle supplies | 74 # XXX: The inputfile is converted to string because Pootle supplies |
| 75 # XXX: a PootleFile object as input which cannot be sent to PoXliffFile. | 75 # XXX: a PootleFile object as input which cannot be sent to PoXliffFile. |
| 76 # XXX: The better way would be to have a consistent conversion API. | 76 # XXX: The better way would be to have a consistent conversion API. |
| 77 if not isinstance(inputfile, (file, wStringIO.StringIO)): | 77 if not isinstance(inputfile, (file, wStringIO.StringIO)): |
| 78 inputfile = str(inputfile) | 78 inputfile = str(inputfile) |
| 79 XliffFile = xliff.xlifffile.parsestring(inputfile) | 79 XliffFile = xliff.xlifffile.parsestring(inputfile) |
| 80 thetargetfile = po.pofile() | 80 thetargetfile = po.pofile() |
| 81 targetheader = thetargetfile.makeheader(charset="UTF-8", encoding="8bit" ) | 81 targetheader = thetargetfile.makeheader(charset="UTF-8", encoding="8bit" ) |
| 82 # TODO: support multiple files | 82 # TODO: support multiple files |
| 83 for transunit in XliffFile.units: | 83 for transunit in XliffFile.units: |
| 84 thepo = self.converttransunit(transunit) | 84 thepo = self.converttransunit(transunit) |
| 85 thetargetfile.addunit(thepo) | 85 thetargetfile.addunit(thepo) |
| 86 return thetargetfile | 86 return thetargetfile |
| 87 | 87 |
| 88 def convertxliff(inputfile, outputfile, templates): | 88 def convertxliff(inputfile, outputfile, templates): |
| 89 """reads in stdin using fromfileclass, converts using convertorclass, writes to stdout""" | 89 """reads in stdin using fromfileclass, converts using convertorclass, writes to stdout""" |
| 90 convertor = xliff2po() | 90 convertor = xliff2po() |
| 91 outputstore = convertor.convertstore(inputfile) | 91 outputstore = convertor.convertstore(inputfile) |
| 92 if outputstore.isempty(): | 92 if outputstore.isempty(): |
| 93 return 0 | 93 return 0 |
| 94 outputfile.write(str(outputstore)) | 94 outputfile.write(str(outputstore)) |
| 95 return 1 | 95 return 1 |
| 96 | 96 |
| 97 def main(argv=None): | 97 def main(argv=None): |
| 98 from translate.convert import convert | 98 from translate.convert import convert |
| 99 formats = {"xlf":("po", convertxliff)} | 99 formats = {"xlf":("po", convertxliff)} |
| 100 parser = convert.ConvertOptionParser(formats, usepots=True, description=__do c__) | 100 parser = convert.ConvertOptionParser(formats, usepots=True, description=__do c__) |
| 101 parser.run(argv) | 101 parser.run(argv) |
| 102 | 102 |
| LEFT | RIGHT |