本地化/指南

From Gentoo Wiki
Jump to: navigation, search
This page is a translated version of the page Localization/Guide and the translation is 75% complete.

Other languages:
Deutsch • ‎English • ‎español • ‎français • ‎italiano • ‎português do Brasil • ‎čeština • ‎русский • ‎中文(中国大陆)‎ • ‎日本語 • ‎한국어

时区

为了在现在的位置保持正确的系统时间,必须设置时区。在 系统时间 这篇文章中可以找到有关基于 OpenRC 系统基于 systemd 的系统 的说明。

Locale 系统

什么是 locale?

区域设置 (locale) 是大多数程序用来确定国家和语言特定设置的一组信息。locale 及其数据是系统库的一部分,大多数系统上可以在 /usr/share/i18n/locales/ 找到 locale。一个 locale 名称通常被命名为 ab_CDab 是两个(或三个)单词的语言代码(在 ISO-639 中指定), CD 是两个字母的国家代码(在 ISO-3166 中指定)。像 @euro@latin 这样的变量通常被附加到 locale 名称之后,例如 de_DE@euro or nan_TW@latin。请浏览 维基百科 了解更多关于 locale 信息和相关文章。

locale 的环境变量

下表给出了 locale 设置中控制不同方面的变量。他们都采用上面给出的 ab_CD 格式 locale 的一个名称。

变量名 解释
LANG Defines all locale settings at once, while allowing further individual customization via the LC_* settings below.
LC_COLLATE Define alphabetical ordering of strings. This affects e.g. output of sorted directory listings.
LC_CTYPE Define the character-handling properties for the system. This determines which characters are seen as alphabetic, numeric, and so on. This also determines the character set used, if applicable.
LC_MESSAGES Programs' localizations stored in /usr/share/locale/ for applications that use a message-based localization scheme (the majority of GNU programs).
LC_MONETARY Defines currency units and formatting of currency-type numeric values.
LC_NUMERIC Defines formatting of numeric values which aren't monetary. Affects things such as thousand separator and decimal separator.
LC_TIME Defines formatting of dates and times.
LC_PAPER Defines default paper size.
LC_ALL Overrides all other settings.
Note
Some programs are written in such a way that they expect traditional English ordering of the alphabet, while some locales, most notably the Estonian one, use a different ordering. Therefore it's recommended to explicitly set LC_COLLATE to C when dealing with system-wide settings.
Warning
强烈建议不要使用LC_ALL,因为它会自动覆盖所有其他的LC_*变量(LANG不受影响)。这意味着在 LC_ALL 设置为空字符串之前,其他方式所做的更改将被隐藏。所以最好不要在启动文件中设置它。

通常,用户只在全局设置 LANG 变量。

生成特定 locales

大多数用户可能只会在其系统上使用一个或两个locale 。在 /etc/locale.gen 文件中说明了如何指定其他 locale。

CODE 在 /etc/locale.gen 添加 locale
en_US.UTF-8 UTF-8
en_US ISO-8859-1
de_DE ISO-8859-1
de_DE@euro ISO-8859-15
Note
若要在基于非 UTF-8 的 locale 中使用欧洲货币符号 (€) ,要使用从 /usr/share/i18n/SUPPORTED/ 内的 @euro 值作为 locale 。

下一步是运行 locale-gen。它将生成所有在 /etc/locale.gen 文件中指定的 locale 并且把他们写入到 locale-存档(/usr/lib/locale/locale-archive)。

root #locale-gen
 * Generating 4 locales (this might take a while) with 1 jobs
 *  (1/4) Generating en_GB.ISO-8859-1 ...                       [ ok ]
 *  (2/4) Generating en_GB.UTF-8 ...                            [ ok ]
 *  (3/4) Generating zh_CN GB2312 ...                       [ ok ]
 *  (4/4) Generating zh_CN.UTF-8 UTF-8 ...                 [ ok ]
 * Generation complete

locale -a验证所选择的 locale 是否可用。

user $locale -a
C
POSIX
de_DE
de_DE.iso88591
de_DE.iso885915@euro
de_DE@euro
deutsch
en_GB
en_GB.iso88591
en_GB.utf8
german

可以用 localedef 列出 /usr/lib/locale/locale-archive 文件。

user $localedef --list-archive

可以用 strings 命令显示原始数据。

user $strings /usr/lib/locale/locale-archive | less

设定 locale

OpenRC

使用 OpenRC 时,locale 设置存储在环境变量中。通常在 /etc/env.d/02locale (系统级的设置)和 ~/.bashrc (用于用户特定的设置)。更多细节可以在 UTF-8 文章中找到。系统级的设置(/etc/env.d/02locale)可以通过 eselect locale 进行管理。例如,要将 LANG 变量的值设置为 C

root #eselect locale list
Available targets for the LANG variable:
  [1]   C 
  [2]   POSIX
  [3]   en_US
  [4]   en_US.iso885915
  [5]   en_US.utf8
  [ ]   (free form)
root #eselect locale set 1

当然,也可以手动编辑文件使 locale 变量多样化。

FILE /etc/env.d/02locale在 /etc/env.d/02locale 设置系统默认 locale
LANG="de_DE.UTF-8"
LC_COLLATE="C"

In some cases users may notice glitchy non-English representation in some applications like Krusader (https://bugs.kde.org/show_bug.cgi?id=371582). Removing or commenting the LC_ALL="" line from /etc/env.d/02locale should fix the problem.

It's also possible, and pretty common especially in a more traditional UNIX environment, to leave the global settings unchanged, i.e. in the C locale. Users can still specify their preferred locale in their own shell configuration file:

FILE ~/.bashrc设置用户 locale
export LANG="de_DE.UTF-8"
export LC_COLLATE="C"
FILE ~/.profile给 X 应用设置用户 locale
export LANG="de_DE.UTF-8"
export LC_COLLATE="C"

配置系统的另一种方法是将其保留在默认的 C locale,但同时启用UTF-8 字符表示(character representation)。此选项使用 /etc/env.d/02locale 中的以下设置实现:

CODE 当指定 UTF-8 时使用传统的 C locale
LC_CTYPE=de_DE.UTF-8

使用上面的代码片段,用户将能够正确地查看本地化文件名,而不必完全使用所选的语言。

一旦设置了正确的 locale,请确保更新环境变量以使系统知道更改了 locale。

对于系统级默认 locale:

root #env-update && source /etc/profile

对于用户用户特定的 locale:

user $source ~/.bashrc

之后,按 Ctrl+Alt+Backspace 结束 X 服务,注销,然后使用用户登录。

现在,严重更改是否生效:

user $locale

systemd

在 systemd 中,使用 localectl 命令设置 locale。通过以下方法检查可用 locale 列表:

root #localectl list-locales

然后设置所需的 local:

root #localectl set-locale LANG=de_DE.utf8

最后检查结果是否良好:

root #localectl | grep "System Locale"
   System Locale: LANG=de_DE.utf8

控制台的键盘布局

OpenRC

控制台使用的键盘布局是在 /etc/conf.d/keymaps 中设置的 keymap 变量。可以在 /usr/share/keymaps/YOUR_ARCH/中找到有效值。i386 进一步细分为布局 (qwerty/azerty/ 等)。 有些语言有多种选择——请尝试各种不同的选择来决定哪一种最能满足你的需要。

FILE /etc/conf.d/keymaps设置控制台按键映射
keymap="de"
#keymap="de-latin1"
#keymap="de-latin1-nodeadkeys"

systemd

With systemd the keymap layout used for the console can be set using the localectl command. First check the available keymap layouts:

root #localectl list-keymaps

Then set the requested console keymap layout:

root #localectl set-keymap it

Finally check if the console keymap layout was set correctly:

root #localectl | grep "VC Keymap"
       VC Keymap: it

X服务的键盘布局

OpenRC

The keyboard layout to be used by the X server is specified in /etc/X11/xorg.conf.d/30-keyboard.conf by the XkbLayout option. For details visit the Xorg guide and the article about Keyboard layout switching.

systemd

With systemd the keymap layout for the X11 server can be set using the localectl command. First check the available X11 keymap layouts:

root #localectl list-x11-keymap-layouts

Then set the requested X11 keymap layout:

root #localectl set-x11-keymap it

Finally check if the X11 keymap layout was set correctly:

root #localectl | grep "X11 Layout"
      X11 Layout: it

本地语言支持

For message based localization to work in programs that support it and have the nls USE flag, compile the programs with this flag set. Message strings are installed in /usr/share/locale/<locale>/LC_MESSAGES/<package>.mo files. Most of the programs using the Native Language Support (NLS) also need the gettext library to extract and use localized messages. Of course, Portage will automatically install it when needed.

After enabling the nls USE flag some packages might need to be re-emerged:

root #emerge --ask --changed-use --deep --with-bdeps=y @world

LINGUAS

Warning
LINGUAS causes packages to implicitly skip locales. When using it, the package manager cannot determine which locales were omitted. Do not use LINGUAS if you intend to redistribute binary packages.

There is also an additional LINGUAS variable that is used by some gettext-based build systems to control which localization files are built and installed. The variable takes in space-separated list of language codes, and a suggested place to set it is /etc/portage/make.conf:

root #nano -w /etc/portage/make.conf
## (Add in the LINGUAS variable. For instance, for German, Finnish and English:)
LINGUAS="de fi en"

Note that there is a large difference between LINGUAS being unset and being set to an empty value: with LINGUAS="", most ebuilds would install only the packages' default language but none of the LC_MESSAGES files.

L10N

A USE_EXPAND variable called L10N decides which extra localization support will be installed. This is commonly used for downloads of additional language packs by packages. Similar to LINGUAS, the variable takes a space separated list of language tags, and it can be set in /etc/portage/make.conf:

root #nano -w /etc/portage/make.conf
## (Add in the L10N variable. For instance, for German and Brazilian Portuguese:)
L10N="de pt-BR"

To set it per-package, edit /etc/portage/package.use and prefix the requested language packs with "l10n_", as shown in the next example:

FILE /etc/portage/package.use
app-text/aspell l10n_de l10n_pt-BR

Note that while the common two letter language codes (like de or fr) are identical in LINGUAS and L10N, more complex entries have a different syntax because L10N uses IETF language tags (aka BCP 47). For example, pt_BR and sr@latin in LINGUAS become pt-BR and sr-Latn in L10N, respectively.

可用的 L10N 值列表在 /var/db/repos/gentoo/profiles/desc/l10n.desc 中提供:

user $grep -i portuguese /var/db/repos/gentoo/profiles/desc/l10n.desc
pt - Portuguese
pt-BR - Portuguese (Brazil)
pt-PT - Portuguese (Portugal)

After setting the L10N USE_EXPAND variable it may be necessary to re-emerge some packages:

root #emerge --ask --changed-use --deep --with-bdeps=y @world

另请参阅

外部资源

参考


This page is based on a document formerly found on our main website gentoo.org.
The following people contributed to the original document: Alexander Holler, Steven Lucy, Benny Chuang, Lars Weiler, Tobias Scherbaum, Flammie Pirinen, , Francisco Blas Izquierdo Riera (klondike)
They are listed here because wiki history does not allow for any external attribution. If you edit the wiki article, please do not add yourself here; your contributions are recorded on each article's associated history page.