Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(24)

Side by Side Diff: translate/convert/xliff2po.py

Issue 65: xliff2po & po2xliff should handle context SVN Base: https://translate.svn.sourceforge.net/svnroot/translate/src/trunk/
Patch Set: A more complete patch, supporting CPO and pypo Created 1 year, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff
OLDNEW
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
45 #Context
46 context = transunit.getcontext_message()
47 if context:
48 thepo.setcontext(context)
44 49
45 #Location comments 50 #Location comments
46 locations = transunit.getlocations() 51 locations = transunit.getlocations()
47 if locations: 52 if locations:
48 thepo.addlocation("%s" % " ".join(locations)) 53 thepo.addlocation("%s" % " ".join(locations))
49 54
50 #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
51 #Translator comments 56 #Translator comments
52 trancomments = transunit.getnotes("translator") 57 trancomments = transunit.getnotes("translator")
53 if trancomments: 58 if trancomments:
54 thepo.addnote(trancomments, origin="translator") 59 thepo.addnote(trancomments, origin="translator")
55 60
56 #Automatic and Developer comments 61 #Automatic and Developer comments
57 autocomments = transunit.getnotes("developer") 62 autocomments = transunit.getnotes("developer")
58 if autocomments: 63 if autocomments:
59 thepo.addnote(autocomments, origin="developer") 64 thepo.addnote(autocomments, origin="developer")
60 65
61 #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
62 if transunit.isfuzzy(): 67 if transunit.isfuzzy():
63 thepo.markfuzzy(True) 68 thepo.markfuzzy(True)
64 69
65 return thepo 70 return thepo
66 71
67 def convertstore(self, inputfile): 72 def convertstore(self, inputfile):
68 """converts a .xliff file to .po format""" 73 """converts a .xliff file to .po format"""
69 # XXX: The inputfile is converted to string because Pootle supplies 74 # XXX: The inputfile is converted to string because Pootle supplies
70 # 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.
71 # 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.
72 if not isinstance(inputfile, (file, wStringIO.StringIO)): 77 if not isinstance(inputfile, (file, wStringIO.StringIO)):
73 inputfile = str(inputfile) 78 inputfile = str(inputfile)
74 XliffFile = xliff.xlifffile.parsestring(inputfile) 79 XliffFile = xliff.xlifffile.parsestring(inputfile)
75 thetargetfile = po.pofile() 80 thetargetfile = po.pofile()
76 targetheader = thetargetfile.makeheader(charset="UTF-8", encoding="8bit" ) 81 targetheader = thetargetfile.makeheader(charset="UTF-8", encoding="8bit" )
77 # TODO: support multiple files 82 # TODO: support multiple files
78 for transunit in XliffFile.units: 83 for transunit in XliffFile.units:
79 thepo = self.converttransunit(transunit) 84 thepo = self.converttransunit(transunit)
80 thetargetfile.addunit(thepo) 85 thetargetfile.addunit(thepo)
81 return thetargetfile 86 return thetargetfile
82 87
83 def convertxliff(inputfile, outputfile, templates): 88 def convertxliff(inputfile, outputfile, templates):
84 """reads in stdin using fromfileclass, converts using convertorclass, writes to stdout""" 89 """reads in stdin using fromfileclass, converts using convertorclass, writes to stdout"""
85 convertor = xliff2po() 90 convertor = xliff2po()
86 outputstore = convertor.convertstore(inputfile) 91 outputstore = convertor.convertstore(inputfile)
87 if outputstore.isempty(): 92 if outputstore.isempty():
88 return 0 93 return 0
89 outputfile.write(str(outputstore)) 94 outputfile.write(str(outputstore))
90 return 1 95 return 1
91 96
92 def main(argv=None): 97 def main(argv=None):
93 from translate.convert import convert 98 from translate.convert import convert
94 formats = {"xlf":("po", convertxliff)} 99 formats = {"xlf":("po", convertxliff)}
95 parser = convert.ConvertOptionParser(formats, usepots=True, description=__do c__) 100 parser = convert.ConvertOptionParser(formats, usepots=True, description=__do c__)
96 parser.run(argv) 101 parser.run(argv)
97 102
OLDNEW

Powered by Google App Engine
This is Rietveld r159