Sunday, December 23, 2007

A Tutorial on GNU gettext

“Hello world” example – hello.c

#include <stdio.h>
#include <locale.h>
#include <libintl.h>
int main()
{
setlocale(LC_ALL, "");
textdomain("hello");
bindtextdomain("hello", "locale");
printf(gettext("Hello, world !\n"));
return 0;
}

Compiling

gcc hello.c –c hello

Extracting translatable strings

xgettext -o hello.po hello.c

Translating

We will try to make a Chinese (zh_TW.UTF-8) translation

$ cat hello.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 , YEAR.

#

#, fuzzy

msgid ""

msgstr ""

"Project-Id-Version: PACKAGE VERSION\n"

"Report-Msgid-Bugs-To: \n"

"POT-Creation-Date: 2007-12-23 13:10+0800\n"

"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"

"Last-Translator: FULL NAME \n"

"Language-Team: LANGUAGE \n"

"MIME-Version: 1.0\n"

"Content-Type: text/plain; charset=CHARSET\n"

"Content-Transfer-Encoding: 8bit\n"

#: hello.cpp:19

#, c-format

msgid "Hello, world!\n"

msgstr ""

modifies hello.po as following

# 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 , YEAR.

#

#, fuzzy

msgid ""

msgstr ""

"Project-Id-Version: 1.0\n"

"Report-Msgid-Bugs-To: \n"

"POT-Creation-Date: 2007-12-23 13:10+0800\n"

"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"

"Last-Translator: FULL NAME \n"

"Language-Team: LANGUAGE \n"

"MIME-Version: 1.0\n"

"Content-Type: text/plain; charset=UTF-8\n"

"Content-Transfer-Encoding: 8bit\n"

#: hello.cpp:19

#, c-format

msgid "Hello, world!\n"

msgstr "大家好!\n"

builds the MO file

mkdir -p locale/zh_TW/LC_MESSAGES

msgfmt hello.po -o locale/zh_TW/LC_MESSAGES/hello.mo

Locale Environment variable

$ ./hello

Hello, world!

$ export LANG=zh_TW.UTF-8

$ ./hello

大家好!

Bibliography

1. GNU gettext

http://www.gnu.org/software/gettext/

2. A tutorial on Native Language Support using GNU gettext

http://oriya.sarovar.org/docs/gettext/

3. Gettext

http://www.haypocalc.com/wiki/Gettext

1 comment:

TV Digital said...

Hello. This post is likeable, and your blog is very interesting, congratulations :-). I will add in my blogroll =). If possible gives a last there on my blog, it is about the TV Digital, I hope you enjoy. The address is http://tv-digital-brasil.blogspot.com. A hug.