DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
Constructing and using message catalogs

Code example using a message catalog

The following code uses the message just created in ``Creating a message catalog''. Rather than hard-coding output directly, it reads the proper message catalog depending on the LANG environment variable.

Internationalized ``Hello, world'' program (hello.c)

  1 #include <stdio.h>
  2 #include <time.h>
  3 #include <nl_types.h>
  4 #include <langinfo.h>
  5 #include <locale.h>

6 #define TEMPBUF_SIZE 100

7 main() 8 { 9 nl_catd catalog; 10 time_t clock; 11 char tempbuffer[TEMPBUF_SIZE];

12 setlocale (LC_ALL, "");

13 if ((catalog = catopen ("hello.cat", 0)) == -1) { 14 fprintf(stderr, "Can't open message catalog\n"); 15 /* Continue anyway, this is not fatal */ 16 }

17 (void) time(&clock);

18 (void) strftime(tempbuffer, TEMPBUF_SIZE, 19 catgets(catalog, 1, 2, "%a %b %T %y"), 20 localtime(&clock)); 21 printf("%s %s\n", 22 catgets(catalog, 1, 1, "hello world (English)"), 23 tempbuffer); 24 );

25 catclose(catalog);

26 exit(0); 27 }


line 1
Needs to be included to use standard routines like printf(S).

line 2
Needs to be included to use time-related functions and data types.

line 3
Needs to be included to use the catalog routines.

lines 4-5
Needs to be included to use the LC_* and LANG environment variables.

line 6
Defines the maximum size of a temporary buffer.

line 9
Defines a variable to be of a type that identifies a catalog (referred to as a catalog descriptor).

line 10
Defines a variable to store the time.

line 11
Defines a temporary buffer of size TEMPBUF_SIZE to store the formatted string corresponding to the time.

line 12
Needs to be included so that the local environment variable LANG is read.

line 13
Checks to see if a catalog with the name argument matching ``hello'' can be opened. If successful, this message catalog will now be referenced by the catalog variable. If the program is to use the LC_MESSAGES environment variable, use NL_CAT_LOCALE as the second argument to the catopen(S) call instead of the 0 shown in the example.

lines 14-16
Prints out error message upon failure.

line 17
Sets the clock variable to UNIX operating system Epoch time.

lines 18-20
Formats the time according to the format in the message catalog designated by the variable catalog. (The final format specification is a default if there is an error in reading the message catalog.)

lines 21-24
Retrieves message number 1 of set number 1 in the message catalog designated by the variable catalog. (The final string is a default if there is an error in reading the message catalog.) The retrieved message is printed and followed by the preformatted time string stored in tempbuffer.

line 25
Closes the message catalog identified by the variable catalog.


Next topic: Combining the message catalog and the application program
Previous topic: Creating a message catalog

© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003