UTF-8/zh-cn

UTF-8 是Article description::一种可变长度的字符编码，在本例中，这意味着它每个符号使用 1 到 4 个字节. 因此，第一个 UTF-8 字节用于编码 ASCII，使字符集与 ASCII 完全向后兼容. UTF-8 意味着 ASCII 和拉丁字符可以互换，数据大小几乎没有增加，因为只使用第一个字节. 被分配了更高字节范围的东方字母（如日语）用户就不会那么幸福了，因为这会导致他们的数据有多达 50% 的冗余.

什么是字符编码？
计算机本身并不像人类那样理解印刷文本. 对于计算机，文本的每个字符都由一个数字表示. 传统上，用于表示字母和字符的每组数字（称为编码系统、编码或字符集）的大小都受到计算机硬件限制.

字符编码的历史
最常见（或至少是最广泛接受的）字符集是 ASCII（美国信息交换标准代码）. 人们普遍认为 ASCII 是有史以来最成功的软件标准. 现代 ASCII 于 1986 年由美国国家标准协会标准化（ANSI X3.4, RFC 20, ISO/IEC 646:1991, ECMA-6）.

ASCII 是严格的七位，这意味着它使用可以用七位二进制数字表示的位模式，它提供 0 到 127 的十进制范围. 其中包括 32 个不可见的控制字符，大多数在 0 到 31 之间，最后一个控制字符 DEL 或删除位于 127. 字符 32 到 126 是可见字符：空格、标点符号、拉丁字母和数字.

ASCII 中的第 8 位最初用作错误检查的奇偶校验位. 如果不需要错误检查，则保留为 0. 这意味着，对于 ASCII，每个字符都由一个字节表示.

Although ASCII was enough for communication in modern English, in other European languages that include accented characters, things were not so easy. The ISO 8859 standards were developed to meet these needs. They were backwards compatible with ASCII, but instead of leaving the eighth bit blank, they used it to allow another 127 characters in each encoding. ISO 8859's limitations soon came to light, and there are currently 15 variants of the ISO 8859 standard (8859-1 through to 8859-15). Outside of the ASCII-compatible byte range of these character sets, there is often conflict between the letters represented by each byte. To complicate interoperability between character encodings further, Windows-1252 is used in some versions of Microsoft Windows instead for Western European languages. This is a super-set of ISO 8859-1, however it is different in several ways; these sets do all retain ASCII compatibility.

The necessary development of completely different single-byte encodings for non-Latin alphabets, such as EUC (Extended Unix Coding) which is used for Japanese and Korean (and to a lesser extent Chinese) created more confusion. Other operating systems still used different character sets for the same languages, for example, Shift-JIS and ISO-2022-JP. Users wishing to view cyrillic glyphs had to choose between KOI8-R for Russian and Bulgarian or KOI8-U for Ukrainian, as well as all the other cyrillic encodings such as the unsuccessful ISO 8859-5, and the common Windows-1251 set. All of these character sets broke most compatibility with ASCII. Although it should be mentioned KOI8 encodings place cyrillic characters in Latin order, so in case the eighth bit is stripped, text is still decipherable on an ASCII terminal through case-reversed transliteration.

All of this has led to mass confusion, and to an almost total inability for multilingual communication; especially across different alphabets. Enter Unicode.

什么是 Unicode?
Unicode 抛弃了传统的单字节字符集限制. 它使用 65536 个码位的 17 个“平面（plane）”来描述最多 1114112 个字符. 作为第一个平面，又名“基本多文种平面”或 BMP，几乎包含用户需要的所有字符. 许多人错误地认为 Unicode 是 16 位字符集.

Unicode 有多种不同的映射方式，但最常见的两种是 UTF（Unicode 转换格式）和 UCS（通用字符集）. UTF 后面的数字表示一个单位的位数，UCS 后面的数字表示字节数. UTF-8 由于其 8-bit clean 的特性，已成为 Unicode 文本交换最普遍的方式；因此，这是本文档的主题.

UTF-8 能做什么
UTF-8 allows users to work in a standards-compliant and internationally accepted multilingual environment, with a comparatively low data redundancy. It is the preferred way for transmitting non-ASCII characters over the Internet, through Email, IRC, or almost any other medium. Despite this, many people regard UTF-8 in online communication as abusive. It is always best to be aware of the attitude towards UTF-8 in a specific channel, mailing list, or Usenet group before using non-ASCII UTF-8.

查找或创建 UTF-8 语言环境
既然 Unicode 背后的原理已经阐述完毕，接下来就可以在本地开始使用 UTF-8 了！

对更多知识感兴趣的用户可以在 Gentoo 本地化指南中找到进一步的解释.

接下来，用户需要决定 UTF-8 语言环境是否可用于选择的语言，或者是否需要生成一个.

From the output of the above command, look for a result with a suffix similar to. If there is no result with a similar suffix a UTF-8 compatible locale must be created. The command lists the suffix in lower case without any hyphens, glibc understands both forms of the suffix, many other programs don't. The most common example of which is X. So it is best to always use UTF-8 in preference to utf8.

Replace "en_GB" with the desired locale setting:

Another way to include a UTF-8 locale is to add it to the file and generate necessary locales using the  command. Locales will be written to the locale-archive.

设定 locale
There is one environment variable that needs to be set in order to use the new UTF-8 locales: LC_CTYPE (optionally modify the LANG variable to change the system language as well). There are also many different ways to set it; some system administrators prefer to only have a UTF-8 environment for a specific user, in which case they set them in their ( for Bourne shell users),  or  ( for Bourne again shell users). More details and best practices can be found in the Localization Guide.

Still others prefer to set the locale globally. One specific circumstance where the author particularly recommends doing this is when is in use, because this init script starts the display manager and desktop before any of the aforementioned shell startup files are sourced. In other words, this is performed before any of the variables are loaded in the environment.

Setting the locale globally should be done using file. This file should look something like the following:

Next, the environment must be updated by running the following command:

Now, run with no arguments to see if the correct variables have been loaded in the environment:

The values of locale environment variables that have been explicitly set e.g. in an export statement (if using bash) are listed without double quotes. Those whose value has been inherited from other locale environment variables have their values in double quotes.

Alternatively, using eselect to set locales
Although it is good to maintain the system as described above, it is possible to verify the correct locale configured using the utility.

Use to list the available locales on the system:

Using setting the locale is as simple as listing them. Once the correct locale has been determined invoke:

Check the result:

In case it is preferred to have with   instead of , run the appropriate  command:

Running the following command will update the variables in the shell:

That is everything. The system is now using UTF-8 locales. The next hurdle is the configuration of the applications used from day to day.

Application support
When Unicode first started gaining momentum in the software world, multibyte character sets were not well suited to languages like C, which is the base language of most commonly used programs. Even today, some programs are not able to handle UTF-8 properly. Fortunately the majority of programs, especially the common ones, are supported.

(V)FAT
For UTF-8 support in FAT filesystems see the FAT article.

Filenames
For changing the encoding of filenames, can be used.

The format of the command is as follows:

Substitute iso-8859-1 with the charset being converted from:

For changing the contents of files, use the utility, it comes bundled with  and should be installed on all Gentoo systems. Substitute iso-8859-1 with the charset being converted from. After running the command be sure to check for sane output:

To convert a file, another file must be created:

The recode package can also be used for this purpose.

The system console
To enable UTF-8 on the console edit. Set  and read the comments -- it is important to have a font that has a good range of characters to make the most of Unicode. For this to work make sure the Unicode locale has been properly created.

The keymap variable, set in, should have a Unicode keymap specified.

Ncurses and Slang
It is wise to add to the global USE flags in, and then to re-emerge  and. Portage will do this automatically if the  or   options are used. Run the following command to pull in the packages:

We also need to rebuild packages that link to these, now the USE changes have been applied. The tool we use is part of the  package.

KDE, GNOME, and Xfce
All of the major desktop environments have full Unicode support, and will require no further setup than what has already been covered in this guide. This is because the underlying graphical toolkits (Qt or GTK 2) are UTF-8 aware. Subsequently, all applications running on top of these toolkits should be UTF-8-aware out of the box.

The exceptions to this rule come in Xlib and GTK 1. GTK 1 requires a iso-10646-1 FontSpec in the, for example. Also, applications using Xlib or Xaw will need to be given a similar FontSpec, otherwise they will not work.

If an application has support for both a Qt and GTK 2 GUI, the GTK 2 GUI will generally give better results with Unicode.

X11 and fonts
TrueType fonts have support for Unicode, and most of the fonts that ship with Xorg have extensive character support, although, obviously, not every single glyph available in Unicode has been created for that font.

Also, many font packages in Portage are Unicode aware. See the Fontconfig page for more information on recommended fonts and configuration.

Window managers and terminal emulators
Window managers not built on GTK or Qt generally have very good Unicode support, as they often use the Xft library for handling fonts. If the window manager does not use Xft for fonts, then it is still possible to use the FontSpec mentioned in the previous section as a Unicode font.

Terminal emulators that use Xft and support Unicode are harder to come by. Aside from Konsole and GNOME Terminal, the best options in Portage are, , , , or plain when built with the   USE flag and invoked as. supports UTF-8 too, when invoked as or the following is put into the :

Vim, emacs, xemacs, and nano
Vim provides full UTF-8 support, and also has builtin detection of UTF-8 files. For further information in Vim, use.

GNU Emacs since version 23 and XEmacs version 21.5 have full UTF-8 support. GNU Emacs 24 also supports editing bidirectional text.

Nano has provided full UTF-8 support since version 1.3.6.

Shells
Currently, provides full Unicode support through the GNU readline library. Z Shell offers Unicode support with the   USE flag.

The C shell, and  do not provide UTF-8 support at all.

Irssi
Irssi has complete UTF-8 support, although it does require a user to set an option.

For channels where non-ASCII characters are often exchanged in non-UTF-8 charsets, the command may be used to convert the characters. Type for more information.

Mutt
The Mutt mail user agent has very good Unicode support. To use UTF-8 with Mutt, nothing needs to be put in the configuration files. Mutt will work under Unicode environment without modification if all the configuration files (signature included) are UTF-8 encoded.

Further information is available from the Mutt Wiki.

links and elinks
These are commonly used text-based browsers, and we shall see how we can enable UTF-8 support on them. On and, there are two ways to go about this, one using the Setup option from within the browser or editing the config file. To set the option through the browser, open a site with or  and then + to enter the Setup Menu then select Terminal options, or press. Scroll down and select the last option  by pressing. Then Save and exit the menu. On one may have to do a repeat + and then press  to save. The config file option, is shown below.

Samba
Samba is a software suite which implements the SMB (Server Message Block) protocol for UNIX systems such as Macs, Linux and FreeBSD. The protocol is also sometimes referred to as the Common Internet File System (CIFS). Samba also includes the NetBIOS system - used for file sharing over windows networks.

Add the following lines under the  section:

全部测试
There are numerous UTF-8 test websites around and most of the popular browsers in Gentoo have full UTF-8 support.

When using one of the text-only web browsers, make absolutely sure a Unicode-aware terminal is used.

If certain characters are displayed as boxes with letters or numbers inside, then the current font does not have glyphs for those characters. Instead, it displays a box with the hex code of the UTF-8 symbol.


 * unicode-table.com
 * A W3C UTF-8 Test Page
 * A UTF-8 test page provided by the University of Frankfurt

系统配置文件（在 /etc 中）
Most system configuration files (such as ) do not support UTF-8. It is recommended to stick with the ASCII character set for these files.

外部资源

 * The Wikipedia entry for Unicode
 * The Wikipedia entry for UTF-8
 * Unicode.org
 * UTF-8.com
 * RFC 3629
 * RFC 2277
 * Characters vs. Bytes
 * The GNU C Library: Locales and Internationalization
 * Unifoundry.com - Unicode Tutorial
 * unicode USE flag description