2 # -*- coding: utf-8 -*-
4 # Copyright 2007 Zuza Software Foundation
6 # This file is part of translate.
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.
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.
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
22 """Builds a new translation file with the target of the input language as
25 Ensure that the two po files correspond 100% to the same pot file before using
28 To translate Kurdish (ku) through French:
29 po2swap -i fr/ -t ku -o fr-ku
31 To convert the fr-ku files back to en-ku:
32 po2swap --reverse -i fr/ -t fr-ku -o en-ku
34 See: http://translate.sourceforge.net/wiki/toolkit/poswap for further examples and
38 from translate
.storage
import po
39 from translate
.convert
import convert
42 """Swap the source and target of each unit."""
43 for unit
in store
.units
:
46 if not unit
.target
or unit
.isfuzzy():
47 unit
.target
= unit
.source
49 unit
.source
, unit
.target
= unit
.target
, unit
.source
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
)
57 templatepo
.makeindex()
58 header
= inputpo
.header()
60 inputpo
.units
= inputpo
.units
[1:]
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
:
68 templateunit
= templatepo
.findunit(unit
.source
)
70 unit
.othercomments
= []
71 if unit
.target
and not unit
.isfuzzy():
72 unit
.source
= unit
.target
75 unit
.addnote("No translation found in %s" % inputpo
.filename
, origin
="programmer")
77 unit
.addnote("No translation found in the supplied source language", origin
="programmer")
81 unit
.addnote(templateunit
.getnotes(origin
="translator"))
82 unit
.markfuzzy(templateunit
.isfuzzy())
83 unit
.target
= templateunit
.target
86 outputpotfile
.write(str(inputpo
))
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")
97 if __name__
== '__main__':