setlocale.3c (2010 09)
s
setlocale(3C) setlocale(3C)
LC_CTYPE=ru_RU.iso88595
All categories will be loaded with da_DK.iso88591
.
Another example with the
LC_ALL environment variable:
setlocale(LC_TIME, "pl_PL.iso88592");
with the following settings in the user’s environment:
LANG=it_IT.iso88591
LC_ALL=nl_NL.iso88591
The LC_TIME category will be set to pl_PL.iso88592
, but all other categories will be set to
nl_NL.iso88591
.
To set the date/time formats to
fr_FR.iso88591
:
setlocale(LC_TIME, "fr_FR.iso88591");
To set the collating sequence to the
C locale:
setlocale(LC_COLLATE, "C");
To set monetary handling to the value of the user’s
LC_MONETARY environment variable:
setlocale(LC_MONETARY, "");
Note that if the LC_MONETARY environment variable is not set or is empty, the value of the user’s
LANG
environment variable is used.
To query a user’s locale:
char *ch = setlocale(LC_ALL, NULL);
To restore the locale saved in the above example:
setlocale(LC_ALL, ch);
To query only the part of the user’s locale pertaining to the
LC_NUMERIC category:
char *ch = setlocale(LC_NUMERIC, NULL);
To restore the LC_NUMERIC category of the user’s locale saved in the above example:
setlocale(LC_NUMERIC, ch);
WARNINGS
The format of the return string from setlocale() is implementation specific, is not standardized
across vendor’s platforms, and is subject to change in future releases. The return string is valid only for
the life of the process invoking the setlocale() and should only be used for restoring a previously
stored locale setting within the same process.
Using
getenv() as the locale argument is not recommended. An example of such incorrect usage is:
setlocale(LC_ALL, getenv("LANG"));
getenv() returns a character string which can be a language name, an empty string, or a null pointer;
depending on the setting of the user’s LANG environment variable. Each of these values as the locale
argument define a specific action to be taken by setlocale(). Therefore the action taken by setlo-
cale() depends upon the value returned from the getenv() call. To ensure that setlocale() sets
the program’s locale based upon the setting of the user’s environment variables the following usage is
recommended:
setlocale(LC_ALL, "");
The value returned by setlocale() points to an area that is overwritten during the next call to set-
locale(). Be sure to copy these values to another area if they are to be used after a subsequent set-
locale() call.
getlocale() is an HP proprietary interface, which will be obsoleted in a future release, and is not
portable to other vendor’s platforms.
The structure returned through a call to
getlocale() is overwritten during the next call to getlo-
cale(). Be sure to save these values if they are to be used after a subsequent getlocale() call.
4 Hewlett-Packard Company − 4 − HP-UX 11i Version 3: September 2010