|
|
Each UnixWare program begins in the ``"C"'' locale, which causes all library functions to behave as they have historically; any other locale will cause certain of these functions to behave in the appropriate language- or culture-dependent ways. Locales have names that are strings -- ``"es"'', ``"fr"'' and ``"de"'', and so forth, following ISO conventions (corresponding to the Spanish, French and German locales, respectively) -- but only ``"C"'' and ``""'' are guaranteed. When given as the second argument to the ANSI C setlocale function, the string ``""'' tells the program to change its current locale to the one set by the user, or the system administrator for all users, in the UnixWare system shell environment. Any other argument will cause the program to change its current locale to the one specified by the string.
Locales are partitioned into categories:
$ LC_COLLATE=de export LC_COLLATE $ LC_CTYPE=fr export LC_CTYPE $ LC_MESSAGES=fr export LC_MESSAGESIn the program's view, the categories are macros that can be passed as the first argument to setlocale to specify that it change the program's locale for just that category. That is,
setlocale(LC_COLLATE, "");tells the program to use the sorting information for the locale specified in the environment, in this case, german, but leaves the other categories unchanged.
LC_ALL is the macro that specifies the entire locale. Given the environment setup above, the code
setlocale(LC_ALL, "");would allow a user to work in a French interface to a program while sorting German text files. Incidentally, the LANG environment variable is the user equivalent of LC_ALL; setting it to es, for instance, causes all the categories to be set to es in the environment. LANG is checked after the environment variables for individual categories, so a user could set a category to fr and use LANG to set the other categories to es.
setlocale, then, is the interface to the program's locale. Any program that has a need to use language or cultural conventions should put a call such as
#include <locale.h> /*...*/ setlocale(LC_ALL, "");early in its execution path. You will generally want to use ``""'' as the second argument to setlocale so that your application will change locales correctly for whatever language environment in which it is run. Occasionally, though, you may want to change the locale or a portion of it for a limited duration in a way that's transparent to the user.
Suppose, for example, there are parts of your program that need only the ASCII upper- and lower case characters guaranteed by ANSI C in the <ctype.h> header. In these parts, in other words, you want the program to see the character classification information in LC_CTYPE for the ``"C"'' locale. Since the user of the program in a non-ASCII environment will presumably have set LC_CTYPE to a locale other than ``"C"'', and will not be able to change its setting mid-program, you will have to arrange for the program to change its LC_CTYPE locale whenever it is in those parts. setlocale returns the name of the current locale for a given category and serves in an inquiry-only capacity when its second argument is a null pointer. So you might want to use code something like this:
char *oloc; /*...*/ oloc = setlocale(LC_CTYPE, NULL); if (setlocale(LC_CTYPE, "C") != 0) { /* use temporarily changed locale */ (void)setlocale(LC_CTYPE, oloc); }The setlocale(3C) function is described in section (3C) of the reference manual set.