Sign up ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

I've created an application that I'd like to submit through the Ubuntu app developer process. One of the requirements for the process is that when installed, all files go into the /opt/<appname> directory.

Through changes in packaging and with help from others I've achieved that, but now I'm facing a new problem: when running from /opt the translations from my app are not being loaded.

What happens is the following: unless specified otherwise, the binary files (MO files) containing translations for an application are loaded from /usr/share/locale (or in the case of Ubuntu, from /usr/share/locale-langpack), but in my app, due to the /opt requirement, they are installed in /opt/

That's in theory fine, as gettext allows specifying an alternative location to load translations, so I add the bindtextdomain call to achieve that:

import gettext
from gettext import gettext as _
gettext.bindtextdomain('qreator', '/opt/')

That works well up to a point: all messages that are output on the command line then appear translated as expected. But it does not solve the main problem: to load the translations from the UI Glade requires specifying the translation domain once more (the equivalent to the gettext.textdomain('qreator') call above), but it does not let specifying where to load translations from (i.e. the Glade equivalent to bindtextdomain, which sadly does not exist). Here's an extract of the code that I'm using:

builder = Gtk.Builder()
# There isn't a way to tell glade to load translations from
# somewhere else than /usr/share/locale here

This essentially means that translations from the UI cannot be loaded if your app is installed in /opt.

I'm pretty much stuck at this point. Any ideas on how to make Glade load translations from /opt/ instead of /usr/share/locale?

share|improve this question
After talking to someone more knowledgeable on Gtk.Builder, I've filed a bug: – David Planella May 22 '12 at 8:47
I've also been pointed out to a workaround, but it did not seem to work for me - here it is: – David Planella May 22 '12 at 8:48
Could you include a full minimal runnable example in your python code? I'm happy to drill down into this, but I'm not sure what imports I'm missing to get a Gtk.Builder(). – slangasek May 22 '12 at 18:52
n/m, apparently that's just 'import gtk' ;) – slangasek May 22 '12 at 18:55
@slangasek,it's from gi.repository import Gtk, but there's a runnable minimal example attached to the bug report I filed on the first comment. – David Planella May 22 '12 at 21:22

1 Answer 1

up vote 7 down vote accepted

I've found a solution: use the locale Python module instead of gettext

import locale
from locale import gettext as _
locale.bindtextdomain('qreator', '/opt/')

Thanks to Juha Sahakangas on the #gtk+ IRC channel for providing the explanation:

For this particular case the locale module needs to be used instead of gettext. Python's gettext module is pure python, it doesn't actually set the text domain in a way that the C library can read, but locale does (by calling libc). So long as that's done, GtkBuilder already works the way you're asking it to.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.