Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1393132
  • 博文数量: 343
  • 博客积分: 13098
  • 博客等级: 上将
  • 技术积分: 2862
  • 用 户 组: 普通用户
  • 注册时间: 2005-07-06 00:35
文章存档

2012年(131)

2011年(31)

2010年(53)

2009年(23)

2008年(62)

2007年(2)

2006年(36)

2005年(5)

分类: Python/Ruby

2011-02-16 00:06:49

1. Simple PyGTK application

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.gettext
1.3. Create the translations

Please note that the command line tools used in this example requires gettext installed. See*  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".

 LANG=de_DE msginit
  • 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.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

  • Use intltool to extract translatable strings directly from the glade files:

 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

  • 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-XX
where XX should be replaced with the first two letter code, eg es for spanish and sv for swedish.
阅读(1086) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~