Understanding and configuring `locale`

Understanding and configuring locale

locale ubuntu ssh 20.04 18.04

ubuntu (like most GNU/Linux distributions) is trying hard to speak your language, even in the console, and not only the language, but also the date and number formatting.

For instance:

  • 15,000 is 15 for an European*1 and 15E3 for an American.
  • 09/08 may be September 8 or August 9*2.

Background

Programs use environment variables to try to translate program output. Those environment variables are named LANGUAGE, LC_ALL, LC_* (LC_TIME, LC_MONETARY, …) and LANG.

Those variables contains the name of the locale.

Example, show the current date format:

echo LC_TIME=$LC_TIME
LC_TIME=en_US.UTF-8

Show the date of a file, using the current date format:

ls -lh /dev/ttyS0
crw-rw---- 1 root dialout 4, 64 Aug 10 11:58 /dev/ttyS0

Show the date of a file, forcing the date format:

LC_TIME=fr_CH.UTF-8 ls -lh /dev/ttyS0
crw-rw---- 1 root dialout 4, 64 aoû 10 11:58 /dev/ttyS0

Configuring the locales

Using system tools

Select the local you need.

sudo dpkg-reconfigure locales

Manual setup

Edit /etc/default/locale as you want:

LANG=en_US.UTF-8
LC_NUMERIC="fr_CH.UTF-8"
LC_TIME="en_DK.UTF-8"
LC_MONETARY="fr_CH.UTF-8"
LC_PAPER="fr_CH.UTF-8"
LC_NAME="fr_CH.UTF-8"
LC_ADDRESS="fr_CH.UTF-8"
LC_TELEPHONE="fr_CH.UTF-8"
LC_MEASUREMENT="fr_CH.UTF-8"
LC_IDENTIFICATION="fr_CH.UTF-8"

Edit /etc/locale.gen, uncomment the locales you need:

#...
# de_CH ISO-8859-1
de_CH.UTF-8 UTF-8
# de_DE ISO-8859-1
#...
# en_DK.ISO-8859-15 ISO-8859-15
en_DK.UTF-8 UTF-8
# en_GB ISO-8859-1
#...
# en_US.ISO-8859-15 ISO-8859-15
en_US.UTF-8 UTF-8
# en_ZA ISO-8859-1
#...
# fr_CH ISO-8859-1
fr_CH.UTF-8 UTF-8
# fr_FR ISO-8859-1
#...

Generate the locales with sudo locale-gen, if all en locales are generated, you may want to remove /var/lib/locales/supported.d/local/en.

Generating locales (this might take a while)...
  de_CH.UTF-8... done
  en_DK.UTF-8... done
  en_US.UTF-8... done
  fr_CH.UTF-8... done
Generation complete.

Remote locales or perl: warning: Please check that your locale settings

You may have seen that kind of warning:

perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
 LANGUAGE = "en_US:en",
 LC_ALL = (unset),
 LC_TIME = "fr_CH.UTF-8",
 LC_MONETARY = "fr_CH.UTF-8",
 LC_ADDRESS = "fr_CH.UTF-8",
 LC_TELEPHONE = "fr_CH.UTF-8",
 LC_NAME = "fr_CH.UTF-8",
 LC_MEASUREMENT = "fr_CH.UTF-8",
 LC_IDENTIFICATION = "fr_CH.UTF-8",
 LC_NUMERIC = "fr_CH.UTF-8",
 LC_PAPER = "fr_CH.UTF-8",
 LANG = "en_US.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to a fallback locale ("en_US.UTF-8").
locale: Cannot set LC_ALL to default locale: No such file or directory

It is usually caused by a connection through ssh, where the host has configured some locales that are unavailable on the remote system.

Solution #1 : add the missing locales

  1. Connect to the remote host
  2. Edit /etc/locale.gen, add the missing locales
  3. Generate the locales using sudo locale-gen
  1. Edit /etc/ssh/ssh_config (or your user’s ~/.ssh/config) and add :
    SendEnv LANG LC_*
    
  1. Edit /etc/ssh/sshd_config (dont’ forget the d) and remove LANG and LC_* from the accepted environment variable:
    #AcceptEnv LANG LC_*
    

While you’re configuring ssh, have a look at ssh keepalive.


*1 My European view may be based on the fr_CH locale.
*2 Dates can be clearly defined using the ISO 8601 format.

~~~

Question, remark, bug? Don't hesitate to contact me or report a bug.