source: chapter09/locale.xml

trunk
Last change on this file was 4ecfc55, checked in by Xi Ruoyao <xry111@…>, 3 months ago

Use C.UTF-8 instead of C in console, and set the consolefont for it

  • Property mode set to 100644
File size: 9.1 KB
Line 
1<?xml version="1.0" encoding="UTF-8"?>
2<!DOCTYPE sect1 PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
3 "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd" [
4 <!ENTITY % general-entities SYSTEM "../general.ent">
5 %general-entities;
6]>
7
8<sect1 id="ch-config-locale">
9 <?dbhtml filename="locale.html"?>
10
11 <title>Configuring the System Locale</title>
12
13 <indexterm zone="ch-config-locale">
14 <primary sortas="e-/etc/profile">/etc/profile</primary>
15 </indexterm>
16
17 <indexterm zone="ch-config-locale" revision='systemd'>
18 <primary sortas="e-/etc/profile">/etc/locale.conf</primary>
19 </indexterm>
20
21 <para>Some environment variables are necessary for native language
22 support. Setting them properly results in:</para>
23
24 <itemizedlist>
25 <listitem>
26 <para>The output of programs being translated into your native language</para>
27 </listitem>
28 <listitem>
29 <para>The correct classification of characters into letters, digits and other
30 classes. This is necessary for <command>bash</command> to properly accept
31 non-ASCII characters in command lines in non-English locales</para>
32 </listitem>
33 <listitem>
34 <para>The correct alphabetical sorting order for the country</para>
35 </listitem>
36 <listitem>
37 <para>The appropriate default paper size</para>
38 </listitem>
39 <listitem>
40 <para>The correct formatting of monetary, time, and date values</para>
41 </listitem>
42 </itemizedlist>
43
44 <para>Replace <replaceable>&lt;ll&gt;</replaceable> below with the two-letter code
45 for your desired language (e.g., <literal>en</literal>) and
46 <replaceable>&lt;CC&gt;</replaceable> with the two-letter code for the appropriate
47 country (e.g., <literal>GB</literal>). <replaceable>&lt;charmap&gt;</replaceable> should
48 be replaced with the canonical charmap for your chosen locale. Optional
49 modifiers such as <literal>@euro</literal> may also be present.</para>
50
51 <para>The list of all locales supported by Glibc can be obtained by running
52 the following command:</para>
53
54<screen role="nodump"><userinput>locale -a</userinput></screen>
55
56 <para>Charmaps can have a number of aliases, e.g.,
57 <literal>ISO-8859-1</literal> is also referred to as
58 <literal>iso8859-1</literal> and <literal>iso88591</literal>.
59 Some applications cannot handle the various synonyms correctly (e.g., require
60 that <literal>UTF-8</literal> is written as <literal>UTF-8</literal>, not
61 <literal>utf8</literal>), so it is the safest in most
62 cases to choose the canonical name for a particular locale. To determine
63 the canonical name, run the following command, where <replaceable>&lt;locale
64 name&gt;</replaceable> is the output given by <command>locale -a</command> for
65 your preferred locale (<literal>en_GB.iso88591</literal> in our
66 example).</para>
67
68<screen role="nodump"><userinput>LC_ALL=<replaceable>&lt;locale name&gt;</replaceable> locale charmap</userinput></screen>
69
70 <para>For the <literal>en_GB.iso88591</literal> locale, the above command
71 will print:</para>
72
73<screen><computeroutput>ISO-8859-1</computeroutput></screen>
74
75 <para>This results in a final locale setting of <literal>en_GB.ISO-8859-1</literal>.
76 It is important that the locale found using the heuristic above is tested prior
77 to it being added to the Bash startup files:</para>
78
79<screen role="nodump"><userinput>LC_ALL=&lt;locale name&gt; locale language
80LC_ALL=&lt;locale name&gt; locale charmap
81LC_ALL=&lt;locale name&gt; locale int_curr_symbol
82LC_ALL=&lt;locale name&gt; locale int_prefix</userinput></screen>
83
84 <para>The above commands should print the language name, the character
85 encoding used by the locale, the local currency, and the prefix to dial
86 before the telephone number in order to get into the country. If any of the
87 commands above fail with a message similar to the one shown below, this means
88 that your locale was either not installed in Chapter&nbsp;8 or is not
89 supported by the default installation of Glibc.</para>
90
91<screen><computeroutput>locale: Cannot set LC_* to default locale: No such file or directory</computeroutput></screen>
92
93 <para>If this happens, you should either install the desired locale using the
94 <command>localedef</command> command, or consider choosing a different locale.
95 Further instructions assume that there are no such error messages from
96 Glibc.</para>
97
98 <para>Other packages can also function incorrectly (but may not necessarily
99 display any error messages) if the locale name does not meet their expectations.
100 In those cases, investigating how other Linux distributions support your locale
101 might provide some useful information.</para>
102
103 <para revision='systemd'>Once the proper locale settings have been
104 determined, create the <filename>/etc/locale.conf</filename> file:</para>
105
106<screen revision='systemd'><userinput>cat &gt; /etc/locale.conf &lt;&lt; "EOF"
107<literal>LANG=<replaceable>&lt;ll&gt;_&lt;CC&gt;.&lt;charmap&gt;&lt;@modifiers&gt;</replaceable></literal>
108EOF</userinput></screen>
109
110 <para>The shell program <command>/bin/bash</command> (here after referred
111 as <quote>the shell</quote>) uses a collection of startup files to help
112 create the environment to run in. Each file has a specific use and may
113 affect login and interactive environments differently. The files in the
114 <filename class="directory">/etc</filename> directory provide global
115 settings. If equivalent files exist in the home directory, they
116 may override the global settings.</para>
117
118 <para>An interactive login shell is started after a successful login,
119 using <command>/bin/login</command>, by reading the
120 <filename>/etc/passwd</filename> file. An interactive non-login shell is
121 started at the command-line (e.g.
122 <prompt>[prompt]$</prompt><command>/bin/bash</command>). A
123 non-interactive shell is usually present when a shell script is running.
124 It is non-interactive because it is processing a script and not waiting
125 for user input between commands.</para>
126
127 <para><phrase revision='systemd'>The login shells are often unaffected by
128 the settings in <filename>/etc/locale.conf</filename>. </phrase>Create the
129 <filename>/etc/profile</filename>
130 <phrase revision='sysv'>once the proper locale settings have been
131 determined to set the desired locale</phrase><phrase
132 revision='systemd'>to read the locale settings from
133 <filename>/etc/locale.conf</filename> and export them</phrase>,
134 but set the <literal>C.UTF-8</literal> locale instead if running in the Linux
135 console (to prevent programs from outputting characters that the Linux
136 console is unable to render):</para>
137
138<screen revision="systemd"><userinput>cat &gt; /etc/profile &lt;&lt; "EOF"
139<literal># Begin /etc/profile
140
141for i in $(locale); do
142 unset ${i%=*}
143done
144
145if [[ "$TERM" = linux ]]; then
146 export LANG=C.UTF-8
147else
148 source /etc/locale.conf
149
150 for i in $(locale); do
151 key=${i%=*}
152 if [[ -v $key ]]; then
153 export $key
154 fi
155 done
156fi
157
158# End /etc/profile</literal>
159EOF</userinput></screen>
160
161<screen revision="sysv"><userinput>cat &gt; /etc/profile &lt;&lt; "EOF"
162<literal># Begin /etc/profile
163
164for i in $(locale); do
165 unset ${i%=*}
166done
167
168if [[ "$TERM" = linux ]]; then
169 export LANG=C.UTF-8
170else
171 export LANG=<replaceable>&lt;ll&gt;_&lt;CC&gt;.&lt;charmap&gt;&lt;@modifiers&gt;</replaceable>
172fi
173
174# End /etc/profile</literal>
175EOF</userinput></screen>
176
177 <para revision='systemd'>Note that you can modify <filename>/etc/locale.conf</filename> with the
178 systemd <command>localectl</command> utility. To use
179 <command>localectl</command> for the example above, run:</para>
180
181<screen revision='systemd' role="nodump"><userinput>localectl set-locale LANG="<replaceable>&lt;ll&gt;_&lt;CC&gt;.&lt;charmap&gt;&lt;@modifiers&gt;</replaceable>"</userinput></screen>
182
183 <para revision='systemd'>You can also specify other language specific
184 environment variables such as <envar>LANG</envar>,
185 <envar>LC_CTYPE</envar>, <envar>LC_NUMERIC</envar> or any other
186 environment variable from <command>locale</command> output. Just separate
187 them with a space. An example where <envar>LANG</envar> is set as
188 en_US.UTF-8 but <envar>LC_CTYPE</envar> is set as just en_US is:</para>
189
190<screen revision='systemd' role="nodump"><userinput>localectl set-locale LANG="en_US.UTF-8" LC_CTYPE="en_US"</userinput></screen>
191
192 <note revision='systemd'><para>Please note that the
193 <command>localectl</command> command doesn't work in the chroot
194 environment. It can only be used after the LFS system is booted with
195 systemd.</para></note>
196
197 <para>The <literal>C</literal> (default) and <literal>en_US</literal>
198 (the recommended one for United States English users) locales are
199 different. <literal>C</literal>
200 uses the US-ASCII 7-bit character set, and treats bytes with the high bit set
201 as invalid characters. That's why, e.g., the <command>ls</command> command
202 substitutes them with question marks in that locale. Also, an attempt to send
203 mail with such characters from Mutt or Pine results in non-RFC-conforming
204 messages being sent (the charset in the outgoing mail is indicated as
205 <computeroutput>unknown 8-bit</computeroutput>). It's suggested that you
206 use the <literal>C</literal> locale only
207 if you are certain that you will never need 8-bit characters.</para>
208
209</sect1>
Note: See TracBrowser for help on using the repository browser.