Museum

Home

Lab Overview

Retrotechnology Articles

Online Manuals

⇒ getlocale(3C) — HP-UX ANSI C A.09.00

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

nlsinfo(1)

buildlang(1M)

conv(3C)

ctype(3C)

ecvt(3C)

getdate(3C)

langinfo(3C)

multibyte(3C)

nl_tools_16(3C)

printf(3S)

scanf(3S)

strcoll(3C)

strftime(3C)

string(3C)

strtod(3C)

vprintf(3S)

wconv(3X)

wctype(3X)

wstring(3X)

hpnls(5)

environ(5)

langinfo(5)

strerror(3C)

setlocale(3C)

NAME

setlocale(), getlocale() − set and get the locale of a program

SYNOPSIS

#include <locale.h>

char *setlocale(int category, const char *locale);

struct locale_data *getlocale(int type);

DESCRIPTION

setlocale() sets, queries or restores that aspect of a program’s locale as specified by the category argument.  A program’s locale refers to those areas of the program’s Native Language Support (NLS) environment for which the following values of category have been defined:

LC_ALL Affects behavior of all categories below as well as all nl_langinfo(3C) items. Note that some nl_langinfo items are only affected by the setting of the LC_ALL category. 

LC_COLLATE Affects behavior of regular expressions and the NLS string collation functions (see string(3C) and regexp(5)).

LC_CTYPE Affects behavior of regular expressions, character classification and conversion functions (see ctype(3C), conv(3C), and regexp(5)). The LC_CTYPE category also affects the behavior of all routines that process multibyte characters (see multibyte(3C) and nl_tools_16(3C)).

LC_MESSAGES Affects the language in which messages are displayed and the processing of affirmative and negative responses. 

LC_MONETARY Affects behavior of functions that handle monetary values (see localeconv(3C)).

LC_NUMERIC Affects handling of the radix character in the formatted input/output functions (see printf(3C), scanf(3C) and vprintf(3C)) and the string conversion functions (see ecvt(3C) and strtod(3C)). LC_NUMERIC also affects the numeric values found in the localeconv structure. 

LC_TIME Affects the behavior of time conversion functions (see getdate(3C) and strftime(3C)).

All nl_langinfo(3C) items are affected by the setting of one of the categories listed above. See langinfo(5) to determine which categories affect each item.

The value of the locale argument determines the action taken by setlocale().  locale is a pointer to a character string. 

Setting the Locale of a Program

To set the program’s locale for category, setlocale() accepts one of the following values as the locale argument: locale name, "C", or "" (the empty string). The actions prescribed by these values are as follows :

locale name If locale is a valid locale name (see lang(5)), setlocale() sets that part of the NLS environment associated with category as defined for that locale. 

"C" If the value of locale is set to "C", setlocale() sets that part of the NLS environment associated with category as defined for the "C" locale (see lang(5)).  The "C" locale is the default prior to successfully calling setlocale(). 

POSIX Same as "C"

"" If the value of locale is the empty string, the setting of that part of the NLS environment associated with category depends on the setting of the following environment variables in the user’s environment (see environ(5)):

LANG                LC_MESSAGES
LC_ALL              LC_MONETARY
LC_COLLATE          LC_NUMERIC
LC_CTYPE            LC_TIME

If category is any defined value other than LC_ALL, setlocale() sets that category as specified by the value of the LC_ALL environment or if LC_ALL is not set to the corresponding environment variable.  If the environment variable is not set or set to the empty string, setlocale() sets the category as specified by the value of the LANG environment variable.  If LANG is not set or is set to the empty string, then setlocale() sets the category to the "C" locale.  For example, setlocale(LC_TIME,"") sets the program’s NLS environment associated with the LC_TIME category to the value specified by the user’s LC_TIME environment variable.  All other aspects of the NLS environment are unaffected. 

If category is LC_ALL, then all categories are set corresponding to the value of LC_ALL if LC_ALL is set, or LANG if LC_ALL is not set, except for those categories in which the corresponding environment variable is set to a valid language name (see lang(5)).  In this case the value of the environment variable overrides the values of LC_ALL and LANG for that category.  If the values of both LC_ALL and LANG are not set or are set to the empty string, then the "C" locale is used. 

The following usage of setlocale() results in the program’s locale being set according to the the user’s language requirements:

setlocale(LC_ALL,"") ;

Querying the Locale of a Program

setlocale() queries the current NLS environment pertaining to category if the value of locale is NULL.  The query operation does not change the environment.  The purpose of performing a query is to save that aspect of the user’s current NLS environment associated with category, in the value returned by setlocale(), such that it can be restored with a subsequent call to setlocale(). 

Restoring the Locale of a Program

To restore a category within the program locale, a setlocale() call is made with the same category argument and the return string of the previous setlocale() call given as the locale argument. 

getlocale() returns a pointer to a locale_data structure (see /usr/include/locale.h).  The members of the locale_data structure contain information about the setting of each setlocale category.  type determines what information is contained in the locale_data structure.  Defined values of type and their behaviour are :

LOCALE_STATUS The structure member corresponding to each category contains a string with the name of the locale currently set for that category.  The string does not include modifier information. 

MODIFIER_STATUS The structure member corresponding to each category contains a string with the name of the modifier currently set for that category.  If no modifier is set then the entry contains an empty string. 

ERROR_STATUS The structure member contains information about errors which occurred during the previous call to setlocale().  If setlocale() could not satisfy a request corresponding to a particular category, the structure member for that category contains a string with the name of the invalid locale. In all other cases the member for the category contains an empty string.

RETURN VALUE

If the pointer to a string is given for locale and the selection can be honored, the setlocale() function returns a pointer to the string associated with the specified category for the new locale.  The maximum length of this string is LC_BUFSIZ bytes (see <locale.h>).  If the selection cannot be honored, the setlocale() function returns a null pointer and the program’s locale is not changed. 

A null pointer for locale causes setlocale() to return a string associated with the category for the program’s current locale. 

The string returned by setlocale() is such that a subsequent call with that string as the locale argument and its associated category restores that part of the program’s locale. 

ERRORS

If a language name given through the locale argument does not identify a valid language name or the language is not available on the system (see lang(5)) a null pointer is returned and the program’s locale is not changed.  The same behavior occurs when the call :

setlocale(LC_ALL, "");

is made and any category related environment variable in the user’s environment identifies an invalid language name or a language that is not available on the system. 

If the category argument is not a defined category value, a null pointer is returned and the program’s locale is not changed. 

setlocale() returns a string which reflects the current setting of that aspect of the NLS environment corresponding to the category argument.  If this return string is used in a subsequent setlocale() call and the category arguments of the two calls do not match, the locale remains unchanged and a null pointer is returned. 

WARNINGS

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 setlocale() 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 a static area that is overwritten during the next call to setlocale().  Be sure to copy these values to another area if they are to be used after a subsequent setlocale() call. 

The structure returned through a call to getlocale() is overwritten during the next call to getlocale().  Be sure to save these values if they are to be used after a subsequent getlocale() call. 

Any program which calls setlocale() before catopen() may behave differently in this release than on previous releases because of the addition of LC_MESSAGES to XPG4 .  In the past, catopen() was directed to the desired language by LANG .  Now, catopen() is controlled by LC_MESSAGES .  Setlocale() can modify the LC_MESSAGES category. 

For example, if the environment variables are set as follows :

LC_MESSAGES="french"

and the following call to setlocale() is made:

setlocale(LC_ALL, "german");

which is followed by a call to catopen() .  Catopen() will open the message catalogs for german rather than french. 

EXAMPLES

To set a program’s entire locale based on the language requirements specified via the user’s environment variables :

setlocale(LC_ALL, "");

If, in the previous example, the user’s environment variables were set as follows :

LANG ="german"
LC_COLLATE ="spanish@nofold"
LC_MONETARY =""
LC_TIME ="american"

the LC_ALL, LC_CTYPE, LC_MONETARY, and LC_NUMERIC category items would be set to correspond to the german language definition, the LC_COLLATE category items would be set to correspond to the spanish language definition for unfolded collation (see hpnls(5)) and the LC_TIME category items would be set corresponding to the american language definition. 

Using the same example, if the following call was made:

struct locale_data *locale_info=getlocale(LOCALE_STATUS);

the contents of *locale_info would be :

locale_info->LC_ALL_D="german"
locale_info->LC_COLLATE_D="spanish"
locale_info->LC_CTYPE_D="german"
locale_info->LC_MESSAGES_D="german"
locale_info->LC_MONETARY_D="german"
locale_info->LC_NUMERIC_D="german"
locale_info->LC_TIME_D="american"

Continuing with the same example, if the following call was made :

struct locale_data *modifier_info=getlocale(MODIFIER_STATUS);

the contents of *modifier_info would now be :

modifier_info−>LC_ALL_D=""
modifier_info−>LC_COLLATE_D="nofold"
modifier_info−>LC_CTYPE_D=""
modifier_info−>LC_MESSAGES_D=""
modifier_info−>LC_MONETARY_D=""
modifier_info−>LC_NUMERIC_D=""
modifier_info−>LC_TIME_D=""

The calls :

setlocale(LC_ALL, "");
struct locale_data *error_info=getlocale(ERROR_STATUS);

with the following settings in the users environment :

LANG=german
LC_COLLATE=junk

where junk is an invalid language, would result in the contents of *error_info being:

_error_info−>LC_ALL_D=""
_error_info−>LC_COLLATE_D="junk"
_error_info−>LC_CTYPE_D=""
_error_info−>LC_MESSAGES_D=""
_error_info−>LC_MONETARY_D=""
_error_info−>LC_NUMERIC_D=""
_error_info−>LC_TIME_D=""

An example showing the precedence of the LC_ALL environment variable :

setlocale(LC_ALL, "");

with the following settings in the users environment :

LANG=german
LC_ALL=french

All categories will be loaded with french. 

Another example showing the precedence of the LC_ALL environment variable :

setlocale(LC_CTYPE, "");

with the following settings in the users environment :

LANG=turkish
LC_ALL=danish
LC_CTYPE=russian

The LC_CTYPE category will be loaded with danish. 

Another example with the LC_ALL environment variable :

setlocale(LC_TIME, "polish");

with the following settings in the users environment :

LANG=italian
LC_ALL=dutch

The LC_TIME category will be set to polish. 

To set the date/time formats to French :

setlocale(LC_TIME, "french");

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 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 just that 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);

AUTHOR

setlocale() was developed by HP. 

SEE ALSO

nlsinfo(1), buildlang(1M), conv(3C), ctype(3C), ecvt(3C), getdate(3C), langinfo(3C), multibyte(3C), nl_tools_16(3C), printf(3S), scanf(3S), strcoll(3C), strftime(3C), string(3C), strtod(3C), vprintf(3S), wconv(3X), wctype(3X), wstring(3X), hpnls(5), environ(5), langinfo(5), strerror(3C), <langinfo.h>, <locale.h>. 

STANDARDS CONFORMANCE

setlocale(): AES, XPG3, XPG4, FIPS 151-2, POSIX.1, ANSI C

Hewlett-Packard Company  —  HP-UX Release 9.0: August 1992

Typewritten Software • bear@typewritten.org • Edmonds, WA 98026