24/7/365 Support

Speaking the right language in CentOS

In this process, we will show you how to change the language settings of your CentOS 7 installation for the whole system and for single users. The need to change this is rare but can be important, for example, if we accidentally chose the wrong language during installation.

To Start With: What Do You Need?

For the successful implementation of this process, you will require a working installation of the CentOS 7 operating system with root privileges, and a console-based text editor of your choice. You should have read the Navigating text files with less process because some commands in this process will use less for printing output.

The Process

There are two categories of settings that you have to adjust if you want to change the system-wide language settings of your CentOS 7 system. We begin by changing the system locale information and then the keyboard settings:

  1. To begin, log in as root and type the following command to show the current locale settings for the console, graphical window managers (X11 layout), and also the current keyboard layout:
    localectl status
  2. Next, to change these settings, we first need to know all the available locale and keyboard settings on this system (both commands use less navigation):
    localectl list-locales localectl list-keymaps
  3. If you have picked the right locale from the output above in our example, de_DE.utf8 and keymap de-mac (change to your own appropriate needs), you can change your locale and keyboard settings using:
    localectl set-locale LANG=de_DE.utf8 localectl set-keymap de-mac
  4. Now, verify the persistence of your changes using the same command again:
    localectl status

How it works…

As we have seen, the localectl command is a very convenient tool that can take care of managing all important language settings in a CentOS 7 system.

So what have we learned from this experience?

We started by logging in to our command line with the root user. Then, we ran the localectl command with the parameter status, which gave us an overview of the current language settings in the system. The output of this command showed us that language properties in a CentOS 7 system can be separated into locale (system locale) and keymap (VC keymap and all X11 layout properties) settings.

Locales on Linux are used to set the system’s language as well as other language-specific properties. This can include texts from error messages, log output, user interfaces, and if you are using a window manager such as Gnome, even Graphical User Interfaces (GUI). Locale settings can also define region-specific formatting such as paper sizes, numbers, and their natural sorting, currency information, and so on. They also define character encoding, which can be important if you choose a language that has characters that cannot be found in the standard ASCII encoding.

Keymap settings, on the other hand, define the exact layout of each key on your keyboard.

Next, to change these settings, we first issued the localectl command with the list-locales parameter to retrieve a full list of all locales on the system, and list-keymaps to show a list of all keyboard settings available in the system. Locales as outputted from the list-locales parameter use a very compact annotation for defining a language:
Language[_Region][.Encoding][@Modificator]

Only the Language part is mandatory, all the rest is optional. Examples for language and region are: en_US for English and region the United States or American English, es_CU would be language Spanish and Region Cuba or Cuban Spanish.

Encodings are important for special characters such as German umlaut or accents in the French language. The memory representation of these special characters can be interpreted differently depending on the used encoding type. In general, UTF-8 should be used as it is capable of encoding almost any character in every language.

Modificators are used to change settings defined by the locale. For example, sr_RS.utf8@latin is used if you want to have Latin settings for serbian Serbia, which normally uses Cyrillic definitions. This will change to western settings such as sorting, currency information, and so on.

To change the actual locale, we used the set-locale LANG=de_DE.utf8 parameter. Here, the encoding was selected to display proper German umlauts. Please note that we used the LANG option to set the same locale value (for example, de_DE.utf8) for all available locale options. If you don’t want to have the same locale value for all available options, you can use a more fine-grained control over single locale options. Please refer to the locale description using the man page, man 7 locale (on minimal installation; you need to install all Linux documentation man pages before using the yum install man-pages command). You can set these additional options using a similar syntax, for example, to set the time locale use:
localectl set-locale LC_TIME="de_DE.utf8"

Next, we showed all available keymap codes using the list-keymaps parameter. As we have seen from running localectl status, the keymaps can be separated in non-graphical (VC keymap) and graphical (X11 layout) settings, which allows the flexible configuration of different keyboard layouts when using a window manager such as Gnome and for the console. Running localectl with the parameter, set-keymap de-mac, sets the current keymap to a German Apple Macintosh keyboard model. This command applies the given keyboard type to both the normal VC and the X11 keyboard mappings. If you want different mappings for X11 than for the console, use localectl --no-convert set-x11keymap cz-querty, where we use cz-querty for the keymap code to a Czech querty keyboard model (change this accordingly).

There's more…

Sometimes, single system users need different language settings than the system’s locale (which can only be set by the root user), according to their regional keyboard differences and for interacting with the system in their preferred human language. System-wide locales get inherited by every user as long as they are not overwritten by local environment variables.

Note

Changing system-wide locales does not necessarily have an effect on your user’s locales if they have already defined something else for themselves.

To print all the current locale environment variables for any system user, we can use the command, locale. To set single environment variables with the appropriate variable name; for example, to set the time locale to US time we would use the following line:
export LC_TIME="en_US.UTF-8"

But, most likely we would want to change all the locales to the same value; this can be done by setting LANG. For example, to set all the locales to American English, use the following line:
export LANG="en_US.UTF-8"

To test the effect of locale changes, we can now produce an error message that will be shown in the language set by the locale command. Here is the different language output for changing the locale from English to German:
export LANG="en_US.UTF-8"
ls !

The following output will be printed:
ls: cannot access !: No such file or directory

Now, change to German locale settings:
export LANG="de_DE.UTF-8"
ls !

The following output will be printed:
ls: Zugriff auf ! nicht möglich: Datei oder Verzeichnis nicht gefunden

Setting a locale in an active console using the export command will not survive closing the window or opening a new terminal session. If you want to make those changes permanent, you can set any locale environment variables, such as the LANG variable, in a file called .bashrc in your home directory, which will be read every time a shell is opened. To change the locale settings permanently to de_DE.UTF-8 in our example (change this to your own needs) use the following line:
echo "export LANG='de_DE.UTF-8'" >> ~/.bashrc

 

Help Category:

What Our Clients Say