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

Delta Between Two Patch Sets: translate/convert/po2xliff.py

Issue 65: xliff2po & po2xliff should handle context SVN Base: https://translate.svn.sourceforge.net/svnroot/translate/src/trunk/
Left Patch Set: Update (minor changes) Created 1 year, 4 months ago
Right Patch Set: 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:
Left: View regular side by side diff
Right: View regular side by side diff
LEFTRIGHT
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*- 2 # -*- coding: utf-8 -*-
3 # 3 #
4 # Copyright 2005, 2006 Zuza Software Foundation 4 # Copyright 2005, 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 Gettext PO localization files to XLIFF localization files 23 """convert Gettext PO localization files to XLIFF localization files
24 24
25 see: http://translate.sourceforge.net/wiki/toolkit/po2xliff for examples and 25 see: http://translate.sourceforge.net/wiki/toolkit/po2xliff 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 poxliff 30 from translate.storage import poxliff
31 31
32 class po2xliff: 32 class po2xliff:
33 def convertunit(self, outputstore, inputunit, filename): 33 def convertunit(self, outputstore, inputunit, filename):
34 """creates a transunit node""" 34 """creates a transunit node"""
35 source = inputunit.source 35 source = inputunit.source
36 target = inputunit.target 36 target = inputunit.target
37 if inputunit.isheader(): 37 if inputunit.isheader():
38 unit = outputstore.addheaderunit(target, filename) 38 unit = outputstore.addheaderunit(target, filename)
39 else: 39 else:
40 unit = outputstore.addsourceunit(source, filename, True) 40 unit = outputstore.addsourceunit(source, filename, True)
41 unit.target = target 41 unit.target = target
42 #Explicitly marking the fuzzy state will ensure that normal (transla ted) 42 #Explicitly marking the fuzzy state will ensure that normal (transla ted)
43 #units in the PO file end up as approved in the XLIFF file. 43 #units in the PO file end up as approved in the XLIFF file.
44 if target: 44 if target:
45 unit.markfuzzy(inputunit.isfuzzy()) 45 unit.markfuzzy(inputunit.isfuzzy())
46 else: 46 else:
47 unit.markapproved(False) 47 unit.markapproved(False)
48 48
49 #Handle msgctxt 49 #Handle msgctxt
50 context = inputunit.getcontext() 50 context = inputunit.getcontext()
51 if context: 51 if context:
52 unit.setcontext_message(context) 52 unit.createcontextgroup("po-1025", [("x-unknown", context)],
georgeyk 2008/07/24 05:14:28 I think this should be done though a method. So we
53 purpose="match information")
53 54
54 #Handle #: location comments 55 #Handle #: location comments
55 for location in inputunit.getlocations(): 56 for location in inputunit.getlocations():
56 unit.createcontextgroup("po-reference", self.contextlist(locatio n), purpose="location") 57 unit.createcontextgroup("po-reference", self.contextlist(locatio n), purpose="location")
57 58
58 #Handle #. automatic comments 59 #Handle #. automatic comments
59 comment = inputunit.getnotes("developer") 60 comment = inputunit.getnotes("developer")
60 if comment: 61 if comment:
61 unit.createcontextgroup("po-entry", [("x-po-autocomment", commen t)], purpose="information") 62 unit.createcontextgroup("po-entry", [("x-po-autocomment", commen t)], purpose="information")
62 unit.addnote(comment, origin="developer") 63 unit.addnote(comment, origin="developer")
63 64
64 #TODO: x-format, etc. 65 #TODO: x-format, etc.
65 66
66 67
67 #Handle # other comments 68 #Handle # other comments
68 comment = inputunit.getnotes("translator") 69 comment = inputunit.getnotes("translator")
69 if comment: 70 if comment:
70 unit.createcontextgroup("po-entry", [("x-po-trancomment", comment)], purpose="information") 71 unit.createcontextgroup("po-entry", [("x-po-trancomment", comment)], purpose="information")
71 unit.addnote(comment, origin="po-translator") 72 unit.addnote(comment, origin="po-translator")
72 73
73 return unit 74 return unit
74 75
75 def contextlist(self, location): 76 def contextlist(self, location):
76 contexts = [] 77 contexts = []
77 if ":" in location: 78 if ":" in location:
78 sourcefile, linenumber = location.split(":", 1) 79 sourcefile, linenumber = location.split(":", 1)
79 else: 80 else:
80 sourcefile, linenumber = location, None 81 sourcefile, linenumber = location, None
81 contexts.append(("sourcefile", sourcefile)) 82 contexts.append(("sourcefile", sourcefile))
82 if linenumber: 83 if linenumber:
83 contexts.append(("linenumber", linenumber)) 84 contexts.append(("linenumber", linenumber))
84 return contexts 85 return contexts
85 86
86 def convertstore(self, inputstore, templatefile=None, **kwargs): 87 def convertstore(self, inputstore, templatefile=None, **kwargs):
87 """converts a .po file to .xlf format""" 88 """converts a .po file to .xlf format"""
88 if templatefile is None: 89 if templatefile is None:
89 outputstore = poxliff.PoXliffFile(**kwargs) 90 outputstore = poxliff.PoXliffFile(**kwargs)
90 else: 91 else:
91 outputstore = poxliff.PoXliffFile(templatefile, **kwargs) 92 outputstore = poxliff.PoXliffFile(templatefile, **kwargs)
92 filename = inputstore.filename 93 filename = inputstore.filename
93 for inputunit in inputstore.units: 94 for inputunit in inputstore.units:
94 if inputunit.isblank(): 95 if inputunit.isblank():
95 continue 96 continue
96 transunitnode = self.convertunit(outputstore, inputunit, filename) 97 transunitnode = self.convertunit(outputstore, inputunit, filename)
97 return str(outputstore) 98 return str(outputstore)
98 99
99 def convertpo(inputfile, outputfile, templatefile): 100 def convertpo(inputfile, outputfile, templatefile):
100 """reads in stdin using fromfileclass, converts using convertorclass, writes to stdout""" 101 """reads in stdin using fromfileclass, converts using convertorclass, writes to stdout"""
101 inputstore = po.pofile(inputfile) 102 inputstore = po.pofile(inputfile)
102 if inputstore.isempty(): 103 if inputstore.isempty():
103 return 0 104 return 0
104 convertor = po2xliff() 105 convertor = po2xliff()
105 outputstring = convertor.convertstore(inputstore, templatefile) 106 outputstring = convertor.convertstore(inputstore, templatefile)
106 outputfile.write(outputstring) 107 outputfile.write(outputstring)
107 return 1 108 return 1
108 109
109 def main(argv=None): 110 def main(argv=None):
110 from translate.convert import convert 111 from translate.convert import convert
111 formats = {"po": ("xlf", convertpo), ("po", "xlf"): ("xlf", convertpo)} 112 formats = {"po": ("xlf", convertpo), ("po", "xlf"): ("xlf", convertpo)}
112 parser = convert.ConvertOptionParser(formats, usepots=True, usetemplates=Tru e, description=__doc__) 113 parser = convert.ConvertOptionParser(formats, usepots=True, usetemplates=Tru e, description=__doc__)
113 parser.run(argv) 114 parser.run(argv)
114 115
115 if __name__ == '__main__': 116 if __name__ == '__main__':
116 main() 117 main()
LEFTRIGHT

Powered by Google App Engine
This is Rietveld r159