9

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/extras.ubuntu.com/<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/extras.ubuntu.com/qreator/locale.

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/extras.ubuntu.com/qreator/share/locale/')
gettext.textdomain('qreator')

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()
builder.set_translation_domain('qreator')
# There isn't a way to tell glade to load translations from
# somewhere else than /usr/share/locale here
builder.add_from_file(ui_filename)

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/extras.ubuntu.com/qreator/share/locale/ instead of /usr/share/locale?

7
  • After talking to someone more knowledgeable on Gtk.Builder, I've filed a bug: bugzilla.gnome.org/show_bug.cgi?id=676543 May 22, 2012 at 8:47
  • I've also been pointed out to a workaround, but it did not seem to work for me - here it is: bugs.debian.org/cgi-bin/bugreport.cgi?bug=114461#12 May 22, 2012 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, 2012 at 18:52
  • n/m, apparently that's just 'import gtk' ;)
    – slangasek
    May 22, 2012 at 18:55
  • 1
    right - I give up, there seems to be no sane way to get gettext.bindtextdomain() to take hold for glade :/
    – slangasek
    May 22, 2012 at 22:38

1 Answer 1

10

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

import locale
from locale import gettext as _
locale.bindtextdomain('qreator', '/opt/extras.ubuntu.com/qreator/share/locale/')
locale.textdomain('qreator')

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .