Difference between revisions of "Locale"

From ArchWiki
Jump to: navigation, search
(Setting system wide locale)
(link to wookiepedia article)
 
(266 intermediate revisions by 79 users not shown)
Line 1: Line 1:
[[Category:Internationalization (English)]]
+
[[Category:Localization]]
[[Category:HOWTOs (English)]]
+
[[ar:Locale]]
{{i18n_links_start}}
+
[[cs:Locale]]
{{i18n_entry|Deutsch|Locales konfigurieren (Deutsch)}}
+
[[de:Locale]]
{{i18n_entry|English|Configuring locales}}
+
[[es:Locale]]
{{i18n_entry|Español|Locales (Español)}}
+
[[fr:Locale]]
{{i18n_entry|Česky|Nastavení locales}}
+
[[it:Locale]]
{{i18n_entry|Українська|Локаль}}
+
[[ja:ロケール]]
{{i18n_entry|简体中文|Locales (简体中文)}}
+
[[ko:Locale]]
{{i18n_links_end}}
+
[[nl:Locale]]
==Introduction==
+
[[pt:Locale]]
Locales are used in Linux to define which language the user uses. As the locales define the charset which the user uses as well, setting up the correct locale is especially important if the language contains non-ASCII characters.
+
[[ru:Locale]]
 +
[[zh-hans:Locale]]
 +
{{Related articles start}}
 +
{{Related|Environment variables}}
 +
{{Related articles end}}
 +
[[w:Locale (computer software)|Locales]] are used by {{Pkg|glibc}} and other locale-aware programs or libraries for rendering text, correctly displaying regional monetary values, time and date formats, alphabetic idiosyncrasies, and other locale-specific standards.
  
[[Locale Naming]] ist defined as following:
+
== Generating locales ==
<lang>_<territory>.<codeset>[@<modifiers>]
 
  
In this howto we're setting up a system that uses the en_US.UTF-8 locale, but you can follow this article easily if you want to setup another locale.
+
Locale names are typically of the form {{ic|language[_territory][.codeset][@modifier]}}, where ''language'' is an [[w:List_of_ISO_639-1_codes|ISO 639 language code]], ''territory'' is an [[w:ISO_3166-1#Current_codes|ISO 3166 country code]], and ''codeset'' is a [[w:Character_encoding|character set]] or encoding identifier like [[w:ISO/IEC_8859-1|ISO-8859-1]] or [[w:UTF-8|UTF-8]]. See {{man|3|setlocale}}.
  
==Enabling necessary locales==
+
For a list of enabled locales, run:
First you have to enable the locales you want being supported by your system. To enable or disable them, the file '''/etc/locale.gen''' is used. It contains every locale you can enable, and you have just to uncomment lines you want to do so.
 
  
As we want to setup an english UTF-8 conform system, we want to enable en_US.UTF-8. But for compatibility to programs that don't support UTF-8 yet, it's recommended to support any other locale, prefixed with en_US as well. Having this in mind, we enable this set of locales:
+
$ locale -a
en_US.UTF8    UTF-8
+
 
en_US ISO-8859-1
+
Before a locale can be enabled on the system, it must be generated. This can be achieved by uncommenting applicable entries in {{ic|/etc/locale.gen}}, and running ''locale-gen''. Equivalently, commenting entries disables their respective locales. While making changes, consider any localisations required by other users on the system, as well as specific [[#Variables]].
 +
 
 +
For example, uncomment {{ic|en_US.UTF-8 UTF-8}} for American-English:
 +
 
 +
{{hc|/etc/locale.gen|
 +
...
 +
#en_SG ISO-8859-1
 +
en_US.UTF-8 UTF-8
 +
#en_US ISO-8859-1
 +
...
 +
}}
 +
 
 +
Save the file, and generate the locale:
 +
 
 +
# locale-gen
  
After you've enabled the necessary locales, you have to run locale-gen as root to update them:
+
{{Note|1=<nowiki></nowiki>
# sudo locale-gen
+
* {{ic|locale-gen}} also runs with every update of {{Pkg|glibc}}. [https://projects.archlinux.org/svntogit/packages.git/tree/trunk/glibc.install?h=packages/glibc#n5]
Generating locales...
+
* {{ic|UTF-8}} is recommended over other character sets. [http://utf8everywhere.org/]}}
  en_US.UTF-8... done
 
  en_US.ISO-8859-1... done
 
Generation complete.
 
  
'''Note:''' Though it's most likely just one language you use on your computer it can be helpful or even necessary to enable other locales as well. For example if you're running a multi-user system with users that don't speak en_US, they won't be happy until their individual locale is at least supported by your system.
+
== Setting the locale ==
  
==Setting system wide locale==
+
To display the currently set locale and its related environmental settings, type:
To define which locale should be used by the system, you can easily add your locale to your '''/etc/rc.conf''' file. As we've just added ISO-8859 support just for (backward-)compability, we add en_US.UTF-8 here:
 
LOCALE="en_US.UTF-8"
 
  
The system wide locale will be updated after rebooting your computer.
+
$ locale
  
==Collation==
+
The locale to be used, chosen among the previously generated ones, is set in {{ic|locale.conf}} files. Each of these files must contain a new-line separated list of [[environment variable]] assignments, having the same format as output by ''locale''.
Collation, or sorting, is a little different. Sorting is a goofy beast and different locales do things differently. To get around potential issues, we have set LC_COLLATE="C" in /etc/profile. This forces collation to "C mode". This is usually a good idea for most users. If you want to overwrite this with sorting rules from your locale, it is best to do one of the following:
 
  
* Modify the line in /etc/profile directly
+
To list available locales which have been previously generated, run:
* Comment out the LC_COLLATE line in /etc/profile (allowing locale aware apps to use LC_ALL or LOCALE)
 
* Manually set LC_COLLATE in addition to LC_ALL or LOCALE in your user profile
 
  
==Setting per user locale==
+
$ localedef --list-archive
As we discussed earlier, some users might want to define a different locale than the system-wide locale.
 
In this case, you can export LC_ALL in your '''~/.bashrc'''.
 
For example you can use the en_US.iso8859 locale, though there is no advantage of using it.
 
export LC_ALL=en_US.iso8859
 
  
Your locales will be updated as soon as you re-source your ~/.bashrc. This happens on login or alternatively you can type:
+
Alternatively, using {{man|1|localectl}}:
$ source ~/.bashrc
 
  
==Setting language==
+
$ localectl list-locales
The language in which the system interacts with the user is also determined by the locales, namely the locale LC_LANG.
 
To set the preferred language, you have to export the LANG variable in your ~/.bashrc. Or you can put it in rc.local for system wide
 
export LANG=en_EN.utf8
 
After sourcing your ~/.bashrc, programs ought to use the defined language, at least they do if they're using proper internationalization.
 
  
==Setting starting weekday==
+
=== Setting the system locale ===
In a lot of countries the first day of the week is Monday. To do change or add the following lines under the LC_TIME section in ''/usr/share/i18n/locales/<your_locale>''
 
  
week            7;19971130;5
+
To set the system locale, write the {{ic|LANG}} variable to {{ic|/etc/locale.conf}}, where {{ic|''en_US.UTF-8''}} belongs to the '''first column''' of an uncommented entry in {{ic|/etc/locale.gen}}:
first_weekday  2
 
first_workday  2
 
And then run
 
  
  # locale-gen
+
{{hc|1=/etc/locale.conf|2=
and restart X.
+
LANG=''en_US.UTF-8''
 +
}}
 +
 
 +
Alternatively, run:
 +
 
 +
  # localectl set-locale LANG=''en_US.UTF-8''
 +
 
 +
See [[#Variables]] and {{man|5|locale.conf}} for details.
 +
 
 +
=== Overriding system locale per user session ===
 +
 
 +
The system-wide locale can be overridden in each user session by creating or editing {{ic|~/.config/locale.conf}} (or, in general, {{ic|$XDG_CONFIG_HOME/locale.conf}} or {{ic|$HOME/.config/locale.conf}}).
 +
 
 +
The precedence of these {{ic|locale.conf}} files is defined in {{ic|/etc/profile.d/locale.sh}}.
 +
 
 +
{{Tip|
 +
* This can also allow keeping the logs in {{ic|/var/log}} in English while using the local language in the user environment.
 +
* You can create a {{ic|/etc/skel/.config/locale.conf}} file so that any new users added using ''useradd'' and the {{ic|-m}} option will have {{ic|~/.config/locale.conf}} automatically generated.}}
 +
 
 +
=== Make locale changes immediate ===
 +
 
 +
Once system and user {{ic|locale.conf}} files have been created or edited, their new values will take effect for new sessions at login. To have the current environment use the new settings unset {{ic|LANG}} and source {{ic|/etc/profile.d/locale.sh}}:
 +
 
 +
$ unset LANG
 +
$ source /etc/profile.d/locale.sh
 +
 
 +
{{Note|The {{ic|LANG}} variable has to be unset first, otherwise {{ic|locale.sh}} will not update the values from {{ic|locale.conf}}. Only new and changed variables will be updated; variables removed from {{ic|locale.conf}} will still be set in the session.}}
 +
 
 +
=== 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=C ./my_application.sh
 +
 
 +
Similarly, to set the locale for all processes run from the current shell (for example, during system installation):
 +
 
 +
$ export LANG=C
 +
 
 +
== Variables ==
 +
 
 +
{{ic|locale.conf}} files support the following environment variables:
 +
 
 +
* [[#LANG: default locale|LANG]]
 +
* [[#LANGUAGE: fallback locales|LANGUAGE]]
 +
* {{ic|LC_ADDRESS}}
 +
* [[#LC_COLLATE: collation|LC_COLLATE]]
 +
* {{ic|LC_CTYPE}}
 +
* {{ic|LC_IDENTIFICATION}}
 +
* {{ic|LC_MEASUREMENT}}
 +
* {{ic|LC_MESSAGES}}
 +
* {{ic|LC_MONETARY}}
 +
* {{ic|LC_NAME}}
 +
* {{ic|LC_NUMERIC}}
 +
* {{ic|LC_PAPER}}
 +
* {{ic|LC_TELEPHONE}}
 +
* [[#LC_TIME: date and time format|LC_TIME]]
 +
 
 +
Full meaning of the above {{ic|LC_*}} variables can be found on manpage {{man|7|locale}}, whereas details of their definition are described on {{man|5|locale}}.
 +
 
 +
=== LANG: default locale ===
 +
 
 +
The locale set for this variable will be used for all the {{ic|LC_*}} variables that are not explicitly set.
 +
 
 +
=== LANGUAGE: fallback locales ===
 +
 
 +
Programs which use {{Pkg|gettext}} for translations respect the {{Ic|LANGUAGE}} option in addition to the usual variables. This allows users to specify a [http://www.gnu.org/software/gettext/manual/gettext.html#The-LANGUAGE-variable 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:
 +
 
 +
{{hc|locale.conf|2=
 +
LANG=en_AU.UTF-8
 +
LANGUAGE=en_AU:en_GB:en
 +
}}
 +
 
 +
=== LC_TIME: date and time format ===
 +
 
 +
If {{ic|LC_TIME}} is set to {{ic|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:
 +
 
 +
{{hc|locale.conf|2=
 +
LC_TIME=en_DK.UTF-8
 +
}}
 +
 
 +
{{Note|Programs do not necessarily respect this variable to format the date. For example, {{man|1|date}} uses its own parameters to do so.}}
 +
 
 +
=== LC_COLLATE: collation ===
 +
 
 +
This variable governs the collation rules used for sorting and regular expressions.
 +
 
 +
Setting the value to {{ic|C}} can for example make the ''ls'' command sort dotfiles first, followed by uppercase and lowercase filenames:
 +
 
 +
{{hc|locale.conf|2=
 +
LC_COLLATE=C
 +
}}
 +
 
 +
See also [http://superuser.com/a/448294/175967].
 +
 
 +
To get around potential issues, Arch used to set {{ic|1=LC_COLLATE=C}} in {{ic|/etc/profile}}, but this method is now deprecated.
 +
 
 +
=== LC_ALL: troubleshooting ===
 +
 
 +
The locale set for this variable will always override {{ic|LANG}} and all the other {{ic|LC_*}} variables, whether they are set or not.
 +
 
 +
{{ic|LC_ALL}} is the only {{ic|LC_*}} variable, which '''cannot''' be set in {{ic|locale.conf}} files: it is meant to be used only for testing or troubleshooting purposes, for example in {{ic|/etc/profile}}.
 +
 
 +
== Troubleshooting ==
  
{{Note| If you experience some kind of problems with your system and would like to ask for help on the forum, mailing list or otherwise, please run the misbehaving program with <code>export LC_MESSAGES&#61;C</code> before posting. It will set the output messages (errors, warnings) to English thus enabling more people to understand what the problem might be, resulting in a more throughout response. This is of course not the point if you are posting to a non-English forum.}}
+
=== My terminal does not support UTF-8 ===
  
==Troubleshooting==
+
The following lists some (not all) terminals that support UTF-8:
===How can I obtain the available locale names?===
 
You can get the correct names of all available locales with this command:
 
$ locale -a
 
===How can I see which locale I am using?===
 
Which locale is currently in use is viewable simply by typing:
 
$ locale
 
===My terminal doesn't support UTF-8 characters===
 
Unfortunately some terminals don't support UTF-8. In this case, you have to use a different terminal.
 
  
'''List of terminals that support UTF-8:'''
 
* gnome-terminal
 
 
* gnustep-terminal
 
* gnustep-terminal
 
* konsole
 
* konsole
 
* [[mlterm]]
 
* [[mlterm]]
* urxvt (rxvt-unicode)
+
* [[rxvt-unicode]]
* xfce-terminal
+
* [[st]]
* xterm
+
* [[termite]]
'''Note:''' This list may be incomplete.
+
* [[List_of_applications/Utilities#VTE-based|VTE-based terminals]]
 +
* [[xterm]] - Run with the argument {{ic|-u8}} or configure resource {{ic|xterm*utf8: 2}}.
 +
 
 +
==== Gnome-terminal or rxvt-unicode ====
 +
 
 +
You need to launch these applications from a UTF-8 locale or they will drop UTF-8 support.  Enable the {{ic|en_US.UTF-8}} locale (or your local UTF-8 alternative) per the instructions above and set it as the default locale, then reboot.
 +
 
 +
=== My system is still using wrong language ===
 +
 
 +
It is possible that the environment variables are redefined in other files than {{ic|locale.conf}}, for example {{ic|~/.pam_environment}}. See [[Environment variables#Defining variables]] for details.
 +
 
 +
If you're using a desktop environment, such as [[GNOME]], its language settings may be overriding the settings in {{ic|locale.conf}}.
 +
 
 +
[[KDE]] Plasma also allows to change the UI's language through the system settings. If the desktop environment is still using the default language after the modification, [https://bbs.archlinux.org/viewtopic.php?pid=1435219#p1435219 deleting the file at] {{ic|~/.config/plasma-localerc}} (previously: {{ic|~/.config/plasma-locale-settings.sh}}) should resolve the issue.
  
===xterm doesn't support UTF-8 characters for me===
+
== See also ==
xterm only supports UTF-8 if you run it as uxterm or xterm -u8.
 
  
==Links==
+
* [[Gentoo:Localization/Guide]]
* [http://en.gentoo-wiki.com/wiki/HOWTO_localedef GentooWiki: Localedef] (404-ish as of 2009-05-24)
+
* [http://www.gentoo-wiki.info/Locales Gentoo Wiki Archives: Locales]{{Dead link|2018|06|07}}
 
* [http://demo.icu-project.org/icu-bin/locexp?_=en_US&x=col ICU's interactive collation testing]
 
* [http://demo.icu-project.org/icu-bin/locexp?_=en_US&x=col ICU's interactive collation testing]
 +
* [http://www.openi18n.org/ Free Standards Group Open Internationalisation Initiative]
 +
* [http://pubs.opengroup.org/onlinepubs/007908799/xbd/locale.html ''The Single UNIX Specification'' definition of Locale] by The Open Group
 +
* [https://help.ubuntu.com/community/EnvironmentVariables#Locale_setting_variables Locale environment variables]

Latest revision as of 07:24, 5 December 2018

Locales are used by glibc and other locale-aware programs or libraries for rendering text, correctly displaying regional monetary values, time and date formats, alphabetic idiosyncrasies, and other locale-specific standards.

Generating locales

Locale names are typically of the form language[_territory][.codeset][@modifier], where language is an ISO 639 language code, territory is an ISO 3166 country code, and codeset is a character set or encoding identifier like ISO-8859-1 or UTF-8. See setlocale(3).

For a list of enabled locales, run:

$ locale -a

Before a locale can be enabled on the system, it must be generated. This can be achieved by uncommenting applicable entries in /etc/locale.gen, and running locale-gen. Equivalently, commenting entries disables their respective locales. While making changes, consider any localisations required by other users on the system, as well as specific #Variables.

For example, uncomment en_US.UTF-8 UTF-8 for American-English:

/etc/locale.gen
...
#en_SG ISO-8859-1
en_US.UTF-8 UTF-8
#en_US ISO-8859-1
...

Save the file, and generate the locale:

# locale-gen
Note:
  • locale-gen also runs with every update of glibc. [1]
  • UTF-8 is recommended over other character sets. [2]

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 these files must contain a new-line separated list of environment variable assignments, having the same format as output by locale.

To list available locales which have been previously generated, run:

$ localedef --list-archive

Alternatively, using localectl(1):

$ localectl list-locales

Setting the system locale

To set the system locale, write the LANG variable to /etc/locale.conf, where en_US.UTF-8 belongs to the first column of an uncommented entry in /etc/locale.gen:

/etc/locale.conf
LANG=en_US.UTF-8

Alternatively, run:

# localectl set-locale LANG=en_US.UTF-8

See #Variables and locale.conf(5) for details.

Overriding system locale per user session

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

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

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.

Make locale changes immediate

Once system and user locale.conf files have been created or edited, their new values will take effect for new sessions at login. To have the current environment use the new settings unset LANG and source /etc/profile.d/locale.sh:

$ unset LANG
$ source /etc/profile.d/locale.sh
Note: The LANG variable has to be unset first, otherwise locale.sh will not update the values from locale.conf. Only new and changed variables will be updated; variables removed from locale.conf will still be set in the session.

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=C ./my_application.sh

Similarly, to set the locale for all processes run from the current shell (for example, during system installation):

$ export LANG=C

Variables

locale.conf files support the following environment variables:

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

Full meaning of the above LC_* variables can be found on manpage locale(7), whereas details of their definition are described on locale(5).

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.UTF-8
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
Note: Programs do not necessarily respect this variable to format the date. For example, date(1) uses its own parameters to do so.

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 [3].

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

LC_ALL: troubleshooting

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.

Troubleshooting

My terminal does not support UTF-8

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

Gnome-terminal or rxvt-unicode

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.

My system is still using wrong language

It is possible that the environment variables are redefined in other files than locale.conf, for example ~/.pam_environment. See Environment variables#Defining variables for details.

If you're using a desktop environment, such as GNOME, its language settings may be overriding the settings in locale.conf.

KDE Plasma also allows to change the UI's language through the system settings. If the desktop environment is still using the default language after the modification, deleting the file at ~/.config/plasma-localerc (previously: ~/.config/plasma-locale-settings.sh) should resolve the issue.

See also