|FAQ Index - Search - Recent Changes - Everything - Add entry|
22.2. How do I internationalize a PyGTK and libglade program?
1.1 Prepare your application
- Use _('') at all places where you handle user-visible strings. Please avoid non-ASCII letters.
print _('Hello World!') s = _('%s: %u / %u\n') % (id, x, y)1.2 Include gettext support
- You will have to choose a non-ambiguous name for your application (or library), let's say "myapp". Furthermore the translated message-files will have to be stored somewhere, the default is often "/usr/share/locale". When working with a not installed application its though much easier to have them lying in a local subdirectory, let's choose "locale".
APP = 'myapp' DIR = 'locale'
- Use the gettext module, which provides the well known basic gettext API, but has some new features. It's easy to use this module for internationalization of your application:
import locale import gettext locale.setlocale(locale.LC_ALL, '') gettext.bindtextdomain(APP, DIR) gettext.textdomain(APP) _ = gettext.gettext1.3. Create the translations
Please note that the command line tools used in this example requires gettext installed. See* [www.gnu.org] for more information
- Extract the strings to translate from the source files, repeat this after changing the strings in your code. If using intltool you should combine the messages.pot that xgettext produces with the pot-file that intltool generates.
xgettext -k_ -kN_ -o messages.pot *.py
- Whenever someone wants to start a new translation, you have to create a file for it. This is easily done by "msginit", for example the following line will create and setup a de_DE (eg German spoken in Germany) translation file "de.po".
- Translation is done by editing such *.po files, have a look at them and the gettext documentation about this.
- Now imagine "de.po" already contains a full translation but the code changes. You will then have to merge the existing translation with new and changed translation strings:
msgmerge -U de.po messages.pot
- The *.po files contain human readable entrys, they have to be prepared for usage by the real application:
mkdir -p locale/de/LC_MESSAGES/ msgfmt de.po -o locale/de/LC_MESSAGES/myapp.mo4. Thats it - now use your application!
# start without any translation LANG=de_DE python myapp.py2 Translating glade files
2.1 Extracting strings
- Use intltool to extract translatable strings directly from the glade files:
intltool-extract --type=gettext/glade foo.gladeThis way is prefered and will also take into account and will not include the strings that were not marked for translation
- Alternatively, to extract strings within Glade: go to the "LibGlade Options" page in your projects options and enable "Save Translatable Strings". Choose a nice filename like "glade-msg.c". Yes - ".c"! Glade will create a C pseudo source file with all the messages in it whenever you save your project. (Please don't compile this file or use it somehow or other without knowing what you do...)
Include *.c as an argument to xgettext if you chose to have glade generate .c file instead of using intltool. In case of intltool-extract add *.glade.h instead of the .c file
2.2 Using the right domain
- Change your code so that when you instantiate gtk.glade.XML(), you include a parameter specifying the textdomain:
widgets = gtk.glade.XML(glade_file, widget_name, APP)
- Or you can bind a textdomain so you don't have to specify it every time. This also allows you to work with translations stored in a local directory. Just change this part of you general setup:
gettext.bindtextdomain(APP, DIR) gettext.textdomain(APP)into this:
for module in (gettext, gtk.glade): module.bindtextdomain(APP_NAME, LOCALE_DIR) module.textdomain(APP_NAME)3.0 Installing .mo files libraries provided by the operating system
In Ubuntu you need to install a separate package for each language you wish to support. The packages contains the .mo files for glibc,gtk+ etc. The name of the packages in ubuntu are
language-pack-XX language-pack-gnome-XXwhere XX should be replaced with the first two letter code, eg es for spanish and sv for swedish.