Package translate :: Package convert :: Module php2po
[hide private]
[frames] | no frames]

Source Code for Module translate.convert.php2po

  1  #!/usr/bin/env python 
  2  # -*- coding: utf-8 -*- 
  3  # 
  4  # Copyright 2002-2006 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 PHP localization files to Gettext PO localization files 
 23   
 24  See: http://translate.sourceforge.net/wiki/toolkit/php2po for examples and 
 25  usage instructions 
 26  """ 
 27   
 28  import sys 
 29   
 30  from translate.storage import po 
 31  from translate.storage import php 
 32   
 33   
34 -class php2po:
35 """convert a .php file to a .po file for handling the translation...""" 36
37 - def convertstore(self, inputstore, duplicatestyle="msgctxt"):
38 """converts a .php file to a .po file...""" 39 outputstore = po.pofile() 40 outputheader = outputstore.init_headers(charset="UTF-8", encoding="8bit") 41 outputheader.addnote("extracted from %s" % inputstore.filename, "developer") 42 43 for inputunit in inputstore.units: 44 outputunit = self.convertunit(inputunit, "developer") 45 if outputunit is not None: 46 outputstore.addunit(outputunit) 47 outputstore.removeduplicates(duplicatestyle) 48 return outputstore
49
50 - def mergestore(self, templatestore, inputstore, blankmsgstr=False, duplicatestyle="msgctxt"):
51 """converts two .properties files to a .po file...""" 52 outputstore = po.pofile() 53 outputheader = outputstore.init_headers(charset="UTF-8", encoding="8bit") 54 outputheader.addnote("extracted from %s, %s" % (templatestore.filename, inputstore.filename), "developer") 55 56 inputstore.makeindex() 57 # loop through the original file, looking at units one by one 58 for templateunit in templatestore.units: 59 outputunit = self.convertunit(templateunit, "developer") 60 # try and find a translation of the same name... 61 if templateunit.name in inputstore.locationindex: 62 translatedinputunit = inputstore.locationindex[templateunit.name] 63 # Need to check that this comment is not a copy of the developer comments 64 translatedoutputunit = self.convertunit(translatedinputunit, "translator") 65 else: 66 translatedoutputunit = None 67 # if we have a valid po unit, get the translation and add it... 68 if outputunit is not None: 69 if translatedoutputunit is not None and not blankmsgstr: 70 outputunit.target = translatedoutputunit.source 71 outputstore.addunit(outputunit) 72 elif translatedoutputunit is not None: 73 print >> sys.stderr, "error converting original properties definition %s" % templateunit.name 74 outputstore.removeduplicates(duplicatestyle) 75 return outputstore
76
77 - def convertunit(self, inputunit, origin):
78 """Converts a .php unit to a .po unit""" 79 outputunit = po.pounit(encoding="UTF-8") 80 outputunit.addnote(inputunit.getnotes(origin), origin) 81 outputunit.addlocation("".join(inputunit.getlocations())) 82 outputunit.source = inputunit.source 83 outputunit.target = "" 84 return outputunit
85 86
87 -def convertphp(inputfile, outputfile, templatefile, pot=False, duplicatestyle="msgctxt"):
88 """reads in inputfile using php, converts using php2po, writes to outputfile""" 89 inputstore = php.phpfile(inputfile) 90 convertor = php2po() 91 if templatefile is None: 92 outputstore = convertor.convertstore(inputstore, duplicatestyle=duplicatestyle) 93 else: 94 templatestore = php.phpfile(templatefile) 95 outputstore = convertor.mergestore(templatestore, inputstore, blankmsgstr=pot, duplicatestyle=duplicatestyle) 96 if outputstore.isempty(): 97 return 0 98 outputfile.write(str(outputstore)) 99 return 1
100 101
102 -def main(argv=None):
103 from translate.convert import convert 104 formats = {"php": ("po", convertphp), ("php", "php"): ("po", convertphp)} 105 parser = convert.ConvertOptionParser(formats, usetemplates=True, usepots=True, description=__doc__) 106 parser.add_duplicates_option() 107 parser.passthrough.append("pot") 108 parser.run(argv)
109 110 111 if __name__ == '__main__': 112 main() 113