Package translate :: Package tools :: Module poswap |
[hide private]
[frames] | no frames]

# Source Code for Module translate.tools.poswap

      1  #!/usr/bin/env python
      2  # -*- coding: utf-8 -*-
      3  #
      4  # Copyright 2007 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  """Builds a new translation file with the target of the input language as
     23  source language.
     24
     25  Ensure that the two po files correspond 100% to the same pot file before using
     26  this.
     27
     28  To translate Kurdish (ku) through French::
     29      po2swap -i fr/ -t ku -o fr-ku
     30
     31  To convert the fr-ku files back to en-ku::
     32      po2swap --reverse -i fr/ -t fr-ku -o en-ku
     33
     34  See: http://translate.sourceforge.net/wiki/toolkit/poswap for further examples and
     35  usage instructions
     36  """
     37
     38  from translate.storage import po
     39  from translate.convert import convert
     40
     41 -def swapdir(store):
     42      """Swap the source and target of each unit."""
     43      for unit in store.units:
     44          if unit.isheader():
     45              continue
     46          if not unit.target or unit.isfuzzy():
     47              unit.target = unit.source
     48          else:
     49              unit.source, unit.target = unit.target, unit.source
     50
     51 -def convertpo(inputpofile, outputpotfile, template, reverse=False):
     52      """reads in inputpofile, removes the header, writes to outputpotfile."""
     53      inputpo = po.pofile(inputpofile)
     54      templatepo = po.pofile(template)
     55      if reverse:
     56          swapdir(inputpo)
     57      templatepo.makeindex()
     58      header = inputpo.header()
     59      if header:
     60          inputpo.units = inputpo.units[1:]
     61
     62      for i, unit in enumerate(inputpo.units):
     63          for location in unit.getlocations():
     64              templateunit = templatepo.locationindex.get(location, None)
     65              if templateunit and templateunit.source == unit.source:
     66                  break
     67          else:
     68              templateunit = templatepo.findunit(unit.source)
     69
     70          unit.othercomments = []
     71          if unit.target and not unit.isfuzzy():
     72              unit.source = unit.target
     73          elif not reverse:
     74              if inputpo.filename:
     75                  unit.addnote("No translation found in %s" % inputpo.filename, origin="programmer")
     76              else:
     77                  unit.addnote("No translation found in the supplied source language", origin="programmer")
     78          unit.target = ""
     79          unit.markfuzzy(False)
     80          if templateunit:
     81              unit.addnote(templateunit.getnotes(origin="translator"))
     82              unit.markfuzzy(templateunit.isfuzzy())
     83              unit.target = templateunit.target
     84          if unit.isobsolete():
     85              del inputpo.units[i]
     86      outputpotfile.write(str(inputpo))
     87      return 1
     88
     89 -def main(argv=None):
     90      formats = {("po", "po"): ("po", convertpo), ("po", "pot"): ("po", convertpo), "po": ("po", convertpo)}
     91      parser = convert.ConvertOptionParser(formats, usetemplates=True, description=__doc__)
     92      parser.add_option("", "--reverse", dest="reverse", default=False, action="store_true",
     93                      help="reverse the process of intermediate language conversion")
     94      parser.passthrough.append("reverse")
     95      parser.run(argv)
     96
     97  if __name__ == '__main__':
     98      main()
     99