程序国际化(I18N)快速起步

本文介绍如何使用Gettexta库实现项目的国际化(i18n),包括修改代码、创建pot和po文件等步骤。

翻译:高莹婷

GettextaGNU国际化(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

第六步:生成potpo文件

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)查找详细的步骤说明。

 

原文地址:

http://moblin.org/documentation/moblin-sdk/coding-tutorials/getting-started-internationalization-i18n

预告:7月将进行Moblin V2.0线上、线下研讨会,请您随时关注!

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值