Locale

From ArchWiki
Revision as of 19:30, 14 December 2014 by Gen2ly (talk | contribs) (→‎LANGUAGE: fallback locales: Details for LC_TIME and using YYYY-MM-DD added.)

zh-CN:Locale Locales define which language the system uses and other regional considerations like currency denomination, numerology and character sets.

Generating locales

Before a locale can be enabled on the system, it has to be generated. The current generated/available locales can be viewed with:

$ locale -a

The locales that can be generated are listed in the /etc/locale.gen file: their names are defined using the format [language][_TERRITORY][.CODESET][@modifier]. To generate a locale, first uncomment the corresponding line in the file (or comment to remove); when doing this, also consider the localizations that other users on the system might need. For example, for American-English uncomment en_US.UTF.8 UTF-8. When done, save the file and generate the uncommented locale(s) by executing:

# locale-gen

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 which must contain a new-line separated list of environment variable assignments, for example:

locale.conf
LANG=en_AU.UTF-8
LC_COLLATE=C
LC_TIME=en_DK.UTF-8
  • A system-wide locale can be set by creating or editing /etc/locale.conf. The same result can be obtained with the localectl command:
# localectl set-locale LANG=en_US.UTF-8
See man 1 localectl for details.
Tip: During system installation, if the output of locale is to your liking, you can save a little time by doing: locale > /etc/locale.conf while chrooted.
  • 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).
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.

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

See #Supported variables, man 5 locale.conf and related for details.

Once locale.conf files have been created or edited, their new values will take effect after rebooting the system and will be set for individual sessions at login. To have the current environment use the new settings, do:

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

Other uses

Locale variables can also be defined with the standard methods as explained in Environment variables.

For example, in order to test or debug a particular application during development, it could be launched with something like:

$ LANG="en_AU.UTF-8" ./my_application.sh

Supported variables

locale.conf files support the following environment variables:

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

LANG: default locale

The locale set for this variable will be used for all the LC_* variables that are not explicitly set.

LANGUAGE: fallback locales

Programs which use gettext for translations respect the LANGUAGE option in addition to the usual variables. This allows users to specify a list of locales that will be used in that order. If a translation for the preferred locale is unavailable, another from a similar locale will be used instead of the default. For example, an Australian user might want to fall back to British rather than US spelling:

locale.conf
LANG=en_AU
LANGUAGE=en_AU:en_GB:en

LC_TIME: date and time format

If LC_TIME is set to en_US.UTF-8, for example, the date format will be "MM/DD/YYYY". If wanting to use the the ISO 8601 date format of "YYYY-MM-DD" use:

locale.conf
...
LC_TIME=en_DK.UTF-8

LC_COLLATE: collation

This variable governs the collation rules used for sorting and regular expressions.

Setting the value to C can for example make the ls command sort dotfiles first, followed by uppercase and lowercase filenames:

locale.conf
LC_COLLATE=C

See also [1].

To get around potential issues, Arch used to set LC_COLLATE=C in /etc/profile, but this method is now deprecated.

LC_ALL

The locale set for this variable will always override LANG and all the other LC_* variables, whether they are set or not.

LC_ALL is the only LC_* variable, which cannot be set in locale.conf files: it is meant to be used only for testing or troubleshooting purposes, for example in /etc/profile.

Customizing locales

Locales are defined in text files located in /usr/share/i18n/locales/ and can be edited to adapt to particular needs.

After editing a locale file, do not forget to re-generate the locales for the changes to take effect after reboot.

Setting the first day of the week

In many countries the first day of the week is Monday. To adjust this, change or add the following lines:

/usr/share/i18n/locales/chosen_locale
LC_TIME
[...]
week            7;19971130;5
first_weekday   2
first_workday   2

Tips and tricks

Launch application with different locale from terminal

For example launch Abiword with Hebrew locale:

$ env LANG=he_IL.UTF-8 abiword &

Launch application with different locale from desktop

Copy the .desktop file to the user home directory so it will take precedence:

$ cp /usr/share/applications/abiword.desktop ~/.local/share/applications/

Edit the Exec command:

~/.local/share/applications/abiword.desktop
Exec=env LANG=he_IL.UTF-8 abiword %U

Troubleshooting

My terminal does not support UTF-8

The following lists some (not all) terminals that support UTF-8:

  • gnustep-terminal
  • konsole
  • mlterm
  • rxvt-unicode
  • st
  • vte-based terminals
  • xterm - Must be run with the argument -u8. Alternatively run uxterm, which is provided by the package xterm.

Gnome-terminal or rxvt-unicode does not support UTF-8

You need to launch these applications from a UTF-8 locale or they will drop UTF-8 support. Enable the en_US.UTF-8 locale (or your local UTF-8 alternative) per the instructions above and set it as the default locale, then reboot.

Changed everything and my GNOME is still using wrong language?

Some GUI tools use ~/.pam_environment as a place where environment variables are defined. GNOME reads this file.

See also