Package translate :: Package convert :: Module po2prop

[hide private]
[frames] | no frames]

# Source Code for Module translate.convert.po2prop

      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
     23  """convert Gettext PO localization files to Java/Mozilla .properties files
     24
     25  see: http://translate.sourceforge.net/wiki/toolkit/po2prop for examples and
     26  usage instructions
     27  """
     28
     29  from translate.misc import quote
     30  from translate.storage import po
     31
     32  eol = "\n"
     33
     34 -class reprop:
     35 -    def __init__(self, templatefile):
     36          self.templatefile = templatefile
     37          self.inputdict = {}
     38
     39 -    def convertstore(self, inputstore, personality, includefuzzy=False):
     40          self.personality = personality
     41          self.inmultilinemsgid = 0
     42          self.inecho = 0
     43          self.makestoredict(inputstore, includefuzzy)
     44          outputlines = []
     45          for line in self.templatefile.readlines():
     46              outputstr = self.convertline(line)
     47              outputlines.append(outputstr)
     48          return outputlines
     49
     50 -    def makestoredict(self, store, includefuzzy=False):
     51          # make a dictionary of the translations
     52          for unit in store.units:
     53              if includefuzzy or not unit.isfuzzy():
     54                  # there may be more than one entity due to msguniq merge
     55                  for entity in unit.getlocations():
     56                      propstring = unit.target
     57
     58                      # NOTE: triple-space as a string means leave it empty (special signal)
     59                      if len(propstring.strip()) == 0 and propstring != "   ":
     60                          propstring = unit.source
     61                      self.inputdict[entity] = propstring
     62
     63 -    def convertline(self, line):
     64          returnline = ""
     65          # handle multiline msgid if we're in one
     66          if self.inmultilinemsgid:
     67              msgid = quote.rstripeol(line).strip()
     68              # see if there's more
     69              self.inmultilinemsgid = (msgid[-1:] == '\\')
     70              # if we're echoing...
     71              if self.inecho:
     72                  returnline = line
     73          # otherwise, this could be a comment
     74          elif line.strip()[:1] == '#':
     75              returnline = quote.rstripeol(line)+eol
     76          else:
     77              line = quote.rstripeol(line)
     78              equalspos = line.find('=')
     79              # if no equals, just repeat it
     80              if equalspos == -1:
     81                  returnline = quote.rstripeol(line)+eol
     82              # otherwise, this is a definition
     83              else:
     84                  # backslash at end means carry string on to next line
     85                  if quote.rstripeol(line)[-1:] == '\\':
     86                      self.inmultilinemsgid = 1
     87                  # now deal with the current string...
     88                  key = line[:equalspos].strip()
     89                  # Calculate space around the equal sign
     90                  prespace = line.lstrip()[line.lstrip().find(' '):equalspos]
     91                  postspacestart = len(line[equalspos+1:])
     92                  postspaceend = len(line[equalspos+1:].lstrip())
     93                  postspace = line[equalspos+1:equalspos+(postspacestart-postspaceend)+1]
     94                  if self.inputdict.has_key(key):
     95                      self.inecho = 0
     96                      value = self.inputdict[key]
     97                      if isinstance(value, str):
     98                          value = value.decode('utf8')
     99                      if self.personality == "mozilla":
    100                          returnline = key+prespace+"="+postspace+quote.mozillapropertiesencode(value)+eol
    101                      else:
    102                          returnline = key+prespace+"="+postspace+quote.javapropertiesencode(value)+eol
    103                  else:
    104                      self.inecho = 1
    105                      returnline = line+eol
    106          if isinstance(returnline, unicode):
    107              returnline = returnline.encode('utf-8')
    108          return returnline
    109
    110 -def convertmozillaprop(inputfile, outputfile, templatefile, includefuzzy=False):
    111      """Mozilla specific convertor function"""
    112      return convertprop(inputfile, outputfile, templatefile, personality="mozilla", includefuzzy=includefuzzy)
    113
    114 -def convertprop(inputfile, outputfile, templatefile, personality, includefuzzy=False):
    115      inputstore = po.pofile(inputfile)
    116      if templatefile is None:
    117          raise ValueError("must have template file for properties files")
    118          # convertor = po2prop()
    119      else:
    120          convertor = reprop(templatefile)
    121      outputproplines = convertor.convertstore(inputstore, personality, includefuzzy)
    122      outputfile.writelines(outputproplines)
    123      return 1
    124
    125 -def main(argv=None):
    126      # handle command line options
    127      from translate.convert import convert
    128      formats = {("po", "properties"): ("properties", convertprop)}
    129      parser = convert.ConvertOptionParser(formats, usetemplates=True, description=__doc__)
    130      parser.add_option("", "--personality", dest="personality", default="java", type="choice",
    131              choices=["java", "mozilla"],
    132              help="set the output behaviour: java (default), mozilla", metavar="TYPE")
    133      parser.add_fuzzy_option()
    134      parser.passthrough.append("personality")
    135      parser.run(argv)
    136
    137  if __name__ == '__main__':
    138      main()
    139