翻译:高莹婷
Gettexta是GNU国际化(i18n)的函数库,并且是开源技术标准。翻译人员了解该函数库后,就可以应用与其相关的大量工具。本文将提供关于开始进行项目国际化工作的关键步骤的简短概要。
注意:如果你正准备开始一个新项目,那么Linux项目生成器(Linux Project Generator)将帮助你正确地建立支持项目国际化的自动安装文件。
改变项目代码
第一步:升级主程序的头文件
使用main()函数,在C模块的前面添加下面内容:
#ifdef USE_GETTEXT
#include "libintl.h"
#include "locale.h"
#define _(String) gettext (String)
#else
#define _(String) String
#endif
第二步:主函数
在你的main()函数的开始添加以下内容:
#ifdef USE_GETTEXT
setlocale (LC_MESSAGES, "");
setlocale (LC_CTYPE, "");
setlocale (LC_COLLATE, "");
textdomain ("my-program");
bindtextdomain ("my-program", NULL);
#endif
第三步:升级头文件
在每个国际化字符串的前面添加以下内容:
#ifdef USE_GETTEXT
#include "libintl.h"
#define _(String) gettext (String)
#else
#define _(String) String
#endif
第四步:字符串前缀
在写字符串代码的时候,使用格式: _( <string> )。例如,将下列表达式:
(Not internationalized)
printf ("some string");
替代为:
(Internationalized)
printf (_("some string") )
第五步:建立标记
使用-DUSE_GETTEXT指令编译程序,或者添加下面的内容在主头文件:
#define USE_GETTEXT
第六步:生成pot及po文件
pot文件包含了可以定域的(可移植的)字符串列表。pot文件通常是一个项目的标识,甚至可以是多个项目的。
创建pot文件:
xgettext -k_ -o my-program.pot *.c *.h --from-code=iso-8859-1
The .pot file that is generated from the above command is the template that contains the English strings and a corresponding empty string field for the translator to add the translation. Once the translations are complete, the file is saved with a .po extension to indicate it is complete. There is one .po file for each language.
.pot文件是由以上的命令生成的一个模板,包含了英文字符串和相应的空字符串文件,用于翻译者添加翻译。每次翻译结束,就可以保存为一个扩展名为po的文件表示完成。po文件可以使用任何一种语言。
最后,为每个po文件创建一个对应的mo文件。mo文件是被纳入安装包的二进位制译本。
msgfmt my-program.es.po -o my-program.es.mo
第七步:安装文件
在正确的目录下安装mo文件:
cp my-program.es.mo /usr/share/locale/es/LC_MESSAGES/my-program.mo
您可以通过Internationalization Guidelines (PDF, 450KB)查找详细的步骤说明。
原文地址:
预告:7月将进行Moblin V2.0线上、线下研讨会,请您随时关注!
本文介绍如何使用Gettexta库实现项目的国际化(i18n),包括修改代码、创建pot和po文件等步骤。
943

被折叠的 条评论
为什么被折叠?



