How to change languages in GNU/Linux and UNIX systems
Contents:
Understanding Locales 1
Checking your current locale 2
Changing your locale in the shell 3
Adding locales to your system 6
Setting the language for only one program 6
Changing the Language at Login 6
Setting the language at the GNOME login 7
Setting the language at the KDM login 7
Setting the language with other Display Managers 7
Logging out of X Windows 7
Installing language packages and spell-checkers 8
Changing the locale when programming 8
Setting environment variables for your system while programming 9
In GNU/Linux y most UNIX-based operating systems, users can easily change their interface language. Unlike in Windows, most UNIX programs are generally available in multiple languages without extra cost, so you don't have to buy a different version of the software every time you want to use another language.
Because the way that language, numbers, money, measurements and other things are displayed to the user can vary, Linux/UNIX uses keeps track of these differences through locales. The locale specifies not only the language being currently used, but also the country and the character set being used.
In order to avoid confusion, the
language in Linux/UNIX is represented by two letters in lower case as
specified by the ISO
639-1
language codes. For instance Spanish is represented as "es
"
and French as "fr
".
Unfortunately, two letters wasn't enough to represent the nearly 7000
human languages, so many of the lesser known languages such as
Mapudungun ("arn
") use
the three letter language codes of the ISO-639-3.
In a similar fashion the country is also represented in Linux/UNIX by using two letters in upper-case, as specified by the ISO 3166-1 alpha-2 country codes. It is necessary to specify the country in the locale because many languages vary according to the country. For instance, English in Great Britain uses a different spelling than English in the United States. Similarly, the German in Switzerland is a different dialect than the German spoken in the majority of Germany.
The locale also tells the system what character set is being used. Computers store characters as numbers. For instance. the letter "A" is generally stored as the number 65 and the letter "z" is stored as 122. Almost all computers today use the same numbers to store the first 128 characters, which contain the English alphabet, spaces, numbers, a few common symbols and some special control characters like End-of-File, Delete and Escape. Roman letters with diacritical marks such "ë" and "ú" and all languages which don't use Roman letters are stored as 128 or higher. The problem is that these higher numbers can represent different characters depending on the character set being used. For instance, in the ISO-8859-1 character set which is used for most Western European languages, the number 228 represents the character "ä", but it represents the character "ה" in the ISO-8859-8 character set which is used for Hebrew. Unfortunately, a problem arose when people needed to use characters found in different sets. The character "ä" could not be used in the same document as the character "ה" because they were both stored as the same number. For this reason many distributions of Linux/UNIX have begun switched to the 8-bit Unicode (UTF-8) character set which is capable of representing all the characters known to humankind. To see all the character sets which your computer can use:
locale -m | more
(Press the spacebar to see one page at a time or Q to exit)
In order to see which language is currently being used by your Linux/UNIX system, at the command prompt enter:
echo $LANG
On my computer this command returns:
en_US.UTF-8
Linux/UNIX lists the locale in the format:
ll_CC.CHARSET
where ll
is the 2 or 3 letter language code in lowercase, CC
is the two letter country code in upper case and CHARSET
is the name of the the character set. In my case, I am using English
from the United States in the 8-bit Unicode (UTF-8) character set.
Often the character set will be omitted. If not specified, most
Western European languages will use the ISO-8859-1 character set by
default, while others will use the character set most appropriate for
their language and country, such as GB18030 for Mainland Chinese and
ISO-8859-5 for Russian.
Not only does Linux/UNIX keep track of the language, it also sets the type of numbers, money, measurements, paper size and many other things according to your locale. To see all these settings, use the locale command:
locale
On my computer I see:
$
locale
LANG=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=
Probably you will only care about the LANG setting, but the other setting can be useful if you want to use the type of measurements, money, addresses, etc found in a different language and country. Below are the meanings of the different settings:
|
Language. |
|
Character classification and case conversion. |
|
Collation order. |
|
Date and time formats. |
|
Non-monetary numeric formats. |
|
Monetary formats. |
|
Formats of informative and diagnostic messages and interactive responses. |
|
Paper size. |
|
Name formats. |
|
Address formats and location information. |
|
Telephone number formats. |
|
Measurement units (Metric or Other). |
|
Metadata about the locale information. |
LANG and the other locale settings are
environment variables on your system. If you would like to change
them, you will need to use the export
command to set environment variables. To change the language:
export LANG="ll_CC.CHARSET"
For instance if you would like to set the language to Spanish from Spain, you would use the command:
export LANG="es_ES"
The ISO-8859-1 character set is the default for es_ES locale so there is no need to specify it. If you want to use the UTF-8 set, however, you will need to specify it:
export LANG="es_ES.UTF-8"
Since the introduction of the Euro, many people in Europe have found that the ISO-8859-1 character set. is no longer adequate because it lacks the new Euro sign (€). In this case, they need the new ISO-8859-15 character set. It can be set like a normal character set or by appending "@euro":
export LANG="es_ES.ISO-8859-15"
Or:
export LANG="es_ES@euro"
You can set each locale variable separately. For instance if you want to use the type of address found in Laos:
export LC_ADDRESS="lo_LA"
You can also set all the locale variables (except for LANG) at the same time with LC_ALL:
export LC_ALL="lo_LA"
You will have to set the LANG environment variable separately:
export LANG="lo_LA"
For the rest of the current shell session you will use the Loa from Loas locale, but it will revert to your original locale when you logout or reboot the computer. Any program which you run from your current shell session (which is the the window with your command prompt) will now appear in Lao (provided that the program has a translation and the translation has been installed on your system). However, the language will only be set to Lao for the user in the current shell session. If you change users or leave the current shell session, you will notice that you are no longer using Lao.
If you want to always use the Lao
language every time you open a shell, you should set the language in
the initialization file for your shell. If you are running GNU/Linux
or MAC OS X, you are most likely using the bash shell and can set the
language in the .bashrc
file which
is run every time a new shell is opened. .bashrc
is a hidden file found in your HOME
directory which will normally be found at
/home/
your_username
/.bashrc
.
Use your favorite plain text editor (e.g., nano, gedit, kate, vi,
emacs, etc.) to add the following line to .bashrc
:
export LANG="lo_LA"
Linux/UNIX is designed to be a
multi-user system, so different users can select different locales. A
Spanish speaker and a Chinese speaker can use the same system in
their native languages without problems. If you want all new users to
use the Laos language in their shell by default, you should add the
line export LANG="lo_LA"
to the file /etc/skel/.bashrc
, so
it will automatically be copied to the HOME
directory of all new users which are created in the future.
If you are using the X Windows graphical environment, you will also notice that setting the language in the shell won't change the language of X Windows or the programs which are executed from its menus. If you would like to change the language for your entire graphical environment, you will need to logout of X Windows and select Lao as your language the next time that you login. See the section Changing the Language at Login below.
After setting a locale variable in your
shell, it is a good idea to check whether it was set properly with
the locale
command:
$
export LC_ALL="xx_XX"
$ locale
locale: Cannot set
LC_ALL to default locale: No such file or directory
If you see the above message informing you that there is "no such file or directory", it either means that you tried to use a locale which isn't supported or the files for that locale are not installed on your system.
Installing all the supported locales takes up roughly 50MB in the harddrive. In order to save space, many people only install a few locales since they won't need the vast majority.
To find out whether your locale is
supported, look for it in the SUPPORTED
file:
more /usr/share/i18n/SUPPORTED
You can also search for a locale with
the grep
command:
grep ll_CC /usr/share/i18n/SUPPORTED
Each locale in the SUPPORTED
file is listed along with its character set, which is handy if you
don't know which character set is used by default for a locale. If
you don't know what the ISO language and country codes mean you will
have to look them up on the internet (see the section above
Understanding Locales). If you have the GNOME display manager
installed on your system, you don't have to look up the meaning of
the ISO language and country codes, because their meanings are listed
next to the codes in the /etc/gdm/locale.conf
file. To see all the supported locales:
more /etc/gdm/locale.conf
To search for the meaning of a
particular locale, use the grep
command:
grep ll_CC /etc/gdm/locale.conf
After you verify that a locale is supported, use the locale command to list all the locales which have been installed on your system:
locale -a
You can also check which locales have
been installed on your system by looking in the /usr/lib/locale
directory (or if not found at that directory, it will be located in
the path fond in $LOCPATH
).
If you have trouble setting a locale to
use a particular character set, you may want to check if that
character set has been installed on your system with the locate
command:
locale -m
Additional locales can be installed on
your system with the locale-gen
command (as long as they are found in the list of supported locales).
For instance. to install French from France with the ISO-8859-15
character set:
locale-gen fr_FR.ISO-8859-15
If a locale doesn't function correctly,
the problem might be that some of its files have been deleted or
corrupted, In this case you may need to recompile and reinstall the
locale files with the localdef
command:
localedef -c -v -f ISO-8859-15 -i fr_FR fr_FR.ISO-8859-15
If you only want to change the language for one program, you can set the language when executing that program. For instance if you want to use OpenOffice Writer in Italian:
LANG="it_IT" oowriter
Some programs may not detect the LANG
variable and will use the locale for LC_MESSAGES
.
In this case it is easier to set LC_ALL
:
LC_ALL="it_IT" oowriter
For some reason on my system the locale can't be set when executing a program with sudo, such as:
sudo LC_ALL="it_IT" oowriter
If you encounter the same problem, you
can get around this by using sudo -i
to login, then exit sudo after executing the program:
sudo
-i
LC_ALL="it_IT" oowriter &
exit
Often the language on your Linux/UNIX
system can be changed when logging in. The graphical environment in
Linux/UNIX is built on the X Window System which by default uses the
XDM
Display Manager to login users. Unfortunately XDM does not provide a
nice menu to allow users to select their interface language at login,
so you will have to edit the XDM configuration file to change the
default user interface language. Usually the configuration file is
found at /usr/var/X11/xdmr/xdm-config
or /usr/lib/X11/xdm/xdm-config
. To
set the locale at login, specify it with the DisplayManager*language
setting. For instance, if you would like to login using Spanish from
Argentina in the UTF-8 character set, you would add the following
line to xdm-config
:
DisplayManager*language: es_AR.UTF-8
Fortunately, few systems use the XDM Display Manager to login nowadays, because desktop environments such as GNOME offer much nicer and more configurable logins. The GNOME Display Manager (GDM) allows users to select their language at login from the Language menu which is usually found at the bottom of the screen. If you do not see your language in the list, you will have to add it to your list of installed locales. See the section Adding locales to your system.
If you want to change the language which GDM appears in, you can set the LANG variable in the /etc/default/gdm file. For instance, to change your language to French from Canada using the ISO-8859-1 character set, you would add the line:
LANG="fr_CA.ISO-8859-1"
To find out what locales are available to be used by GDM, see the contents of the file /etc/gdm/locale.conf. If you specify a locale which hasn't been installed in your system, GDM will be displayed in English because the system defaults to the "C" locale .
Like GDM, the KDE
Display Manager
(KDM) has a user-friendly Language menu which allows you to select
the language at login. If you want to set the language in which KDM
will be displayed, edit the kdmrc file which is usually found at
/etc/kde3/kdm/kdmrc
if using KDE 3
or /etc/kde4/kdm/kdmrc
if using KDE
4. Inside the [X-*-Greeter]
section, set the Language variable in the standard ll_CC.CHARSET
format. For example, if using Italian from Italy with the ISO-8859-15
character set:
Language it_IT.ISO-8859-15
Other display managers such as Entrance for the Enlightenment environment and WINGs Display Manager (WDM) for a Window Maker-style environment also allow the user interface language to be selected at login from a user-friendly menu. Check their documentation for how to set the language in their configuration files.
If you are currently using the X
Windows system and you would like to change the language, you will
need to logout of X Windows. You can do this from the menu of most
Desktop Managers for Linux/UNIX. If you don't see a menu or X Windows
has frozen, you can also kill X Windows by pressing
CTRL+ALT+BACKSPACE. Many Linux/UNIX systems will automatically run
the Display Manager to log you back into X Windows. On some systems
you will have to manually restart X Windows by typing startx
at the command line. If you want to login as a different user or
change the language you will need to run the Display Manager for your
system. If using a system with the GNOME desktop environment, type:
gdm
at the command prompt. On
systems with KDE, type: kdm
. On
systems with Enlightenment, type: entrance
.
Otherwise, try: xdm
, slim
or wdm
.
Even though you can change the locale to use a new language, it is not guaranteed that all programs will be displayed in that new language. If no translation exists for your language, the program will default to whatever language was used in the original programming which is almost always English. Depending on your distribution of Linux/UNIX, all the translation files may not have been installed when you installed the program. For instance, in Ubuntu GNU/Linux, the translation files for GNOME, KDE, OpenOffice and the Mozilla programs are found in separate packages from the executable files. Sometimes these packages are labeled according to the language, so you could search for packages whose names end in "-es" if you are looking for Spanish packages. Often, all the translation files for a program are placed in the same package and labeled as "-i18n" which stands for "internationalization".
Most package managers provide an easy
way to search for the available packages. For instance, in
Debian-based systems, you can use the apt-cache
command to search for all the packages containing the word "spanish
"
or ending in "-es
or "-i18n
":
apt-cache search "spanish|\-es$|\-i18n$"
Similarly, on Red Hat-based systems you
can look for packages containing the word "spanish
"
with the rpm command:
rpm -qa spanish
The translations for a particular
program are usually stored in Machine Object (.mo
)
files which are accessed through the gettext function. The
translation files for each language are stored in the
/usr/share/locale
directory (or in
the location specified by the environment variable $TEXTDOMAINDIR
).
The translations will be found in directories for each language. For
instance if searching for the Spanish translation file for AbiWord,
it will be found at /usr/share/locale/es/LC_MESSAGES/abiword.mo
.
If writing C/C++ code, the ISO C standard specifies that all C programs will start in the "C" locale which is a default locale which only guarantees that you can use the English language with the 7-bit ASCII character set. In order to use the locale environment variables set on your system, use the setlocale function from the locale.h header. Set the first argument to LC_ALL and the second argument to NULL (or an empty string). If you need to know what locale your system is using, this function will return the current locale:
#include
<locale.h>
...
char * str;
str = setlocale(LC_ALL,
NULL);
If you want to change the locale for your C program, put the name of the desired locale in the second argument:
setlocale(LC_ALL, "es_AR.ISO-8859-1");
If setlocale has a return value of NULL, then the locale name was invalid and the locale was not changed. To learn how to internationalize your code and switch the language of the messages in your programs, see this tutorial and the gettext documentation.
If writing PHP code, environment variables are stored in the $_ENV array. To find the current locale:
str = $_ENV["LANG"];
To set the locale for your currently running program, use the putenv function:
putenv("LANG=es_AR.ISO-8859-1");
If programming in C/C++ and you want to change the locale for an environment variable in your system and not just the locale for the currently executing program, you can use the getenv function in the stdlib.h header to discover the current locale and the system function to execute a system command as if using the normal command line:
#include
<stdlib.h>
...
char * s;
s =
getenv("LANG");system("export
LANG=\"es_AR.ISO-8859-1\"");
If you are programming in PHP you can use the getenv and exec function to do the same:
s
= getenv("LANG");
exec('export
LANG="es_AR.ISO-8859-1"');
Written by:
Amos B. Batto
email: amosbatto AT yahoo DOT com
Project: www.runasimipi.org
Version: 0.1, Date: 09-09-2008
This document is public domain and may be shared and modified freely without restrictions.