Dealing with missing locales on remote hosts

Vincent Bernat

On my sys­tem, I hap­pen to set LANG to fr_​FR.​utf8 and LC_​MESSAGES to en_​US.​utf8. This means that ap­pli­ca­tions should fol­low French cul­tural con­ven­tions for most things ex­cept for mes­sages which should be dis­played in US Eng­lish. On my own sys­tem, /etc/​locale.​gen con­tains these two lo­cales. How­ever, when I con­nect to some ran­dom re­mote sys­tem, they may be un­avail­able. Most ap­pli­ca­tions will fall back silently to C. How­ever, Perl can be quite noisy:

$ perl -e 'print "Hello\n";'
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
        LANGUAGE = (unset),
        LC_ALL = (unset),
        LC_MESSAGES = "en_US.utf8",
        LANG = "fr_FR.utf8"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
Hello

This is an in­cred­i­bly an­noy­ing mes­sage. I don’t un­der­stand why we still need to bear it. Perl doc­u­men­ta­tion ex­plains how to get rid of this mes­sage. The sim­plest way is to set PERL_​BADLANG en­vi­ron­ment vari­able to 0. Prob­lem solved.

$ PERL_BADLANG=0 perl -e 'print "Hello\n";'
Hello

Well, no. Keep read­ing. When you con­nect to a re­mote host with ssh, all your en­vi­ron­ment vari­ables are thrown away, ex­cept ones de­fined in AcceptEnv di­rec­tive in /etc/​ssh/​sshd_​config file on the re­mote host. On De­bian, this di­rec­tive de­faults to LANG LC_*. This means that PERL_​BADLANG will not be trans­mit­ted on the re­mote sys­tem. Back to square one.

Since I can­not in­stall my favourite lo­cales on all hosts or fid­dle en­vi­ron­ment vari­ables on them, I use some­thing like this in my .zshrc:

ssh() {
  [ -t 1 ] && echo -ne "\033]0;$@\007"
  LANG=C LC_MESSAGES=C =ssh "$@"
}

Un­for­tu­nately, on sys­tems where my lo­cales are present, I still fall back to the crappy C lo­cale. I could just un­set LANG and LC_​MESSAGES to al­low fall­back to the proper de­fault lo­cale but if the re­mote sys­tem starts to speak Span­ish, I will not be pleased. More­over, the be­hav­ior of C lo­cale (also known as POSIX lo­cale) is un­de­fined with char­ac­ters not in the por­ta­ble char­ac­ter set. From IEEE Std 1003.1:

Con­form­ing sys­tems shall pro­vide a POSIX lo­cale, also known as the C lo­cale. The be­hav­iour of stan­dard util­i­ties and func­tions in the POSIX lo­cale shall be as if the lo­cale was de­fined via the localedef util­ity with in­put data from the POSIX lo­cale ta­bles in Lo­cale Def­inition.

The ta­bles in Lo­cale Def­i­ni­tion de­scribe the char­ac­ter­is­tics and be­hav­iour of the POSIX lo­cale for data con­sist­ing en­tirely of char­ac­ters from the por­ta­ble char­ac­ter set and the con­trol char­ac­ter set. For other char­ac­ters, the be­hav­iour is un­spec­i­fied. For C-lan­guage pro­grams, the POSIX lo­cale shall be the de­fault lo­cale when the setlocale() func­tion is not called.

The POSIX lo­cale can be spec­i­fied by as­sign­ing to the ap­pro­pri­ate en­vi­ron­ment vari­ables the val­ues C or POSIX.

There is no way to de­tect the lo­cales in­stalled on the re­mote sys­tem un­less you con­nect to it. Start­ing a con­nec­tion just to check the lo­cale is too ex­pensive.

On my own sys­tems, I use an­other snip­pet in .zshrc to re­con­fig­ure the lo­cales to my favourite ones if they are available:

export LANG=C
export LC_MESSAGES=C
(( $+commands[locale] )) && function {
    local available
    local locales
    local locale
    locales=( "LANG fr_FR.utf8 en_US.utf8 C.UTF-8 C" \
              "LC_MESSAGES en_US.utf8 fr_FR.utf8 C.UTF-8 C" )
    available=("${(f)$(locale -a)}")
    for locale in $locales; do
        for l in $=locale[(w)2,-1]; do
            if (( ${available[(i)$l]} <= ${#available} )); then
                export $locale[(w)1]=$l
                break
            fi
        done
    done
    unset LC_ALL
} 2> /dev/null

This is an in­ter­est­ing snip­pet which uses some Zsh features.

  • The use of (( $+commands[locale] )) is a way to check if the locale com­mand ex­ists. Briefer than if $(which locale >& /​dev/null)
  • The use of an anony­mous func­tion to avoid clut­ter­ing the global en­vi­ron­ment with variables.
  • The use of word split­ting. $locale[(w)1] con­verts $locale to an ar­ray and pops the first item. $=locale[(w)2,-1] con­verts $locale to an ar­ray, keeps the tail and ap­plies word split­ting to it (by de­fault, zsh does not au­to­mat­i­cally ap­ply word split­ting to un­quoted variables).

Feel free to tell me if you have found a bet­ter way to han­dle this problem!