With gettext
you can either use the default system-wide locale directory, or specify one yourself using bindtextdomain
. This is useful when running a program directly from source when the compiled .mo translation files are not available in the system's default location.
In Python you would do this:
import gettext
from gettext import gettext as _
gettext.bindtextdomain('nautilus-image-manipulator', '/path/to/mo/folder')
gettext.textdomain('nautilus-image-manipulator')
where /path/to/mo/folder
contains the familiar fr/LC_MESSAGES/nautilus-image-manipulator.mo
structure. Calls like this:
print _("Delete this profile")
return the properly translated string from the local .mo files, thank you very much.
In GTK+2/pygtk, there existed gtk.glade.bindtextdomain
, but I'm wondering if there is any equivalent in GTK+3/PyGObject.
To give you a specific example, this is how Nautilus Image Manipulator;s UI is created from its Glade file:
from gi.repository import Gtk
builder = Gtk.Builder()
builder.set_translation_domain('nautilus-image-manipulator')
builder.add_from_file(ui_filename)
return builder
Parts of the UI that are not built from the Glade file (i.e. set from code) show up properly translated, but the strings from the Glade file still show up in English.
It seems to me that I'm missing a call to some kind of builder.bind_text_domain('nautilus-image-manipulator', '/path/to/mo/folder')
before the call to builder.set_translation_domain
... Any idea how to perform this?
In PyGtk you can use Gtk.Builder too. Accordingly to the PyGtk Gtk.Builder documentation:
http://developer.gnome.org/pygtk/stable/class-gtkbuilder.html#properties-gtkbuilder
That is, Gtk.Builder uses dgettext() from "C library". The problem is that Python's gettext module, function bindtextdomain(), for some reason unknown to me, don't set the "C library". The option is to use the locale module that also exposes that interface. From the Python locale module documentation:
http://docs.python.org/library/locale#access-to-message-catalogs
Which, is the current case. What a hack :S
This will do it, file test.py:
My Glade file test.glade:
Remember to create the mo in mo/LANG/LC_MESSAGES/myapp.mo based on .po extracted with:
What it looks like:
Kind regards
Well after the bounty failing so miserably to attract a mac os x answer, I had to do my own research. Here's the snippet I use:
Later, when you have the Gtk.Builder, set the domain:
This will only work if gettext's library
libintl
is in the library path, otherwise fail gracefully. For transaltions to work, you will need to install gettext as a dependency.The solution to activate gettext translations in Gtk / python under Windows is elib_intl.py It is easy to find the file with Google. This allows translation of text in the code and text in a glade UI.
Here is the code used for the following environment :
Windows 7 Python 2.7 Gtk 3+ loaded by : pygi-aio-3.10.2-win32_rev18-setup.exe
It should work in any windows and also for Python 3. elib_intl.py can be used with pyGtk (Gtk 2).
If you are using Gtk 3, you will probably receive an error : for line 447 :
This error indicates : module not found. The reason is that in Gtk3, the name of the dll has been changed. It is no longer intl.dll. In the described Pygi installation the name is : libintl-8. You must replace the line which provoques the error by :
You can find a full working example here : pdfBooklet 2.4.0 (Warning : not yet in line when I write)
thanks to dieter Verfaillie who has written elib_intl