c语言程序写的时候字符串用英语,基本固定了。那么如果想让别的语言的人使用(比如中文),就得把程序里面的字符串挨个翻译,最后再重新编译一次程序维护起来特别麻烦。那么有什么办法能够让程序显示中文?GNU的gettext项目就是用来做这个的。这篇文章以c语言为例介绍了国际化的gettext。
请移步到小宇的博客获取更多技术文章
术语
.po文件: po,Portable Object,里面记录了翻译内容。
.mo文件: mo,Machine Object,其实是一个二进制文件,用来提供给程序使用。
xgettext:一个工具,根据c文件生成po文件
msgfmt: 一个工具,根据po文件生成mo文件
msgmerge:更新po文件
基本过程
xgettext 翻译 msgfmt INSTALL
.c文件----------->.pot文件---------->.po---------->.mo------------>./bin/po/zh/LC_MESSAGES/.mo
gcc INSTALL
.c文件----------->可执行文件-------------------------------------->./bin
准备
在程序中添加这么几行
#include <libintl.h>
#include<locale.h>
#define _(String) gettext (String)
#define gettext_noop(String) String
#define N_(String) gettext_noop (String)
int main()
{
...
setlocale (LC_ALL, "");
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);
...
}