Locale

From ArchWiki
Revision as of 20:39, 22 August 2014 by Pyroh (Talk | contribs) (Troubleshooting: Removed contractions, alphabetized terminals, mentioned st as well.)

Jump to: navigation, search

Locales are used in Linux to define which language the user uses. As the locales define the character sets being used as well, setting up the correct locale is especially important if the language contains non-ASCII characters.

Locale names are defined using the following format:

<lang>_<territory>.<codeset>[@<modifiers>]

Enabling necessary locales

Before a locale can be used on the system, it has to be enabled first. To list all available locales, use:

$ locale -a

To enable a locale, uncomment the name of the locale in the file /etc/locale.gen. This file contains all the available locales that can be used on the system. Revert the process to disable a locale. After the necessary locales are enabled, the system needs to be updated with the new locales:

# locale-gen

To display the locales currently in use, use:

$ locale
Tip: Though it's most likely that just one language is used on your computer, it can be helpful or even necessary to enable other locales as well. If you're running a multi-user system with users that do not speak English, their individual locale should at least be supported by your system.

American English example

First uncomment the following locales in /etc/locale.gen:

/etc/locale.gen
en_US.UTF-8 UTF-8

Then update the system as root:

# locale-gen

Setting the locale system-wide

To define the system-wide locale used on the system, set LANG in /etc/locale.conf.

locale.conf contains a new-line separated list of environment variable assignments: besides LANG, it supports all the LC_* variables, with the exception of LC_ALL.

Tip: If the output of locale is to your liking during installation, you can save a little time by doing: # locale > /etc/locale.conf while chrooted.
/etc/locale.conf
LANG="en_US.UTF-8"

An advanced example configuration would be:

/etc/locale.conf
# Enable UTF-8 with Australian settings.
LANG="en_AU.UTF-8"

# Keep the default sort order (e.g. files starting with a '.'
# should appear at the start of a directory listing.)
LC_COLLATE="C"

# Set the short date to YYYY-MM-DD (test with "date +%c")
LC_TIME="en_DK.UTF-8"

You can set the default locale in locale.conf also using localectl, for example:

# localectl set-locale LANG="de_DE.UTF-8"

See man 1 localectl and man 5 locale.conf for details.

They will take effect after rebooting the system and will be set for individual sessions at login.

Setting 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:

~/.bashrc
export LANGUAGE="en_AU:en_GB:en"

or system-wide:

/etc/locale.conf
LANG="en_AU"
LANGUAGE="en_AU:en_GB:en"

Setting per-user locale

As mentioned earlier, some users might want to define a different locale than the system-wide locale.

The script /etc/profile.d/locale.sh states that if present, the file locale.conf in the folder ~/.config (or if defined, the environment variable XDG_CONFIG_HOME[1]) is sourced instead of /etc/locale.conf. This file, like /etc/locale.conf, must be created by the user.

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

Setting collation

Collation, or sorting, is a little different. Sorting is a goofy beast and different locales do things differently. To get around potential issues, Arch used to set LC_COLLATE="C" in /etc/profile. However, this method is now deprecated. To enable this behavior, simply add the following to /etc/locale.conf:

/etc/locale.conf
LC_COLLATE="C"

Now the ls command will sort dotfiles first, followed by uppercase and lowercase filenames. Note that without a LC_COLLATE setting, locale aware apps sort by LC_ALL or LANG, but LC_COLLATE settings will be overridden if LC_ALL is set. If this is a problem, ensure that LC_ALL is not set by adding the following to /etc/profile instead:

/etc/profile
export LC_ALL=

Note that LC_ALL is the only LC_* variable, which cannot be set in /etc/locale.conf.

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 in /usr/share/i18n/locales/your_locale:

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

And then update the system:

# locale-gen

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 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