how to set your locale in linux

Locales are used in Linux to specify the language and the character set that should be used by the operating system. It is important to set the locale correctly, so that text can be shown and translated correctly and the characters are rendered appropriately. It is especially important if the character set include non-ASCII characters.

Usually you need to set it only once. But sometimes you would want to change it depending on what you are doing. For example, you might be trying to view a different language albeit for short period of time which is not mapped by the ASCII character set. Many times software and product need the locale to be set appropriately to work correctly.

In Linux and Unix systems you can change, update or set locale by changing the environment variable values. The advantage of having them set in environment variables is that it can be set system wide, on a per user or per session basis.

Format

The locale string follows a standard format in POSIX. It is defined as shown below, where the values inside the square brackets ([..]) are optional.

language[_territory][.charset][@modifier]

language: a two letter code that denotes the language. eg: en for English, fr for French etc.
territory: a two letter code that denotes the country or territory. eg: US for USA, GB for Great Britain or CA for Canada
charset: The character set that denotes the encoding. eg: UTF-8 for 8 bit Unicode or ISO-8859-1 for ASCII etc.
modifiers: Mostly un-used or ignored in many cases. eg: @euro denotes the Euro currency, @latin represents the latin script etc.

All fields expect the language is optional. If one of the fields is omitted, it takes on the default value based on the already specified values in the string.

Some examples are :

en_US.UTF-8: English language in USA with a UTF-8 encoding.
fr_FR.UTF-8: French in France with UTF-8 encoding
de_CH: German in Switzerland with the default character set

Environment Variables

There are several different environment variables that control the locale on your system. The most important ones are LANG and LC_ALL.

LANG: Specifies the default locale of the system. This is used by every process unless the LC_* variables are set and it differs from the LANG value. This is a low priority variable that can be overridden.
LC_ALL: This overrides the LANG variable and any other LC_* variables that are set. This is a high priority variable.

Other locale specific variables are LC_CTYPE, LC_NUMERIC, LC_TIME, LC_COLLATE, LC_MONETARY, LC_MESSAGES, LC_PAPER, LC_NAME, LC_ADDRESS, LC_TELEPHONE, LC_MEASUREMENT, LC_IDENTIFICATION. For almost all practical use-cases, you usually do not have any reason to manually modify any of the variables individually.

Before you set the locale, you might want to check what the current locale settings are. You can do that using the locale command without any arguments. This will print out all the locale specific environments that are currently set.

bash$ locale

Before you can change the locale, it should be enabled and available on the system. You can check all the locales that are currently supported on your system by using the same locale command, with -a or –all-locales as the command line option.

bash$ locale -a

If the locale that want to change to is not listed in the output, then you will need to generate and add it before you can change to that. All the supported locales are listed in the file /usr/share/i18n/SUPPORTED file, print the file and check if the locale is there …

bash$ cat /usr/share/i18n/SUPPORTED

If it is supported, then you will need to generate and add the locale list. In order to generate the new locale, you can use the locale-gen command.

locale-gen uses the configuration file that is at /etc/locale.gen. You will need to modify this file and add the new locale there first in the specified format. Some commonly used locales already exist in the file but are commented out. You can un-comment it or add a new line as shown below. Note that there is no . (dot) between the language and charset, but a space.

fr_FR UTF-8

The locale-gen takes the locale string as a command line argument along with the -G option. As an example, if you want to generate fr_FR.UTF-8 as a locale, then the command is:

bash$ locale-gen -G fr_FR.UTF-8

or you can regenerate all the locales specified in the /etc/locale.gen file, by executing locale-gen with out any arguments.

You can now verify that the new locale is added to the system by using the locale command: locale -a. We will use this locale string  fr_FR.UTF-8 as an example for the rest of the post.

Once you have generated the locale, changing it as just as quick and easy as changing any other environment variable in the system. You can set the environment variable LANG to the desired locale string to set locale of the system.

Current Session

To change the locale in the current session, set the environment variable as below

bash$ set LANG=fr_FR.UTF-8

This settings will not however persist across logins or sessions. In order for that to happen, you will have to change the default locale settings for either the user or set it system wide.

User Based Setting

If you want to change the default locale for a particular user, then modify the profile file in the user’s home directory (eg: ~/.bashrc or ~/.profile or ~/.cshrc). This will make it persist across sessions and will be available once the user re-logins.

export LANG=fr_FR.UTF-8

System Wide

Again, this is no different from setting any other environment variable system wide. In order to change the locale system wide, you will need to set the environment variables for all users. Depending on your distro it could be set in any of the following files:

  • /etc/default/locale
  • /etc/locale.conf
  • /etc/profile.env
  • /etc/csh.env
  • /etc/env.d/*

Check if any of these files do exist and if it contains the entry for the locale. You can then modify that file to insert the newly generated locale string in the appropriate format of the file.