Locale

Locales are used by glibc and other locale-aware programs or libraries for rendering text, correctly displaying regional monetary values, time and date formats, alphabetic idiosyncrasies, and other locale-specific standards.

Generating locales

Locale names are typically of the form language[_territory][.codeset][@modifier], where language is an ISO 639 language codeterritory is an ISO 3166 country code, and codeset is a character set or encoding identifier like ISO-8859-1 or UTF-8. See setlocale(3).

For a list of enabled locales, run:

locale -a 

Before a locale can be enabled on the system, it must be generated. This can be achieved by uncommenting applicable entries in /etc/locale.gen, and running locale-gen. Equivalently, commenting entries disables their respective locales. While making changes, consider any localisations required by other users on the system, as well as specific #Variables.

For example, uncomment en_US.UTF-8 UTF-8 for American-English:

etc/locale.gen
...
#en_SG ISO-8859-1
en_US.UTF-8 UTF-8
#en_US ISO-8859-1
... 

Save the file, and generate the locale:

 locale-gen

Note:
locale-gen also runs with every update of glibc[1]
UTF-8 is recommended over other character sets. [2]

Setting the locale

To display the currently set locale and its related environmental settings, type:

locale 

The locale to be used, chosen among the previously generated ones, is set in locale.conf files. Each of these files must contain a new-line separated list of environment variable assignments, having the same format as output by locale.

To list available locales which have been previously generated, run:

localedef --list-archive

Alternatively, using localectl(1):

localectl list-locales

Setting the system locale

To set the system locale, write the LANG variable to /etc/locale.conf, where en_US.UTF-8 belongs to the first column of an uncommented entry in /etc/locale.gen:

 /etc/locale.conf
LANG=en_US.UTF-8

Alternatively, run:

localectl set-locale LANG=en_US.UTF-8 

See #Variables and locale.conf(5) for details.

Overriding system locale per user session

The system-wide locale can be overridden in each user session by creating or editing ~/.config/locale.conf (or, in general, $XDG_CONFIG_HOME/locale.conf or $HOME/.config/locale.conf).

The precedence of these locale.conf files is defined in /etc/profile.d/locale.sh.

Tip:
This can also allow keeping the logs in /var/log in English while using the local language in the user environment.
You can create a /etc/skel/.config/locale.conf file so that any new users added using useradd and the -m option will have ~/.config/locale.conf automatically generated.

Make locale changes immediate

Once system and user locale.conf files have been created or edited, their new values will take effect for new sessions at login. To have the current environment use the new settings unset LANG and source /etc/profile.d/locale.sh:

$ unset LANG
$ source /etc/profile.d/locale.sh

 

Note: The LANG variable has to be unset first, otherwise locale.sh will not update the values from locale.conf. Only new and changed variables will be updated; variables removed from locale.conf will still be set in the session.

Variables

locale.conf files support the following environment variables:

  • LANG
  • LANGUAGE
  • LC_ADDRESS
  • LC_COLLATE
  • LC_CTYPE
  • LC_IDENTIFICATION
  • LC_MEASUREMENT
  • LC_MESSAGES
  • LC_MONETARY
  • LC_NAME
  • LC_NUMERIC
  • LC_PAPER
  • LC_TELEPHONE
  • LC_TIME

Full meaning of the above LC_* variables can be found on manpage locale(7), whereas details of their definition are described on locale(5).

Setting up user local

As we mentioned earlier, some users may want to define a locale other than the local system level.

The script /etc/profile.d/locale.sh replaces the locale ‘at system level’ ‘with the one found in ~/.config/locale.conf. This file does not exist by default.

nano ~/.config/locale.conf
LANG="en_US.UTF-8"
LANGUAGE="en_US.UTF-8" 

 

Note:  ~/.config/locale.conf does not exist by default and must be created manually.

 

Calogero Scarnà
Calogero Scarnà
Articoli: 299

Newsletter

Inserisci il tuo nome e il tuo indirizzo email qui sotto e iscriviti alla nostra newsletter