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