source: chapter09/locale.xml@ 6ebb3b9

12.1 12.1-rc1 12.2 12.2-rc1 multilib trunk xry111/arm64 xry111/clfs-ng xry111/loongarch xry111/loongarch-12.1 xry111/loongarch-12.2 xry111/mips64el xry111/multilib xry111/update-glibc
Last change on this file since 6ebb3b9 was 6ebb3b9, checked in by Xi Ruoyao <xry111@…>, 8 months ago

Unify locale settings in sysv and systemd

Do not duplicate large paragraphs of texts.

Always use C locale if running in a Linux console. Create /etc/profile
for systemd too, but reading the locale setting from /etc/locale.conf.

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