GTK 中多国语言

来源:互联网 发布:淘宝上在哪里投诉快递 编辑:程序博客网 时间:2024/04/29 10:42
 

1. In source code
#include<gtk/gtk.h>
#include<libintl.h>
#define _(STRING) gettext(STRING)
#define PACKAGE "record_zh"   //will build record_zh.mo
#define LOCALEDIR "./mo"     

int main(int argc, char *argv[])
{
    ...
    bindtextdomain(PACKAGE, LOCALEDIR);
    textdomain(PACKAGE);
    bind_textdomain_codeset(PACKAGE, "UTF-8");
   
    gtk_init(&argc, &argv);
    ...
}

2. Build po file
#xgettext -k_ -o ui_pref.pot *.c *.h
#cp ui_pref.pot zh_CN.po
#vim zh_CN.po
change the zh_CN.po file head "charset=CHARSET" to "charset=UTF-8"
Change capital string part in head if you need.
translate msgid and write then in msgstr
eg, in zh_CN.po:
...
#callback.c:67
msgid "Delete record failure/n"
msgstr "删除纪录失败/n"
...

3. Make .mo file
#msgfmt -o record_zh.mo zh_CN.po

4. Copy .mo to right directly
#mkdir -p mo/zh_CN/LC_MESSAGES
#cp record_zh.mo mo/zh_CN/LC_MESSAGES

You also can copy .mo to /usr/share/locale/zh_CN/LC_MESSAGES/
then your main file need remove lines

#define LOCALEDIR "./mo"

bindtextdomain(PACKAGE, LOCALEDIR);

5. Do test
#export LANG=zh_CN.UTF-8
run your program


When you run your program, if show below information

Gtk-WARNING **: Locale not supported by c library
        Using the fallback 'C' locale

You can use "locale -a" check the language
#vim /var/lib/locales/supported.d/local
Add "zh_CN.UTF-8 UTF-8"
The other support language you can see /usr/share/i18n/SUPPORTED

#sudo locale-gen
Then you can see a zh_CN.utf8 in /usr/lib/locale/

原创粉丝点击