Locale oddity

Polytropon freebsd at edvax.de
Mon Jul 20 10:54:01 UTC 2020

On Mon, 20 Jul 2020 11:30:33 +0200, Morten Bo Johansen via freebsd-questions wrote:
> On 2020-07-19 Yuri Pankov wrote:
> > I'm not sure what exactly you are asking, so I'll just quote the 
> > specification, 
> > https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html, 
> > for the logic used:
> Well, the reason I was asking was that I was working on a
> script where one of the functions will determine the user's
> language reliably from the locale settings. Therefore I tried
> various error handling, like e.g. the LANG variable not being
> set, etc. and I noticed this behaviour and I wondered why. I
> see the rule #4 you are referring to, but the logic in it is
> still not clear to me: Some script or program might set the
> $LANG variable to an empty value and then the LC_* variables
> that I set myself will be changed behind my back?

No, the variables (being set, and how)won't affect one another.
Rule #4 states that if $LANG is not usable (and no other information
is present), the default "fallback locale" _should_ be used, which
is "C". So the "hierarchy" is this:

	1. LC_ALL
	2. LC_* depending on category
	3. LANG
	4. fallback (usually "C")

Just as an example, if you have not set $LANG, but you have set
the several $LC_* variables, except $LC_ALL, rule #2 applies.
With $LC_ALL set, all $LC_* variables will be overridden, as
says rule #1. If no $LC_<anything> is set, but $LANG is set,
rule #3 applies. If there is nothing set, rule #4 says to use
the implementation-specific locale, which usually is "C".

So the $LC_* variables you've set yourself will always keep
their value. The question simply is: Will a program use them
according to the standard? A program _might_ do the wrong thing
and even though you have set $LC_MESSAGES to "da_DK.UTF-8",
but the program expects and only reads $LANG, it could say
something like "no locale found, falling back to 'C' locale",
which of course is _not_ the correct (!) thing to do... :-)

The common solution is:

Use LC_ALL to define all locale types with the same value.

Use LC_* variables individually, but do not set LC_ALL, if
you want to have different locale interpretations for
different things, like collation information or decimal
notation according to your local language, but messages
in English.

Optionally set LANG to the value of LC_ALL.


You can use the login.conf mechanism to have the system
set the variables for you; for example in ~/.login_conf:


This should work as expected.

Magdeburg, Germany
Happy FreeBSD user since 4.0
Andra moi ennepe, Mousa, ...

More information about the freebsd-questions mailing list