[pygtk] Translation, gtk labels, and gettext

John Dennis jdennis at redhat.com
Thu Dec 6 23:17:06 WST 2007


Caleb Marcus wrote:
> I'm planning to make my application translatable with gettext. Do I have 
> to make the text in my gtk stock buttons and things like that use 
> gettext, or will they automatically translate due to their use of gtk 
> stock things?

Here are a few quick hints to make life easier. Do not use Python 
unicode strings in your application, use UTF-8 and put it in a normal 
Python string. Why? Because all the libraries you're linking with expect 
UTF-8, not unicode.

Use gettext.install() as soon as your application starts up, that will 
install _() as a global symbol. Tell gettext you're using UTF-8, 
otherwise it will default to unicode and you will have a raft of 
problems. (If you're not providing a program, but rather library modules 
another program will load then the technique is different).

gettext.install(domain    = program_name),
                 unicode   = False,
                 codeset   = 'utf-8')

Then in your application wherever you have a translatable string enclose 
it it _(). For example: button.set_text(_('Press me for a good time'))

Use intltool to produce the .po files and install them.

If you don't know what _() is or .po files then read the first couple of 
chapters of the GNU GetText manual. Hint: any string enclosed in _() 
get's factored out into a translation file, a translator translates it 
(e.g. 'Press me for a good time'), and the translation is put into a .po 
file. At run time gettext.install loads the translation, and _() causes 
the source string (e.g. 'Press me for a good time') to be looked up in 
the translation catalog and replaced with the translated string. The 
translated string is what is actually passed to GTK. If a translation 
can't be found the original string is passed instead. The strings must 
be UTF-8.
-- 
John Dennis <jdennis at redhat.com>


More information about the pygtk mailing list