Our first example of using gettext will be the good old Hello World program,
whose sole function is to print the phrase ``Hello, world!'' to the terminal.
The internationalized version of this program might be saved in hello.c as:
1 #include <libintl.h>
2 #include <locale.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 int main(void)
6 {
7 setlocale( LC_ALL, "" );
8 bindtextdomain( "hello", "/usr/share/locale" );
9 textdomain( "hello" );
10 printf( gettext( "Hello, world!\n" ) );
11 exit(0);
12 }
Of course, a real program would check the return values of the functions and
try to deal with any errors, but we have omitted that part of the code for
clarity. Compile as usual with gcc -o hello hello.c. The program should
be linked to the GNU libintl library, but as this is part of the GNU C
library, this is done automatically for you under Linux, and other systems
using glibc.
Subsections
Gora Mohanty
2004-07-24