NAME
setlocale — set
program locale
SYNOPSIS
#include
<locale.h>
char *setlocale(int category, const char *locale);
DESCRIPTION
[CX] The functionality described on this reference page is aligned with the ISO C standard. Any conflict between the requirements described here and the ISO C standard is unintentional. This volume of POSIX.1-2008 defers to the ISO C standard.
The setlocale () function selects the
appropriate piece of the locale of the process, as specified by the
category and locale arguments, and may
be used to change or query the entire locale of the process or portions
thereof. The value LC_ALL for category
names the entire locale of the process; other values for
category name only a part of the locale of the
process:
- LC_COLLATE
- Affects the behavior of regular expressions and the collation functions.
- LC_CTYPE
- Affects the behavior of regular expressions, character classification, character conversion functions, and wide-character functions.
- LC_MESSAGES
- [CX] Affects what strings are expected by commands and utilities as
affirmative or negative responses.
[XSI] It also affects what strings are given by commands and utilities as affirmative or negative responses, and the content of messages.
- LC_MONETARY
- Affects the behavior of functions that handle monetary values.
- LC_NUMERIC
- Affects the behavior of functions that handle numeric values.
- LC_TIME
- Affects the behavior of the time conversion functions.
The locale argument is a pointer to a character string containing the required setting of category. The contents of this string are implementation-defined. In addition, the following preset values of locale are defined for all settings of category:
- "POSIX"
- [CX] Specifies the minimal environment for C-language translation called
the POSIX locale. If
setlocale() is not invoked, the POSIX locale is the default at entry to main (). - "C"
- Equivalent to "POSIX" .
- ""
- Specifies an implementation-defined native environment. [CX] The determination of the name of the new locale for the specified category depends on the value of the associated environment variables, LC_* and LANG ; see XBD Locale and Environment Variables .
- A null pointer
- Used to direct
setlocale() to query the current internationalized environment and return the name of the locale.
[CX] Setting all of the categories of the locale
of the process is similar to successively setting each individual category
of the locale of the process, except that all error checking is done before
any actions are performed. To set all the categories of the locale of the
process, setlocale () is invoked as:
setlocale(LC_ALL, "");
In this case, setlocale () shall first
verify that the values of all the environment variables it needs according
to the precedence rules (described in XBD V1_chap08(7) )
indicate supported locales. If the value of any of these environment
variable searches yields a locale that is not supported (and non-null),
setlocale () shall return a null pointer and the
locale of the process shall not be changed. If all environment variables
name supported locales, setlocale () shall proceed
as if it had been called for each category, using the appropriate value from
the associated environment variable or from the implementation-defined
default if there is no such value.
The locale state is common to all threads within a process.
RETURN VALUE
Upon successful completion, setlocale ()
shall return the string associated with the specified category for the new
locale. Otherwise, setlocale () shall return a null
pointer and the locale of the process is not changed.
A null pointer for locale causes
setlocale () to return a pointer to the string
associated with the category for the current locale of the
process. The locale of the process shall not be changed.
The string returned by setlocale () is
such that a subsequent call with that string and its associated
category shall restore that part of the locale of the
process. The application shall not modify the string returned which may be
overwritten by a subsequent call to setlocale
().
ERRORS
No errors are defined.
EXAMPLES
None.
APPLICATION USAGE
The following code illustrates how a program can initialize the international environment for one language, while selectively modifying the locale of the process such that regular expressions and string operations can be applied to text recorded in a different language:
setlocale(LC_ALL, "De"); setlocale(LC_COLLATE, "Fr@dict");
Internationalized programs must call
setlocale () to initiate a specific language
operation. This can be done by calling setlocale ()
as follows:
setlocale(LC_ALL, "");
Changing the setting of LC_MESSAGES has no effect on catalogs that have already been opened by calls to catopen(3).
RATIONALE
The ISO C standard defines a collection of functions to support internationalization. One of the most significant aspects of these functions is a facility to set and query the international environment. The international environment is a repository of information that affects the behavior of certain functionality, namely:
- Character handling
- Collating
- Date/time formatting
- Numeric editing
- Monetary formatting
- Messaging
The setlocale () function
provides the application developer with the ability to set all or portions,
called
categories, of
the international environment. These categories correspond to the areas of
functionality mentioned above. The syntax for
setlocale () is as follows:
char *setlocale(int category, const char *locale);
where category is the name of one of following categories, namely:
LC_COLLATE LC_CTYPE LC_MESSAGES LC_MONETARY LC_NUMERIC LC_TIME
In addition, a special value called LC_ALL
directs setlocale () to set all categories.
There are two primary uses of setlocale
():
- Querying the international environment to find out what it is set to
- Setting the international environment, or locale, to a specific value
The behavior of setlocale () in these two
areas is described below. Since it is difficult to describe the behavior in
words, examples are used to illustrate the behavior of specific uses.
To query the international environment,
setlocale () is invoked with a specific category and
the null pointer as the locale. The null pointer is a special directive to
setlocale () that tells it to query rather than set
the international environment. The following syntax is used to query the
name of the international environment:
setlocale({LC_ALL, LC_COLLATE, LC_CTYPE, LC_MESSAGES, LC_MONETARY, \
LC_NUMERIC, LC_TIME},(char *) NULL);
The setlocale () function shall
return the string corresponding to the current international environment.
This value may be used by a subsequent call to
setlocale () to reset the international environment
to this value. However, it should be noted that the return value from
setlocale () may be a pointer to a static area
within the function and is not guaranteed to remain unchanged (that is, it
may be modified by a subsequent call to setlocale
()). Therefore, if the purpose of calling setlocale
() is to save the value of the current international environment so it can
be changed and reset later, the return value should be copied to an array of
char in the calling
program.
There are three ways to set the international environment with
setlocale ():
- setlocale(category, string)
-
This usage sets a specific category in the international environment to a specific value corresponding to the value of the string . A specific example is provided below:setlocale(LC_ALL, "fr_FR.ISO-8859-1");
In this example, all categories of the international environment are set to the locale corresponding to the string
"fr_FR.ISO-8859-1", or to the French language as spoken in France using the ISO/IEC 8859-1:1998 standard codeset.If the string does not correspond to a valid locale,
setlocale() shall return a null pointer and the international environment is not changed. Otherwise,setlocale() shall return the name of the locale just set. - setlocale(category, "C")
-
The ISO C standard states that one locale must exist on all conforming implementations. The name of the locale is C and corresponds to a minimal international environment needed to support the C programming language. - setlocale(category, "")
-
This sets a specific category to an implementation-defined default. This corresponds to the value of the environment variables.
FUTURE DIRECTIONS
None.
SEE ALSO
exec(3) , fprintf(3) , fscanf(3) , isalnum(3) , isalpha(3) , isblank(3) , iscntrl(3) , isdigit(3) , isgraph(3) , islower(3) , isprint(3) , ispunct(3) , isspace(3) , isupper(3) , iswalnum(3) , iswalpha(3) , iswblank(3) , iswcntrl(3) , iswctype(3) , iswdigit(3) , iswgraph(3) , iswlower(3) , iswprint(3) , iswpunct(3) , iswspace(3) , iswupper(3) , iswxdigit(3) , isxdigit(3) , localeconv(3) , mblen(3) , mbstowcs(3) , mbtowc(3) , nl_langinfo(3) , setlocale(3) , strcoll(3) , strerror(3) , strfmon(3) , strsignal(3) , strtod(3) , strxfrm(3) , tolower(3) , toupper(3) , towlower(3) , towupper(3) , uselocale(3) , wcscoll(3) , wcstod(3) , wcstombs(3) , wcsxfrm(3) , wctomb(3)
XBD V1_chap07(7) , V1_chap08(7) , <langinfo.h> , <locale.h>
CHANGE HISTORY
First released in Issue 3.
Issue 5
The DESCRIPTION is updated for alignment with the POSIX Threads Extension.
Issue 6
Extensions beyond the ISO C standard are marked.
The normative text is updated to avoid use of the term "must" for application requirements.
IEEE Std 1003.1-2001 (“POSIX.1”)/Cor 2-2004, item XSH/TC2/D6/124 is applied, updating the DESCRIPTION to clarify the behavior of:
setlocale(LC_ALL, "");
Issue 7
Functionality relating to the Threads option is moved to the Base.