locale.xml 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. <sect1 id="ch-scripts-locale" revision="systemd">
  8. <?dbhtml filename="locale.html"?>
  9. <title>配置系统 Locale</title>
  10. <indexterm zone="ch-scripts-locale">
  11. <primary sortas="e-etc-locale-conf">/etc/locale.conf</primary>
  12. </indexterm>
  13. <para>下面将创建的 <filename>/etc/locale.conf</filename>
  14. 设定本地语言支持必要的环境变量,将它们正确设定后,
  15. 可以得到以下好处:</para>
  16. <itemizedlist>
  17. <listitem>
  18. <para>程序输出被翻译成本地语言</para>
  19. </listitem>
  20. <listitem>
  21. <para>字符被正确分类为字母、数字和其他类别,
  22. 这对于使 <command>bash</command> 正确接受命令行中的非 ASCII
  23. 本地字符来说是必要的</para>
  24. </listitem>
  25. <listitem>
  26. <para>根据所在地区惯例排序字母</para>
  27. </listitem>
  28. <listitem>
  29. <para>适用于所在地区的默认纸张尺寸</para>
  30. </listitem>
  31. <listitem>
  32. <para>正确格式化货币、时间和日期值</para>
  33. </listitem>
  34. </itemizedlist>
  35. <note><title>译注</title>
  36. <para>译者强烈反对在 <filename>/etc/locale.conf</filename>
  37. 中指定中文 locale。由于字符终端的局限性,它根本无法显示中文字符。
  38. 使用中文 locale 将导致许多程序输出中文消息,
  39. 结果在字符终端中都显示为问号。建议在
  40. <filename>/etc/locale.conf</filename> 中指定英文 locale ,
  41. 在 BLFS 中安装了图形环境后再将
  42. <literal>LANG=zh_CN.UTF-8</literal> 写入
  43. <filename>.xprofile</filename> 等图形界面配置文件,
  44. 这样即可在字符终端中使用英文 locale ,而在图形界面中使用中文。
  45. </para>
  46. </note>
  47. <para>将下面的 <replaceable>&lt;ll&gt;</replaceable>
  48. 替换为所需语言的双字符代号(例如 <quote>en</quote>),
  49. <replaceable>&lt;CC&gt;</replaceable> 替换为国家或地区的双字符代号
  50. (例如 <quote>GB</quote>),
  51. <replaceable>&lt;charmap&gt;</replaceable> 替换为您选定的 locale
  52. 的标准字符映射。另外,还可以加入 <quote>@euro</quote>
  53. 之类的可选修饰符。</para>
  54. <para>Glibc 支持的所有 locale 可以用以下命令列出:</para>
  55. <screen role="nodump"><userinput>locale -a</userinput></screen>
  56. <para>字符映射可能有多个别名,例如 <quote>ISO-8859-1</quote>
  57. 也可以称为 <quote>iso8859-1</quote> 和 <quote>iso88591</quote>。
  58. 某些程序不能正确处理一些别名(例如,只识别 <quote>UTF-8</quote>,
  59. 不能识别 <quote>utf8</quote>),因此在多数情况下,为了保险起见,
  60. 最好使用 locale 的规范名称。为了确定规范名称,执行以下命令,
  61. 将 <replaceable>&lt;locale name&gt;</replaceable> 替换成
  62. <command>locale -a</command> 对于您希望的 locale 的输出
  63. (以 <quote>en_GB.iso88591</quote> 为例)。</para>
  64. <screen role="nodump"><userinput>LC_ALL=<replaceable>&lt;locale name&gt;</replaceable> locale charmap</userinput></screen>
  65. <para>对于 <quote>en_GB.iso88591</quote> locale,以上命令输出:</para>
  66. <screen><computeroutput>ISO-8859-1</computeroutput></screen>
  67. <para>这样就最终确定 locale 应设置为 <quote>en_GB.ISO-8859-1</quote>。
  68. 在将以上启发方法获得的 locale 添加到 Bash 启动文件之前,
  69. 一定要进行下列测试:</para>
  70. <screen role="nodump"><userinput>LC_ALL=&lt;locale name&gt; locale language
  71. LC_ALL=&lt;locale name&gt; locale charmap
  72. LC_ALL=&lt;locale name&gt; locale int_curr_symbol
  73. LC_ALL=&lt;locale name&gt; locale int_prefix</userinput></screen>
  74. <!--para>The above commands should print the language name, the character
  75. encoding used by the locale, the local currency, and the prefix to dial
  76. before the telephone number in order to get into the country. If any of the
  77. commands above fail with a message similar to the one shown below, this means
  78. that your locale was either not installed in Chapter&nbsp;6 or is not supported by
  79. the default installation of Glibc.</para-->
  80. <para>以上命令应该输出语言名称、选定 locale 使用的字符编码、
  81. 本地货币符号以及所在国家或地区的国际电话区号。
  82. 如果以上某个命令失败并输出类似下面这样的消息,
  83. 意味着您的 locale 在第 6 章中没有安装,或者不被 Glibc 的默认安装支持。
  84. </para>
  85. <screen><computeroutput>locale: Cannot set LC_* to default locale: No such file or directory</computeroutput></screen>
  86. <!--para>If this happens, you should either install the desired locale using the
  87. <command>localedef</command> command, or consider choosing a different locale.
  88. Further instructions assume that there are no such error messages from
  89. Glibc.</para-->
  90. <para>如果出现了这种消息,您应该用 <command>localedef</command>
  91. 命令安装所需的 locale ,或重新选择一个不同的 locale 。
  92. 后文假设 Glibc 没有输出类似错误消息。</para>
  93. <!-- FIXME: the xlib example will became obsolete real soon -->
  94. <!--para>Some packages beyond LFS may also lack support for your chosen locale. One
  95. example is the X library (part of the X Window System), which outputs the
  96. following error message if the locale does not exactly match one of the character
  97. map names in its internal files:</para-->
  98. <para>某些 LFS 以外的软件包可能缺乏对您选择的 locale 的支持,
  99. 例如 X 库(X 窗口系统的一部分),它在您的 locale
  100. 与它内部文件中的字符映射表名不完全匹配时,会输出以下错误消息:</para>
  101. <screen><computeroutput>Warning: locale not supported by Xlib, locale set to C</computeroutput></screen>
  102. <!--para>In several cases Xlib expects that the character map will be listed in
  103. uppercase notation with canonical dashes. For instance, "ISO-8859-1" rather
  104. than "iso88591". It is also possible to find an appropriate specification by
  105. removing the charmap part of the locale specification. This can be checked
  106. by running the <command>locale charmap</command> command in both locales.
  107. For example, one would have to change "de_DE.ISO-8859-15@euro" to
  108. "de_DE@euro" in order to get this locale recognized by Xlib.</para-->
  109. <para>某些情况下 Xlib 期望字符映射以带有规范连字符的大写形式给出,
  110. 例如应该使用 <quote>ISO-8859-1</quote> 而不是
  111. <quote>iso88591</quote>。
  112. 有时也可以通过去除 locale 规范中的字符映射部分找到合适的规范,
  113. 可以通过运行 <command>locale charmap</command> 确认。
  114. 例如,您需要将 <quote>de_DE.ISO-8859-15@euro</quote>
  115. 替换成 <quote>de_DE@euro</quote>,以获得 Xlib 能够识别的 locale。
  116. </para>
  117. <!--para>Other packages can also function incorrectly (but may not necessarily
  118. display any error messages) if the locale name does not meet their expectations.
  119. In those cases, investigating how other Linux distributions support your locale
  120. might provide some useful information.</para-->
  121. <para>其他软件包在 locale 名不符合它们的期望时可能工作不正常
  122. (但未必输出错误消息)。在这种情况下,探索一下其他 Linux
  123. 发行版是如何支持您的 locale 的,可以得到一些有用的信息。</para>
  124. <para>在确定了正确的 locale 设置后,创建
  125. <filename>/etc/locale.conf</filename> 文件:</para>
  126. <screen><userinput>cat &gt; /etc/locale.conf &lt;&lt; "EOF"
  127. <literal>LANG=<replaceable>&lt;ll&gt;_&lt;CC&gt;.&lt;charmap&gt;&lt;@modifiers&gt;</replaceable></literal>
  128. EOF</userinput></screen>
  129. <para>您也可以使用 systemd 的 <command>localectl</command> 工具修改
  130. <filename>/etc/locale.conf</filename> 。如果希望使用
  131. <command>localectl</command> 创建以上例子中的
  132. <filename>locale.conf</filename>,运行:</para>
  133. <screen role="nodump"><userinput>localectl set-locale LANG="<replaceable>&lt;ll&gt;_&lt;CC&gt;.&lt;charmap&gt;&lt;@modifiers&gt;</replaceable>"</userinput></screen>
  134. <!--para>You can also specify other language specific environment variables such
  135. as <envar>LANG</envar>, <envar>LC_CTYPE</envar>, <envar>LC_NUMERIC</envar> or
  136. any other environment variable from <command>locale</command> output. Just
  137. separate them with a space. An example where <envar>LANG</envar> is set as
  138. en_US.UTF-8 but <envar>LC_CTYPE</envar> is set as just en_US is:</para-->
  139. <para>您也可以指定其他语言相关的环境变量,例如 <envar>LANG</envar>、
  140. <envar>LC_CTYPE</envar>、<envar>LC_NUMERIC</envar> 或
  141. <command>locale</command> 输出的其他环境变量,用空格将它们分割即可。
  142. 例如,将 <envar>LANG</envar> 设置为 en_US.UTF-8,但
  143. <envar>LC_CTYPE</envar> 设置为 en_US:</para>
  144. <screen role="nodump"><userinput>localectl set-locale LANG="en_US.UTF-8" LC_CTYPE="en_US"</userinput></screen>
  145. <note><title>译注</title>
  146. <para>这种设置的好处是使得一些程序(如 <command>gcc</command>)
  147. 不使用 UTF-8 引号。默认终端字体不包含 UTF-8 字形,因此 UTF-8
  148. 引号会显示为一个白底黑字问号。如果希望使用 UTF-8 引号,
  149. 参阅
  150. <ulink url="&blfs-book;postlfs/console-fonts.html">BLFS</ulink>
  151. 中的说明安装支持 UTF-8 符号的终端字体。
  152. </para>
  153. </note>
  154. <note><para>请注意 <command>localectl</command> 只能在使用 systemd
  155. 引导的系统中使用。</para></note>
  156. <!--para>The <quote>C</quote> (default) and <quote>en_US</quote> (the recommended
  157. one for United States English users) locales are different. <quote>C</quote>
  158. uses the US-ASCII 7-bit character set, and treats bytes with the high bit set
  159. as invalid characters. That's why, e.g., the <command>ls</command> command
  160. substitutes them with question marks in that locale. Also, an attempt to send
  161. mail with such characters from Mutt or Pine results in non-RFC-conforming
  162. messages being sent (the charset in the outgoing mail is indicated as <quote>unknown
  163. 8-bit</quote>). So you can use the <quote>C</quote> locale only if you are sure that
  164. you will never need 8-bit characters.</para-->
  165. <para><quote>C</quote> (默认 locale)和 <quote>en_US</quote>
  166. (推荐美式英语用户使用的 locale)是不同的。
  167. <quote>C</quote> locale 使用 US-ASCII 7 位字符集,并且将最高位为 1
  168. 的字节视为无效字符。因此,<command>ls</command>
  169. 等命令会将它们替换为问号。另外,如果试图用 Mutt 或 Pine
  170. 发送包含这些字符的邮件,会发出不符合 RFC 标准的消息
  171. (发出邮件的字符集会被标为 <quote>未知 8 位</quote>)。
  172. 因此,您只能在确信自己永远不会使用 8 位字符时才能使用
  173. <quote>C</quote> locale。</para>
  174. <para>许多程序不能很好地支持基于 UTF-8 的 locale,
  175. 我们正在努力记录并(如果可能的话)修复它们,参阅
  176. <ulink url="&blfs-book;introduction/locale-issues.html"/>。</para>
  177. </sect1>