Package translate :: Package tools :: Module phppo2pypo

[hide private]
[frames] | no frames]

# Source Code for Module translate.tools.phppo2pypo

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