Your understanding is partly right, but only partly. One thing you're missing to understand this is how locale settings are used. Locale settings are used by various library functions that perform locale-dependent actions such as translating messages (which uses LC_MESSAGES
), formatting numbers (LC_NUMERIC
) and dates (LC_TIME
, encoding and decoding text (LC_CTYPE
), sorting text (LC_COLLATE
), etc.
Take for example a function that formats a date. If asked to use a locale-dependent format, it will look up the rules for date formatting in the locale that was configured for use in the current process. The date formatting doesn't care about a locale name, what it needs to know is how to format the date. So while it does look in what you could call a “locale table”, this table doesn't contain names (e.g. LC_TIME
is fr_FR
) but settings (e.g. “the short date format uses the order day-month-year, the long date format uses the month names janvier
, février
, …”).
The C function setlocale
fills some entries in the process's locale settings table. It takes two arguments: a category to fill, and a string which is a name given to a particular value for these locale settings. The string is basically a file name to load the settings from. For example, setlocale(LC_TIME, "fr_FR")
basically means “load date formatting settings into the process's locale table from the file /usr/share/i18n/locales/fr_FR
” (it's more complicated than that, other files are involved, but that's the basic idea).
The C function setlocale
has a mode of operation where it will look up environment variables. If you give it an empty string instead of a name, it will determine a locale name based on the locale environment variable hierarchy. This mode is what most programs use. Once again, the environment variables and the locale names influence how setlocale
works, not how functions that perform locale-dependent actions work.
The locale settings table is a feature of the standard library (libc
) which almost all programs are linked against (regardless of which language(s) they're written in). Most languages provide a way to set it by calling the standard library's setlocale
function. For example, Perl and Python both have a setlocale
function which resembles C's. High-level languages also typically have a way to set locale settings based on the environment, for example use locale
in Perl; in bash it's automatic but the locale settings are not based on the environment but on the shell variables of the same name (so setting e.g. LC_COLLATE
has an effect in bash even if you don't export
it).
setlocale()
. If it callsselocale()
, it may call it with the second argument set to""
to get the values from the environment, or set to"C"
to explicitly ignore them. It does not have to callsetlocale()
at all; if it doesn't, it behaves as if it calledsetlocale (LC_ALL, "C")
. It may look at the environment variables directly. It may also ignore them. The function callsetlocale()
is specific to the C programming language; other language may or may not offer comparable mechanisms. – AlexP Jun 11 '18 at 17:50