C:setlocale
lastmod 2024-10-24created 2016-12-119 edits
Sets or gets the runtime locale.
Category
The portion of the locale affected. One of the following constant values defined as macros in clocale.
| value | Portion of the locale affected |
| LC_ALL | The entire locale. |
| LC_COLLATE | Selects the C locale collation category. Affects the behavior of strcoll and strxfrm. |
| LC_CTYPE | Affects the character handling functions (all functions of cctype except isdigit and isxdigit) and the multibyte and wide character functions. |
| LC_MONETARY | Affects the monetary formatting information returned by localeconv. |
| LC_NUMERIC | Affects the decimal-point character in formatted input/output operations and string formatting functions, and the non-monetary information returned by localeconv. |
| LC_TIME | Affects the behavior of strftime. |
locale parameter
-
"C": The minimal "C" locale. -
"": The default locale of the current environment. -
NULL: Does not change the locale environment; lets you check the current value.
Simple example
-
setlocale (LC_ALL, ""); - Sets the locale by consulting the appropriate environment variables and returns its value. Environment variables are consulted in the order
LC_ALL, the other categories, then theLANGvariable. Once the variable value is found, it consultslocale.alias(e.g./usr/share/locale/), then finds an available locale by trimming from the end in units of@ . _, in order.
-
locale = setlocale (LC_ALL, NULL); - An example that uses the return value only to find out the current locale value for a category, without changing the locale value.
Example
#include <cstdio>
#include <clocale>
#include <ctime>
#include <cwchar>
int main()
{
std::setlocale(LC_ALL, "en_US.UTF-8"); // the C locale will be the UTF-8 enabled English
std::setlocale(LC_NUMERIC, "de_DE"); // decimal dot will be German
std::setlocale(LC_TIME, "ja_JP"); // date/time formatting will be Japanese
wchar_t str[100];
std::time_t t = std::time(nullptr);
std::wcsftime(str, 100, L"%A %c", std::localtime(&t));
std::wprintf(L"Number: %.2f\nDate: %Ls\n", 3.14, str);
}
Troubleshooting
warning: setlocale: LC_ALL: cannot change locale
- ubuntu - Linode Lish bash: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8) - Stack Overflow
- Handling the locale error "warning: setlocale: LC_ALL: cannot change locale" on Linux
It is usually printed in bash something like this:
The warning means the system is trying to set the printed locale (in this case "en_US.UTF-8"), but that locale is not installed or not configured correctly.
Check the locales available on the current system:
Ways to fix this are as follows.
Using locale-gen on Debian/Ubuntu-based systems:
On CentOS/RHEL-based systems, use localedef:
Reconfigure with dpkg-reconfigure:
Installing the package on Red Hat-based systems:
Installing the package on Debian-based systems:
If the locale has been generated but the error still occurs, you can set the locale environment variables directly. To do so, add the following lines to ~/.bashrc or /etc/environment:
See also
Favorite site
- MSDN - setlocale, _wsetlocale 1
- Dark Programmer :: How to use setlocale
- IBM Knowledge Center - setlocale() — Set locale
- Joinc - Linux locale settings
- [Recommended] What is a locale 2
References
-
Msdn_-_setlocale_and_wsetlocale.pdf ↩