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
     42 -def swapdir(store):
     43      """Swap the source and target of each unit."""
     44      for unit in store.units:
     45          if unit.isheader():
     46              continue
     47          if not unit.target or unit.isfuzzy():
     48              unit.target = unit.source
     49          else:
     50              unit.source, unit.target = unit.target, unit.source
     51
     52
     53 -def convertpo(inputpofile, outputpotfile, template, reverse=False):
     54      """reads in inputpofile, removes the header, writes to outputpotfile."""
     55      inputpo = po.pofile(inputpofile)
     56      templatepo = po.pofile(template)
     57      if reverse:
     58          swapdir(inputpo)
     59      templatepo.makeindex()
     60      header = inputpo.header()
     61      if header:
     62          inputpo.units = inputpo.units[1:]
     63
     64      for i, unit in enumerate(inputpo.units):
     65          for location in unit.getlocations():
     66              templateunit = templatepo.locationindex.get(location, None)
     67              if templateunit and templateunit.source == unit.source:
     68                  break
     69          else:
     70              templateunit = templatepo.findunit(unit.source)
     71
     72          unit.othercomments = []
     73          if unit.target and not unit.isfuzzy():
     74              unit.source = unit.target
     75          elif not reverse:
     76              if inputpo.filename:
     77                  unit.addnote("No translation found in %s" % inputpo.filename, origin="programmer")
     78              else:
     79                  unit.addnote("No translation found in the supplied source language", origin="programmer")
     80          unit.target = ""
     81          unit.markfuzzy(False)
     82          if templateunit:
     83              unit.addnote(templateunit.getnotes(origin="translator"))
     84              unit.markfuzzy(templateunit.isfuzzy())
     85              unit.target = templateunit.target
     86          if unit.isobsolete():
     87              del inputpo.units[i]
     88      outputpotfile.write(str(inputpo))
     89      return 1
     90
     91
     92 -def main(argv=None):
     93      formats = {("po", "po"): ("po", convertpo), ("po", "pot"): ("po", convertpo), "po": ("po", convertpo)}
     94      parser = convert.ConvertOptionParser(formats, usetemplates=True, description=__doc__)
     95      parser.add_option("", "--reverse", dest="reverse", default=False, action="store_true",
     96                      help="reverse the process of intermediate language conversion")
     97      parser.passthrough.append("reverse")
     98      parser.run(argv)
     99
    100
    101  if __name__ == '__main__':
    102      main()
    103