Skip to content
Search

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 the LANG variable. Once the variable value is found, it consults locale.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

It is usually printed in bash something like this:

bash: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8)

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:

locale -a

Ways to fix this are as follows.

Using locale-gen on Debian/Ubuntu-based systems:

sudo locale-gen en_US.UTF-8
sudo update-locale LANG=en_US.UTF-8

On CentOS/RHEL-based systems, use localedef:

sudo localedef -v -c -i en_US -f UTF-8 en_US.UTF-8

Reconfigure with dpkg-reconfigure:

sudo dpkg-reconfigure locales

Installing the package on Red Hat-based systems:

yum install glibc-langpack-en

Installing the package on Debian-based systems:

sudo apt install language-pack-en

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:

export LANG=en_US.UTF-8
export LANGUAGE=en_US.UTF-8
export LC_ALL=en_US.UTF-8

See also

Favorite site

References


  1. Msdn_-_setlocale_and_wsetlocale.pdf ↩

  2. About locales... ↩

author Zer0 sitemap