Linux – How does the “locale” program work

linuxlocale

The locale program will print the locale variables of the process that launched it, this is a sample output of locale when launched from the shell:

LANG=en_US.UTF-8
LANGUAGE=
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=

Where does locale get this information from (I know that this information is not locale's inherited environment variables from the shell, because the shell only has 4 locale environment variables)?

Best Answer

It gets it by knowing how locale settings are processed, based on the values of the corresponding environment variables.

Taking the GNU version as an example, it starts by calling setlocale (LC_ALL, "") to set the current locale. Then it goes through all the locale categories, printing the value of each one in turn, with special exceptions for LANG (printed first) and LC_ALL (printed last). The values are determined by looking at the environment values and following the rules which apply to the locale settings:

  • if LC_ALL is set, all categories take the corresponding value;
  • if a category has no value set in the environment, it takes the value of LANG if it has one, “POSIX” otherwise and the value is enclosed in double-quotes.
Related Question