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