|
|
You use the gencat command to store the strings for a given locale in a catalog that can be read by the message retrieval function catgets. The gencat input file for our example would be:
$set 1 votre choix (o/n) 2 oui 3 nonThe $set directive specifies that the three messages are members of set 1. A subsequent $set directive would mean that the following messages are members of set 2, and so on. The messages for each module of an application, then, can be assigned to different sets, making it easier to keep track of message numbers across source files: the messages for any given module will always be numbered consecutively from 1. Note that each message in a gencat input file must be numbered. For details of the input file syntax, see the gencat(1) manual page.
If the gencat input file is named fr.str, the command
$ gencat progmsgs fr.strwill generate a catalog called
progmsgs
that,
when installed in the appropriate directory,
can be read by catgets such that
puts(catgets(catd, 1, 1, "Choose (y/n)"));will display
Votre choix (o/n)in a French locale. catd is the message catalog descriptor returned by the earlier call to catopen; the second and third arguments are the set and message numbers, respectively, of the string in the catalog. Again, you hard-code the final argument in case catgets fails. see the gencat(1), catgets(3C), and catopen(3C) manual pages.
The X/Open version of our example follows:
#include <stdio.h> #include <nl_types.h> #include <string.h> #include <locale.h> #define RESPLEN 16char yesstr[RESPLEN]; /* assumed to be long enough */ extern char *catgets(); main() { int yes(); nl_catd catd; setlocale(LC_ALL, ""); catd = catopen("progmsgs", 0);
/* save local yes string for subsequent comparisons */ strcpy(yesstr, catgets(catd, 1, 2, "yes"));
while(1) { puts(catgets(catd, 1, 1, "Choose (y/n)")); if (yes()) puts(yesstr); else puts(catgets(catd, 1, 3, "no")); } }
static int yes() { int i, b; i = b = getchar(); while (b != '\n' && b != '\0' && b != EOF) b = getchar(); return(i == (int) yesstr[0]); }