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

# Source Code for Module translate.convert.web2py2po

     1  #!/usr/bin/env python
     2  # -*- coding: utf-8 -*-
     3  #
     4  # Copyright 2009 Zuza Software Foundation
     5  #
     6  # This file is part of translate.
     7  #
     8  # This program 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  # This program 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 this program; if not, see <http://www.gnu.org/licenses/>.
    20  #
    21  # (c) 2009 Dominic König (dominic@nursix.org)
    22  #
    23
    24  """convert web2py translation dictionaries (.py) to GNU/gettext PO files"""
    25
    26  import sys
    27  from translate.storage import po
    28
    29 -class web2py2po:
    30 -    def __init__(self, pofile=None):
    31          self.mypofile = pofile
    32
    33 -    def convertunit(self, source_str, target_str):
    34          pounit = po.pounit(encoding="UTF-8")
    35          pounit.setsource( source_str )
    36          if target_str:
    37              pounit.settarget( target_str )
    38          return pounit
    39
    40 -    def convertstore(self, mydict):
    41
    42          targetheader = self.mypofile.makeheader(charset="UTF-8", encoding="8bit")
    43          targetheader.addnote("extracted from web2py", "developer")
    44
    45          self.mypofile.addunit(targetheader)
    46
    47          for source_str in mydict.keys():
    48              target_str = mydict[source_str]
    49              if target_str.startswith('*** '):
    50                  target_str = ''
    51              pounit = self.convertunit(source_str, target_str)
    52              self.mypofile.addunit(pounit)
    53
    54          return self.mypofile
    55
    56 -def convertpy(inputfile, outputfile, encoding="UTF-8"):
    57
    58      new_pofile = po.pofile()
    59      convertor = web2py2po(new_pofile)
    60
    61      mydict = eval(inputfile.read())
    62      if not isinstance(mydict, dict):
    63          return 0
    64
    65      outputstore = convertor.convertstore(mydict)
    66
    67      if outputstore.isempty():
    68          return 0
    69
    70      outputfile.write(str(outputstore))
    71      return 1
    72
    73 -def main(argv=None):
    74      from translate.convert import convert
    75      formats = {("py", "po"): ("po", convertpy), ("py", None): ("po", convertpy)}
    76      parser = convert.ConvertOptionParser(formats, usetemplates=False, description=__doc__)
    77      parser.run(argv)
    78
    79  if __name__ == '__main__':
    80      main()
    81