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  from translate.storage import po
    27
    28 -class web2py2po:
    29 -    def __init__(self, pofile=None):
    30          self.mypofile = pofile
    31
    32 -    def convertunit(self, source_str, target_str):
    33          pounit = po.pounit(encoding="UTF-8")
    34          pounit.setsource( source_str )
    35          if target_str:
    36              pounit.settarget( target_str )
    37          return pounit
    38
    39 -    def convertstore(self, mydict):
    40
    41          targetheader = self.mypofile.init_headers(charset="UTF-8", encoding="8bit")
    42          targetheader.addnote("extracted from web2py", "developer")
    43
    44          for source_str in mydict.keys():
    45              target_str = mydict[source_str]
    46              if target_str.startswith('*** '):
    47                  target_str = ''
    48              pounit = self.convertunit(source_str, target_str)
    49              self.mypofile.addunit(pounit)
    50
    51          return self.mypofile
    52
    53 -def convertpy(inputfile, outputfile, encoding="UTF-8"):
    54
    55      new_pofile = po.pofile()
    56      convertor = web2py2po(new_pofile)
    57
    58      mydict = eval(inputfile.read())
    59      if not isinstance(mydict, dict):
    60          return 0
    61
    62      outputstore = convertor.convertstore(mydict)
    63
    64      if outputstore.isempty():
    65          return 0
    66
    67      outputfile.write(str(outputstore))
    68      return 1
    69
    70 -def main(argv=None):
    71      from translate.convert import convert
    72      formats = {("py", "po"): ("po", convertpy), ("py", None): ("po", convertpy)}
    73      parser = convert.ConvertOptionParser(formats, usetemplates=False, description=__doc__)
    74      parser.run(argv)
    75
    76  if __name__ == '__main__':
    77      main()
    78