Package translate :: Package tools :: Module pypo2phppo
[hide private]
[frames] | no frames]

Source Code for Module translate.tools.pypo2phppo

 1  #!/usr/bin/env python 
 2  # -*- coding: utf-8 -*- 
 3  # 
 4  # Copyright 2009 Mozilla Corporation, 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  """ Convert Python format .po files to PHP format .po files """ 
23   
24  import re 
25  from translate.storage import po 
26  from translate.misc.multistring import multistring 
27   
28 -class pypo2phppo:
29 - def convertstore(self, inputstore):
30 """Converts a given .po file (Python Format) to a PHP format .po file, the difference being 31 how variable substitutions work. PHP uses a %1$s format, and Python uses 32 a {0} format (zero indexed). This method will convert, e.g.: 33 I have {1} apples and {0} oranges 34 to 35 I have %2$s apples and %1$s oranges 36 This method ignores strings with %s as both languages will recognize that. 37 """ 38 thetargetfile = po.pofile(inputfile="") 39 40 for unit in inputstore.units: 41 newunit = self.convertunit(unit) 42 thetargetfile.addunit(newunit) 43 return thetargetfile
44
45 - def convertunit(self, unit):
46 developer_notes = unit.getnotes(origin="developer") 47 translator_notes = unit.getnotes(origin="translator") 48 unit.removenotes() 49 unit.addnote(self.convertstrings(developer_notes)) 50 unit.addnote(self.convertstrings(translator_notes)) 51 unit.source = self.convertstrings(unit.source) 52 unit.target = self.convertstrings(unit.target) 53 return unit
54
55 - def convertstring(self, string):
56 return re.sub('\{(\d)\}', lambda x: "%%%d$s" % (int(x.group(1))+1), string)
57
58 - def convertstrings(self, input):
59 if isinstance(input, multistring): 60 strings = input.strings 61 elif isinstance(input, list): 62 strings = input 63 else: 64 return self.convertstring(input) 65 66 for index, string in enumerate(strings): 67 strings[index] = re.sub('\{(\d)\}', lambda x: "%%%d$s" % (int(x.group(1))+1), string) 68 return strings
69
70 -def convertpy2php(inputfile, outputfile, template=None):
71 """Converts from Python .po to PHP .po 72 73 @param inputfile: file handle of the source 74 @param outputfile: file handle to write to 75 @param template: unused 76 """ 77 convertor = pypo2phppo() 78 inputstore = po.pofile(inputfile) 79 outputstore = convertor.convertstore(inputstore) 80 if outputstore.isempty(): 81 return False 82 outputfile.write(str(outputstore)) 83 return True
84
85 -def main(argv=None):
86 """Converts from Python .po to PHP .po""" 87 from translate.convert import convert 88 89 formats = {"po":("po", convertpy2php)} 90 parser = convert.ConvertOptionParser(formats, description=__doc__) 91 parser.run(argv)
92 93 if __name__ == '__main__': 94 main() 95