1   
  2   
  3   
  4   
  5   
  6   
  7   
  8   
  9   
 10   
 11   
 12   
 13   
 14   
 15   
 16   
 17   
 18   
 19   
 20   
 21   
 22  """convert Java/Mozilla .properties files to Gettext PO localization files 
 23   
 24  See: http://translate.sourceforge.net/wiki/toolkit/prop2po for examples and  
 25  usage instructions 
 26  """ 
 27   
 28  import sys 
 29  from translate.storage import po 
 30  from translate.storage import properties 
 31   
 33      """convert a .properties file to a .po file for handling the translation...""" 
 34 -    def convertstore(self, thepropfile, duplicatestyle="msgctxt"): 
  35          """converts a .properties file to a .po file...""" 
 36          thetargetfile = po.pofile() 
 37          targetheader = thetargetfile.makeheader(charset="UTF-8", encoding="8bit", x_accelerator_marker="&") 
 38          targetheader.addnote("extracted from %s" % thepropfile.filename, "developer") 
 39           
 40          appendedheader = 0 
 41          waitingcomments = [] 
 42          for propunit in thepropfile.units: 
 43              pounit = self.convertunit(propunit, "developer") 
 44              if pounit is None: 
 45                  waitingcomments.extend(propunit.comments) 
 46               
 47              if pounit is "discard": 
 48                  continue 
 49              if not appendedheader: 
 50                  if propunit.isblank(): 
 51                      pounit = targetheader 
 52                  else: 
 53                      thetargetfile.addunit(targetheader) 
 54                  appendedheader = 1 
 55              if pounit is not None: 
 56                  pounit.addnote("".join(waitingcomments).rstrip(), "developer", position="prepend") 
 57                  waitingcomments = [] 
 58                  thetargetfile.addunit(pounit) 
 59          thetargetfile.removeduplicates(duplicatestyle) 
 60          return thetargetfile 
  61   
 62 -    def mergestore(self, origpropfile, translatedpropfile, blankmsgstr=False, duplicatestyle="msgctxt"): 
  63          """converts two .properties files to a .po file...""" 
 64          thetargetfile = po.pofile() 
 65          targetheader = thetargetfile.makeheader(charset="UTF-8", encoding="8bit") 
 66          targetheader.addnote("extracted from %s, %s" % (origpropfile.filename, translatedpropfile.filename), "developer") 
 67          translatedpropfile.makeindex() 
 68           
 69          appendedheader = 0 
 70          waitingcomments = [] 
 71           
 72          for origprop in origpropfile.units: 
 73              origpo = self.convertunit(origprop, "developer") 
 74              if origpo is None: 
 75                  waitingcomments.extend(origprop.comments) 
 76               
 77              if origpo is "discard": 
 78                  continue 
 79               
 80              if not appendedheader: 
 81                  if origprop.isblank(): 
 82                      origpo = targetheader 
 83                  else: 
 84                      thetargetfile.addunit(targetheader) 
 85                  appendedheader = 1 
 86               
 87              if origprop.name in translatedpropfile.locationindex: 
 88                  translatedprop = translatedpropfile.locationindex[origprop.name] 
 89                   
 90                  translatedpo = self.convertunit(translatedprop, "translator") 
 91              else: 
 92                  translatedpo = None 
 93               
 94              if origpo is not None: 
 95                  if translatedpo is not None and not blankmsgstr: 
 96                      origpo.target = translatedpo.source 
 97                  origpo.addnote("".join(waitingcomments).rstrip(), "developer", position="prepend") 
 98                  waitingcomments = [] 
 99                  thetargetfile.addunit(origpo) 
100              elif translatedpo is not None: 
101                  print >> sys.stderr, "error converting original properties definition %s" % origprop.name 
102          thetargetfile.removeduplicates(duplicatestyle) 
103          return thetargetfile 
 104   
 124   
125 -def convertprop(inputfile, outputfile, templatefile, pot=False, duplicatestyle="msgctxt"): 
 126      """reads in inputfile using properties, converts using prop2po, writes to outputfile""" 
127      inputstore = properties.propfile(inputfile) 
128      convertor = prop2po() 
129      if templatefile is None: 
130          outputstore = convertor.convertstore(inputstore, duplicatestyle=duplicatestyle) 
131      else: 
132          templatestore = properties.propfile(templatefile) 
133          outputstore = convertor.mergestore(templatestore, inputstore, blankmsgstr=pot, duplicatestyle=duplicatestyle) 
134      if outputstore.isempty(): 
135          return 0 
136      outputfile.write(str(outputstore)) 
137      return 1 
 138   
139 -def main(argv=None): 
 140      from translate.convert import convert 
141      formats = {"properties": ("po", convertprop), ("properties", "properties"): ("po", convertprop)} 
142      parser = convert.ConvertOptionParser(formats, usetemplates=True, usepots=True, description=__doc__) 
143      parser.add_duplicates_option() 
144      parser.passthrough.append("pot") 
145      parser.run(argv) 
 146   
147  if __name__ == '__main__': 
148      main() 
149