Difference between revisions of "Locale"

From ArchWiki
Jump to: navigation, search
(My terminal doesn't support UTF-8: adds inter-wiki links)
(Undo revision 448067 by Alad (talk) - brackets allow to express syntactically optional parts)
 
(169 intermediate revisions by 31 users not shown)
Line 1: Line 1:
 
[[Category:Internationalization]]
 
[[Category:Internationalization]]
 +
[[ar:Locale]]
 
[[cs:Locale]]
 
[[cs:Locale]]
 
[[de:Locale]]
 
[[de:Locale]]
Line 5: Line 6:
 
[[fr:Locale]]
 
[[fr:Locale]]
 
[[it:Locale]]
 
[[it:Locale]]
[[ja:Locale]]
+
[[ja:ロケール]]
 
[[ko:Locale]]
 
[[ko:Locale]]
 
[[nl:Locale]]
 
[[nl:Locale]]
Line 11: Line 12:
 
[[uk:Locale]]
 
[[uk:Locale]]
 
[[zh-CN:Locale]]
 
[[zh-CN:Locale]]
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.
+
{{Related articles start}}
 +
{{Related|Environment variables}}
 +
{{Related articles end}}
 +
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 names are defined using the following format:
+
== Generating locales ==
<lang>_<territory>.<codeset>[@<modifiers>]
+
 
 +
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}}.
 +
 
 +
For a list of enabled locales, run:
  
==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
 
  $ locale -a
To enable a locale, uncomment the name of the locale in the file {{ic|/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:
+
 
 +
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
 
  # locale-gen
  
To display the locales now currently in use, use:
+
{{Note|1=<nowiki></nowiki>
 +
* {{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]
 +
* {{ic|UTF-8}} is recommended over other character sets. [http://utf8everywhere.org/]}}
 +
 
 +
== Setting the locale ==
 +
 
 +
To display the currently set locale and its related environmental settings, type:
 +
 
 
  $ locale
 
  $ locale
  
{{Tip| Though it's most likely that just one language is used use 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 en_US, their individual locale should at least be supported by your system.}}
+
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''.
  
===US English example===
+
To list available locales which have been previously generated, run:
  
First uncomment the following locales in {{ic|/etc/locale.gen}}:
+
  $ localedef --list-archive
  en_US.UTF-8 UTF-8
+
  
Then update the system as root:
+
Alternatively, using {{man|1|localectl}}:
# locale-gen
+
  
==Setting system-wide locale==
+
$ localectl list-locales
To define the system-wide locale used on the system, set {{ic|LANG}} in {{ic|/etc/locale.conf}}.
+
  
{{ic|locale.conf}} contains a new-line separated list of environment variable assignments: besides {{ic|LANG}}, it supports all the {{ic|LC_*}} variables, with the exception of {{ic|LC_ALL}}.
+
=== Setting the system locale ===
  
{{Note|{{ic|/etc/locale.conf}} does not exist by default and must be created manually.}}
+
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}}:
  
{{Tip| If the output of {{ic|locale}} is to your liking during installation, you can save a little time by doing: {{ic| # locale > /etc/locale.conf}} while chrooted.}}
+
{{hc|1=/etc/locale.conf|2=
 +
LANG=''en_US.UTF-8''
 +
}}
  
{{hc|/etc/locale.conf|2=
+
Alternatively, run:
LANG="en_US.UTF-8"}}
+
  
An advanced example configuration would be:
+
# localectl set-locale LANG=''en_US.UTF-8''
  
{{hc|/etc/locale.conf|2=
+
See [[#Variables]] and {{man|5|locale.conf}} for details.
# Enable UTF-8 with Australian settings.
+
LANG="en_AU.UTF-8"
+
  
# Keep the default sort order (e.g. files starting with a '.'
+
=== Overriding system locale per user session ===
# should appear at the start of a directory listing.)
+
LC_COLLATE="C"
+
  
# Set the short date to YYYY-MM-DD (test with "date +%c")
+
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}}).
LC_TIME="en_DK.UTF-8"}}
+
  
You can set the default locale in {{ic|locale.conf}} also using {{ic|localectl}}, for example:
+
The precedence of these {{ic|locale.conf}} files is defined in {{ic|/etc/profile.d/locale.sh}}.
  
# localectl set-locale LANG="de_DE.utf8"
+
{{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.}}
  
See {{ic|man 1 localectl}} and {{ic|man 5 locale.conf}} for details.
+
=== Make locale changes immediate ===
  
They will take effect after rebooting the system and will be set for individual sessions at login.
+
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}}:
 +
 
 +
$ 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 ===
  
==Setting fallback locales==
 
 
Programs which use 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:
 
Programs which use 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|~/.bashrc|2=
+
 
export LANGUAGE="en_AU:en_GB:en"
+
{{hc|locale.conf|2=
 +
LANG=en_AU
 +
LANGUAGE=en_AU:en_GB:en
 
}}
 
}}
or system-wide
+
 
{{hc|/etc/locale.conf|2=
+
=== LC_TIME: date and time format ===
LANG="en_AU"
+
 
LANGUAGE="en_AU:en_GB:en"
+
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
 
}}
 
}}
  
==Setting per user locale==
+
=== LC_COLLATE: collation ===
As we mentioned earlier, some users might want to define a different locale than the system-wide locale. To do this, export the variable {{Ic|LANG}} with the specified locale in the {{ic|~/.bashrc}} file. For example, to use the {{Ic|en_AU.UTF-8}} locale:
+
export LANG=en_AU.UTF-8
+
  
The locales will be updated next time {{ic|~/.bashrc}} is sourced. To update, either re-login or source it manually:
+
This variable governs the collation rules used for sorting and regular expressions.
$ source ~/.bashrc
+
  
==Setting collation==
+
Setting the value to {{ic|C}} can for example make the ''ls'' command sort dotfiles first, followed by uppercase and lowercase filenames:
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 {{ic|1=LC_COLLATE="C"}} in {{ic|/etc/profile}}. However, this method is now deprecated. To enable this behavior, simply add the following to {{ic|/etc/locale.conf}}:
+
LC_COLLATE="C"
+
  
Now the ls command will sort dotfiles first, followed by uppercase and lowercase filenames. Note that without a {{ic|LC_COLLATE}} setting, locale aware apps sort by {{ic|LC_ALL}} or {{ic|LANG}}, but {{ic|LC_COLLATE}} settings will be overridden if {{ic|LC_ALL}} is set. If this is a problem, ensure that LC_ALL is not set by adding the following to {{ic|/etc/profile}} instead:
+
{{hc|locale.conf|2=
export LC_ALL=
+
LC_COLLATE=C
Note that LC_ALL is the only LC variable which '''cannot''' be set in {{ic|/etc/locale.conf}}.
+
}}
  
==Setting the first day of the week==
+
See also [http://superuser.com/a/448294/175967].
In many countries the first day of the week is Monday. To adjust this, change or add the following lines in the {{ic|LC_TIME}} section in {{ic|/usr/share/i18n/locales/<your_locale>}}:
+
  
week            7;19971130;5
+
To get around potential issues, Arch used to set {{ic|1=LC_COLLATE=C}} in {{ic|/etc/profile}}, but this method is now deprecated.
first_weekday  2
+
first_workday  2
+
  
And then update the system:
+
=== LC_ALL: troubleshooting ===
  
# locale-gen
+
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 ==
  
{{Tip| 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 include the output from the misbehaving program with {{ic|export LC_MESSAGES&#61;C}} before posting. It will set the output messages (errors and warnings) to English, thus enabling more people to understand what the problem might be. This is not relevant if you are posting on a non-English forum.}}
+
=== My terminal does not support UTF-8 ===
  
==Troubleshooting==
+
The following lists some (not all) terminals that support UTF-8:
===My terminal doesn't support UTF-8===
+
Unfortunately some terminals do not support UTF-8. In this case, you have to use a different terminal. Here are some terminals that have support for UTF-8:
+
  
* vte-based terminals
 
 
* gnustep-terminal
 
* gnustep-terminal
 
* konsole
 
* konsole
 
* [[mlterm]]
 
* [[mlterm]]
 
* [[rxvt-unicode]]
 
* [[rxvt-unicode]]
* [[xterm]]
+
* [[st]]
 +
* [[termite]]
 +
* [[List_of_applications/Utilities#VTE-based|VTE-based terminals]]
 +
* [[xterm]] - Run with the argument {{ic|-u8}} or configure resource {{ic|xterm*utf8: 2}}.
  
====Xterm doesn't support UTF-8====
+
==== Gnome-terminal or rxvt-unicode ====
xterm only supports UTF-8 if you run it as {{Ic|uxterm}} or {{Ic|xterm -u8}}.
+
  
====Gnome-terminal or rxvt-unicode doesn't support UTF-8====
 
 
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.
 
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.
  
==See also==
+
=== 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.
 +
 
 +
== See also ==
 +
 
 
* [http://www.gentoo.org/doc/en/guide-localization.xml Gentoo Linux Localization Guide]
 
* [http://www.gentoo.org/doc/en/guide-localization.xml Gentoo Linux Localization Guide]
 
* [http://www.gentoo-wiki.info/Locales Gentoo Wiki Archives: Locales]
 
* [http://www.gentoo-wiki.info/Locales Gentoo Wiki Archives: Locales]

Latest revision as of 17:27, 26 August 2016

Related articles

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:

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

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.

See also