FAQ Index - Search - Recent Changes - Everything - Add entry

<< Previous Entry | FAQ Entry 22.2 | Next Entry >>

22.2. How do I internationalize a PyGTK and libglade program?

1. Simple PyGTK application

1.1 Prepare your application

 print _('Hello World!')
 s = _('%s: %u / %u\n') % (id, x, y)
1.2 Include gettext support

 APP = 'myapp'
 DIR = 'locale'

 import locale
 import gettext
 locale.setlocale(locale.LC_ALL, '')
 gettext.bindtextdomain(APP, DIR)
 gettext.textdomain(APP)
 _ = gettext.gettext
1.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

 xgettext -k_ -kN_ -o messages.pot *.py

 LANG=de_DE msginit

 msgmerge -U de.po messages.pot

 mkdir -p locale/de/LC_MESSAGES/
 msgfmt de.po -o locale/de/LC_MESSAGES/myapp.mo
4. Thats it - now use your application!

 # start without any translation
 LANG=de_DE python myapp.py
2 Translating glade files

2.1 Extracting strings

 intltool-extract --type=gettext/glade foo.glade
This way is prefered and will also take into account and will not include the strings that were not marked for translation

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

 widgets = gtk.glade.XML(glade_file, widget_name, APP)

 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-XX
where XX should be replaced with the first two letter code, eg es for spanish and sv for swedish.

PyGTK FAQ Wizard | PyGTK Homepage | Feedback to faq at pygtk.org