Create a gist now

Instantly share code, notes, and snippets.

A Python script for swaping source and target language on a Gettext PO file.
#!/usr/bin/env python
# poswap.py
#
# Copyright (C) 2013 Santiago M. Mola
# Released under the terms of the MIT License.
#
"""
Swaps the source and target language in a Gettext PO file.
Learn more at http://mola.io/2013/09/17/swapping-languages-in-gettext-po-file
"""
import sys
try:
from translate.storage.pypo import pofile
except ImportError:
print 'This script requires Translate Toolkit.'
print 'Download it at http://toolkit.translatehouse.org/'
sys.exit(1)
if len(sys.argv) != 2:
print 'USAGE: %s <PO file>' % sys.argv[0]
sys.exit(1)
filename = sys.argv[1]
po = pofile(open(filename, 'r'))
# Swap every unit, except the first one,
# which should be the gettext header.
for i in po.units[1:]:
source = i.getsource()
target = i.gettarget()
i.setsource(target)
i.settarget(source)
print po
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment