这是英文书上的示例,贴在这里当翻译的文章跟大家share下。没有逐字逐句翻译哈,加了我个人的一些口水话和一些废话哈哈
这是来自《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:
哈哈,完美:)
职场unix休闲开发-测试-SCM
0