SETLOCALE(3POSIX)
π SETLOCALE(3POSIX) π
okay bestie, welcome to the POSIX Programmer's Manual β this is the setlocale page and she is a whole queen π
quick heads up before we start: the Linux implementation of this interface may differ (go check the corresponding Linux manual page for the deets on Linux behavior), or honestly the interface might not even be implemented on Linux at all. π
β¨ PROLOG β¨
so like, this manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (consult the corresponding Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux. that's it that's the prolog π«Ά
πΈ NAME πΈ
setlocale β set program locale π
π SYNOPSIS π
#include <locale.h>
char *setlocale(int category, const char *locale);
periodt. that's the whole signature.
π¦ DESCRIPTION π¦
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β2017 defers to the ISO C standard. (yes she's a rule-follower, we love that for her β¨)
The setlocale() function selects the appropriate piece of the global locale, as specified by the category and locale arguments, and can be used to change or query the entire global locale or portions thereof. The value LC_ALL for category names the entire global locale; other values for category name only a part of the global locale:
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β affects the affirmative and negative response expressions returned bynl_langinfo()and the way message catalogs are located. It may also affect the behavior of functions that return or write message strings.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"β specifies the minimal environment for C-language translation called the POSIX locale. The POSIX locale is the default global locale at entry tomain()."C"β equivalent to"POSIX". (twin sisters fr fr π―)""β specifies an implementation-defined native environment. The determination of the name of the new locale for the specified category depends on the value of the associated environment variables,LC_*andLANG; see the Base Definitions volume ofPOSIX.1β2017, Chapter 7, Locale and Chapter 8, Environment Variables.- A null pointer β directs
setlocale()to query the current global locale setting and return the name of the locale ifcategoryis notLC_ALL, or a string which encodes the locale name(s) for all of the individual categories ifcategoryisLC_ALL.
setting all of the categories of the global locale is similar to successively setting each individual category of the global locale, except that all error checking is done before any actions are performed. To set all the categories of the global locale, setlocale() can be 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 the Base Definitions volume of POSIX.1β2017, Chapter 8, Environment Variables) 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 global locale 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 global locale established using setlocale() shall only be used in threads for which no current locale has been set using uselocale() or whose current locale has been set to the global locale using uselocale(LC_GLOBAL_LOCALE).
The implementation shall behave as if no function defined in this volume of POSIX.1β2017 calls setlocale().
β οΈ big one: the setlocale() function need not be thread-safe. iykyk π
π 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 global locale shall not be changed.
A null pointer for locale shall cause setlocale() to return a pointer to the string associated with the specified category for the current global locale. The global locale 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 global locale. The application shall not modify the string returned. The returned string pointer might be invalidated or the string content might be overwritten by a subsequent call to setlocale(). The returned pointer might also be invalidated if the calling thread is terminated. (so copy it if you care bestie, don't be out here holding onto a dangling pointer π« )
π ERRORS π
No errors are defined. iconic honestly.
The following sections are informative.
π EXAMPLES π
None. π
π§ APPLICATION USAGE π§
The following code illustrates how a program can initialize the international environment for one language, while selectively modifying the global locale 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 can initiate language operation according to environment variable settings (see the Base Definitions volume of POSIX.1β2017, Section 8.2, Internationalization Variables) 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().
In order to make use of different locale settings while multiple threads are running, applications should use uselocale() in preference to setlocale(). (main character energy: uselocale() β¨)
π RATIONALE π
References to the international environment or locale in the following text relate to the global locale for the process. This can be overridden for individual threads using uselocale().
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.
π querying, bestie π
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. (yes this is the same warning again, she's serious about it π«Ά)
π the three ways to set it π
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 (the bestie reading list) β¨
okay so like, if you wanna go deeper (and you SHOULD, these are all lowkey iconic), go check out:
catopen()execfprintf()fscanf()isalnum()isalpha()isblank()iscntrl()isdigit()isgraph()islower()isprint()ispunct()isspace()isupper()iswalnum()iswalpha()iswblank()iswcntrl()iswctype()iswdigit()iswgraph()iswlower()iswprint()iswpunct()iswspace()iswupper()iswxdigit()isxdigit()localeconv()mblen()mbstowcs()mbtowc()newlocale()nl_langinfo()perror()psiginfo()strcoll()strerror()strfmon()strsignal()strtod()strxfrm()tolower()toupper()towlower()towupper()uselocale()wcscoll()wcstod()wcstombs()wcsxfrm()wctomb()
and ALSO, bestie, the actual lore lives in these:
- The Base Definitions volume of POSIX.1β2017, Chapter 7, Locale, Chapter 8, Environment Variables,
<langinfo.h>,<locale.h>
likeβ¦ that's the syllabus. that's the reading. iykyk π
π COPYRIGHT (the legal tea, not me being dramatic) π«Ά
okay so legally speaking, some of this text is reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. π«‘
and if there's EVER any discrepancy between this version and the original IEEE and The Open Group Standard β bestie, the original IEEE and The Open Group Standard is the referee document. no cap. it wins, every time. the original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
also, any typographical or formatting errors that show up in this page are most likely from the conversion of the source files into man page format (the glow-up was imperfect, okay π). to report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
and that's the whole page, bestie!! πβ¨π
this one's brought to you by IEEE/The Open Group, dated 2017, filed under SETLOCALE(3POSIX) β so yeah, it's giving officially certified, Open Group approved, main character documentation energy ππ no cap, go set some locales, ily, bye π¦πΈ
π¦ where this page comes from
- debian bookworm: manpages-posix-dev 2017a-2 Β· Other
- debian trixie: manpages-posix-dev 2017a-2 Β· Other
- ubuntu jammy: manpages-posix-dev 2017a-2 Β· Other
- ubuntu noble: manpages-posix-dev 2017a-2 Β· Other
- ubuntu resolute: manpages-posix-dev 2017a-2build1 Β· Other
- ubuntu stonking: manpages-posix-dev 2017a-2.1 Β· Other
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms