一个示范gettext实现L10N的小例子

简介:

这是来自《Free/Open Source Software Guide to Localization》里面Chapter10的示例,简单明了,我在RedHat5.3上尝试了一把,终于明白了gettext的使用方法,虽然还很浅显哈哈,但对我来说意义非凡!小弟一直想整个可以随便国际化本地化的程序,不知道如何入手,《Free/Open Source Software Guide to Localization》给了我相当的指导,推荐大家阅读O(∩_∩)O~


废话不说,直接上代码,helloworldintl.c:

#include<libintl.h>
#include<locale.h>
#include<stdio.h>

#define _(String) gettext (String)
#define _t(String1,String2,n) ngettext (String1,String2,n)

int main()
{
     int i;
    setlocale(LC_ALL,"");
    bindtextdomain( "helloworldintl", "/usr/share/locale");
    textdomain( "helloworldintl");
    printf(_( "again"));
    printf( "\n");
    printf(_( "Hello World"));
    printf( "\n");
    printf(_( "These text demonstrate that translation using po files is easier\n"));
    scanf( "%d",&i);
    printf(_t( "This number %d is of singular form", "This number %d is of plural form",i),i);
    printf( "\n");
    printf(_( "The magic number is %d\n"),i);
    printf(_( "These are additional words"));
    printf( "\n");
    printf(_( "I love translation\n"));
} //main


Step 1:
执行如下命令提取程序中的字符串:
xgettext -d helloworldintl -o helloworldintl.pot --keyword=_t:1,2 -k_ -s helloworldintl.c
注意-k参数后面有个下划线,表示需要提取以下划线引用gettext函数的相关字符串。我弄了好几次才弄对的!

Step 2:
然后随便用Linux上的支持Unicode的文本编辑器,比如gedit打开helloworldintl.pot,瞧下内容:

# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file  is distributed under the same license  as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: Brant I18N Hello World 1.0\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2009-12-16 17:15+0800\n"
"PO-Revision-Date: 2009-12-16 17:15+0800\n"
"Last-Translator: BrantC <BrantC@xxx.com>\n"
"Language-Team: BrantC <BrantC@xxx.com>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=n != 11;\n"

#: helloworldintl.c:17
#, c-format
msgid  "Hello World"
msgstr ""

#: helloworldintl.c:26
#, c-format
msgid  "I love translation\n"
msgstr  "\n"

#: helloworldintl.c:23
#, c-format
msgid  "The magic number is %d\n"
msgstr  " %d\n"

#: helloworldintl.c:24
#, c-format
msgid  "These are additional words"
msgstr ""

#: helloworldintl.c:19
#, c-format
msgid  "These text demonstrate that translation using po files is easier\n"
msgstr  "po\n"

#: helloworldintl.c:21
#, c-format
msgid  "This number %d is of singular form"
msgid_plural  "This number %d is of plural form"
msgstr[0]  " %d "
msgstr[1]  " %d "

#: helloworldintl.c:14
#, c-format
msgid  "again"
msgstr ""


Step 3:
把这个pot文件copy为po文件,即helloworldintl.po文件,这种po文件时要给LS team的,他们就把字符串翻译更新到po文件中,翻译后的po文件,如下:

# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file  is distributed under the same license  as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: Brant I18N Hello World 1.0\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2009-12-16 17:15+0800\n"
"PO-Revision-Date: 2009-12-16 17:15+0800\n"
"Last-Translator: BrantC <BrantC@xxx.com>\n"
"Language-Team: BrantC <BrantC@xxx.com>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=n != 11;\n"

#: helloworldintl.c:17
#, c-format
msgid  "Hello World"
msgstr  "你好,世界"

#: helloworldintl.c:26
#, c-format
msgid  "I love translation\n"
msgstr  "我爱翻译\n"

#: helloworldintl.c:23
#, c-format
msgid  "The magic number is %d\n"
msgstr  "这个神齐的数字是 %d\n"

#: helloworldintl.c:24
#, c-format
msgid  "These are additional words"
msgstr  "这些是附加的单词"

#: helloworldintl.c:19
#, c-format
msgid  "These text demonstrate that translation using po files is easier\n"
msgstr  "这些示例文本通过po文件翻译更容易\n"

#: helloworldintl.c:21
#, c-format
msgid  "This number %d is of singular form"
msgid_plural  "This number %d is of plural form"
msgstr[0]  "这个数字 %d 是单数形式"
msgstr[1]  "这个数字 %d 是复数形式"

#: helloworldintl.c:14
#, c-format
msgid  "again"
msgstr  "重试"


Step 4:
然后,执行下面的命令生成mo文件:
msgfmt helloworldintl.po -o helloworldintl.m

Step 5:
然后把mo文件copy到相应的locale目录下面,我这里是/usr/share/locale/zh_CN.GB2312/LC_MESSAGES/.

Step 6:
现在可以算大功告成,执行中文版的helloworldintl:



执行英文版的helloworldintl:



哈哈,完美:)






本文转自 xkdcc 51CTO博客,原文链接:http://blog.51cto.com/brantc/244767,如需转载请自行联系原作者

目录
相关文章
|
NoSQL Java Redis
Redis 从入门到精通之Redis Pipeline
使用Redis Pipeline可以大大提高Redis的性能和吞吐量,但需要注意命令的顺序和语义,以保证数据的正确性和一致性。同时,使用Jedis和RedisTemplate实现Redis Pipeline时,需要遵循最佳实践,避免出现错误和异常。 2. 在使用Jedis实现Redis Pipeline时,需要使用Pipeline对象添加多个命令并执行,然后通过`syncAndReturnAll`方法获取所有命令的执行结果。 3. 在使用RedisTemplate实现Redis Pipeline时,需要使用`executePipelined`方法添加多个命令并执行,然后通过返回的结果列表获取
1030 104
Redis 从入门到精通之Redis Pipeline
|
人工智能 安全 大数据
元宇宙游戏:沉浸式体验的新纪元
在科技飞速发展的今天,元宇宙游戏作为融合了虚拟现实(VR)、增强现实(AR)、人工智能(AI)与区块链等前沿技术的数字新世界,正引领我们进入一个前所未有的沉浸式体验时代。本文将深入探讨元宇宙游戏的特点、技术基础及其如何引领沉浸式体验的新潮流。
|
Swift iOS开发
SwiftUI极简教程01:搭建一个新项目&Text文字的使用
SwiftUI极简教程01:搭建一个新项目&Text文字的使用
791 1
SwiftUI极简教程01:搭建一个新项目&Text文字的使用
|
存储 人工智能
西门子S7-200 SMART Modbus RTU通信,如何编写从站程序
上篇文章中我们通过一个例子学习了西门子S7-200 SMART中断程序的编写,本篇我们开始学习S7-200 SMART的Modbus RTU通信。通过集成RS485端口或可选通信板SM CM01的RS485/RS232端口,S7-200 SMART可以作为Modbus RTU主站或者从站同多个设备进行通信。
西门子S7-200 SMART Modbus RTU通信,如何编写从站程序
|
应用服务中间件 网络安全 nginx
阿里云服务器给WordPress网站添加SSL证书,并且设置http自动跳转https
阿里云服务器给WordPress网站添加SSL证书,并且设置http自动跳转https
1193 0
阿里云服务器给WordPress网站添加SSL证书,并且设置http自动跳转https
|
消息中间件 存储 运维
让数据流动起来,RocketMQ Connect 技术架构解析
本文介绍了 RocketMQ Connect 的概念,然后讲解了 RocketMQ Connect 的实现原理,对服务发现,配置同步,位点同步,负载均衡都有了初步的介绍,接着以 MySqlSourceConnector 为例讲解了如何自己实现一个 Connector,最后对 Connect API 和生态做了一些介绍,提供了一些 RocketMQ Connect 相关的上手教程。
让数据流动起来,RocketMQ Connect 技术架构解析
|
SQL 数据库
数据库原理与应用(SQL Server)教程 主键、外键以及联合主键、复合主键和设置种子数目和增量
数据库原理与应用(SQL Server)教程 主键、外键以及联合主键、复合主键和设置种子数目和增量
数据库原理与应用(SQL Server)教程 主键、外键以及联合主键、复合主键和设置种子数目和增量
|
开发框架 Dart 前端开发
初探Flutter在IoT场景下生态和趋势
IoT 领域,一个避不开的词就是碎片化。在硬件方面,厂商、架构、芯片、传感器等等方面的差异,形成了硬件体系的多样性。
初探Flutter在IoT场景下生态和趋势
|
数据采集 数据安全/隐私保护 Python
爬虫,遇到aspx动态加载的验证码怎么办?
爬虫,遇到aspx动态加载的验证码怎么办?
爬虫,遇到aspx动态加载的验证码怎么办?
|
消息中间件 XML JSON
面对Spring Boot 3最低支持Java17如洪水猛兽袭来,何去何从
面对Spring Boot 3最低支持Java17如洪水猛兽袭来,何去何从
面对Spring Boot 3最低支持Java17如洪水猛兽袭来,何去何从