Next | Prev | Up | Top | Contents | Index
Setting the Current Locale
Applications begin in the C locale. (C is the name used to indicate the system default locale; it usually corresponds to American English.) Applications should therefore call setlocale() as soon as possible to put the process into the desired locale. The syntax for setlocale() is:
#include <locale.h>
char *setlocale(int category, const char *locale);
The call almost always looks either like this:
if (setlocale(LC_ALL, "") == NULL)
exit_with_error();
or like this:
if (setlocale(LC_ALL, "") == NULL)
setlocale(LC_ALL, "C");
Details of the two parameters are given in the next two sections.
Category
Applications need not perform every aspect of their work in the same locale. Although this approach is not recommended, an application could (for example) perform most of its activities in the English locale but use French sorting rules. You can use locale categories to do this kind of locale-mixing. (Mixing locale categories is not the same as multilingual support--see "Multilingual Support.")
The category argument is a symbolic constant that tells setlocale() which items in a locale to change. Table 6-1 lists the available category choices.
Locale Categories
Category | Affects |
---|
LC_ALL | All categories below |
LC_COLLATE | Regular expressions, strcoll(), and strxfrm() |
LC_CTYPE | Regular expressions and ctype routines (such as islower()) |
LC_MESSAGES[1] | gettxt(), pfmt(), and nl_langinfo() |
LC_MONETARY | localeconv() and strfomon() |
LC_NUMERIC | Decimal-point character for formatted I/O and nonmonetary formatting information returned by localeconv() |
LC_TIME | ascftime(), cftime(), getdate(), and strftime() |
Categories correspond to databases that contain relevant information for each defined locale. The locations of these databases are given in the "Location of Locale-Specific Data".
Locale
The setlocale() function attempts to set the locale of the specified category to the specified locale. You should almost always pass the empty string as the locale parameter to conform to user preferences.
On success, setlocale() returns the new value of the category. If setlocale() couldn't set the category to the value requested, it returns NULL and does not change locale.
The Empty String
An empty string passed as the locale parameter is special. It specifies that the locale should be chosen based on environment variables. This is the way a user specifies a preferred locale, and that preference should almost always be honored. The variables are checked hierarchically, depending on category, as shown in Table 6-2; for instance, if the category is LC_COLLATE, an empty-string locale parameter indicates that the locale should be chosen based on the value of the environment variable LC_COLLATE--or, if that value is undefined, the value of the environment variable LANG, which should contain the name of the locale that the user wishes to work in.
Category Environment Variables
Category | First Environment Variable | Second Environment Variable |
---|
LC_COLLATE | LC_COLLATE | LANG |
LC_CTYPE | LC_CTYPE | LANG |
LC_MESSAGES | LC_MESSAGES | LANG |
LC_MONETARY | LC_MONETARY | LANG |
LC_NUMERIC | LC_NUMERIC | LANG |
LC_TIME | LC_TIME | LANG |
Specifying the category LC_ALL attempts to set each category individually to the value of the appropriate environment variable.
If no non-null environment variable is available, setlocale() returns the name of the current locale.
Nonempty Strings in Calls to setlocale()
Here are the possibilities for specifying the locale parameter:
NULL | Specifying a null pointer argument--not the same as the empty string--causes setlocale() to return the name of the current locale. |
"C" | Specifying a locale value of the single-character string "C" requests whatever locale the system uses as a default. (Note that this is a string and not just a character.) |
Other strings | Request a particular locale by specifying its name. This overrides any user preferences and should only be done with good reason. |
Location of Locale-Specific Data
Except for XPG/4 message catalogs, locale-specific data (that is, the "compiled" files containing the collation information, monetary information, and so on) are located in /usr/lib/locale/<locale>/<category>, where <locale> and <category> are the names of the locale and category, respectively. For example, the database for the LC_COLLATE category of the French locale fr would be in /usr/lib/locale/fr/LC_COLLATE.
There will probably be multiple locales symbolically linked to each other, usually in cases where a specific locale name points to the more general case. For example, /usr/lib/locale/En_US.ascii might point to /usr/lib/locale/C.
Locale Naming Conventions
A locale string is of the form
language[_territory[.encoding]][@modifier]...
where
- language is the two-letter ISO 639 abbreviation for the language name.
- territory is the two-uppercase-letter ISO 3166 abbreviation for the territory name. (For a list of these abbreviations, see the table in Appendix A, "ISO 3166 Country Names and Abbreviations.")
- encoding is the name of the character encoding (mapping between numbers and characters). For western languages, this is typically the codeset, such as 8859-1 or ASCII. For Asian languages, where an encoding may encode multiple codesets, the encodings themselves have names, such as UJIS or EUC (these encodings are described later in this section). "Character Sets, Codesets, and Encodings" discusses codesets and encodings.
- modifiers are not actually part of the locale name definition; they give more specific information about the desired localized behavior of an application. For example, under X11R5 or X11R6, a user can select an input method with modifiers. (To use the xwnmo Input Method server provided by Silicon Graphics, for example, add @im=_XWNMO to the locale string.) No standards exist for this part of a locale string.
Language data is implementation specific; databases for the language en (English) might contain British cultural data in England and American cultural data in the United States. If other than the default settings are required, the territory field may be used. For example, the above cases could be more strictly defined by setting LANG to en_EN or en_US. Full rigor might lead to en_EN.88591 for England (the locale encoding specification for ISO 8859-1 is "88591") and en_US.ascii for the USA.
ANSI C has defined a special locale value of C. The C locale is guaranteed to work on all compliant systems and provides the user with the system's default locale. This default is typically American English and ASCII, but need not be. POSIX has also defined a special locale value, POSIX, which is identical to the C locale.
The length of the locale string may not exceed NL_LANGMAX characters (NL_LANGMAX is defined in /usr/include/limits.h). However, XPG/4 recommends that this string (not counting modifiers) not exceed 14 characters.
[1] LC_MESSAGES is supported by SVR4 but isn't required by XPG/4.
Next | Prev | Up | Top | Contents | Index