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