UTF-8/ru

UTF-8 — это кодировка символов переменной длины, что, в данном случае, означает длину от 1 до 4 байт на символ. Первый байт UTF-8 используется для кодирования ASCII, что означает, что данный набор символов полностью обратно совместим с ASCII. UTF-8 означает, что символы ASCII и Latin полностью взаимозаменяемы с небольшим увеличением размера данных, так как используется только первый байт. Пользователи восточных алфавитов, например, японского, которым назначили диапазон с большим числом байт несчастливы, так как это приводит к 50%-й избыточности в их данных.

Что такое кодировка символов?
Компьютеры неспособны воспринимать текст как человек. Вместо этого они каждый символ представляют как число. Традиционно каждый набор чисел, используемый для представления алфавитов и символов (известный как кодировка или набор символов), ограничен по размеру в силу ограничений оборудования.

История кодировок символов
The most common (or at least the most widely accepted) character set is ASCII (American Standard Code for Information Interchange). It is widely held that ASCII is the most successful software standard ever created. Modern ASCII was standardized in 1986 (ANSI X3.4, RFC 20, ISO/IEC 646:1991, ECMA-6) by the American National Standards Institute.

ASCII является строго семибитной кодировкой, из чего следует, что она использует семь двоичных цифр, то есть интервал от 0 до 127. ASCII начинается с 32 невидимых управляющих символов (с кодами от 0 до 31) и заканчивается управляющим символом DEL (или delete) с кодом 127. Символы в диапазоне от 32 до 126 видимы &mdash; это пробел, знаки препинания, латинские буквы и цифры.

The eighth bit in ASCII was originally used as a parity bit for error checking. If error checking is not desired, it is left as 0. This means that, with ASCII, each character is represented by a single byte.

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 throws away the traditional single-byte limit of character sets. It uses 17 "planes" of 65,536 code points to describe a maximum of 1,114,112 characters. As the first plane, aka. "Basic Multilingual Plane" or BMP, contains almost every character a user will ever need. Many have made the wrong assumption that Unicode was a 16-bit character set.

Unicode has been mapped in many different ways, but the two most common are UTF (Unicode Transformation Format) and UCS (Universal Character Set). A number after UTF indicates the number of bits in one unit, while the number after UCS indicates the number of bytes. UTF-8 has become the most widespread means for the interchange of Unicode text as a result of its eight-bit clean nature; it is therefore the subject of this document.

What UTF-8 can do
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
Now that the principles behind Unicode have been laid out, get ready to start using UTF-8 locally!

For users interested in more knowledge further explanation can be found in the Gentoo Localization Guide.

Next, the user needs to decide whether a UTF-8 locale is available for the language of choice, or whether one needs to be generated.

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.

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 locale-gen command. Locales will be written to the locale-archive.

Настройка локали
There is one environment variable that needs to be set in order to use the new UTF-8 locales:  (optionally modify the   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 (/bin/sh for Bourne shell users),  or  (/bin/bash 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:

Далее следует обновить среду.

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

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 eselect utility.

Use eselect to list the available locales on the system:

Using eselect 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 eselect 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.

Поддержка приложениями
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.

Имена файлов, NTFS и FAT
There are several NLS options in the Linux kernel configuration menu, but it is important to not become confused. For the most part, the only thing that needs to be done is to build UTF-8 NLS support into the kernel, and change the default NLS option to utf8.

When planning to mount NTFS partitions, users may need to specify a  option with mount. When planning on mounting FAT partitions, users may need to specify a  option with mount. Optionally, users can also set a default codepage for FAT in the kernel configuration.

Avoid setting  to UTF-8; it is not recommended. Instead, pass the  option when mounting FAT partitions. For further information man mount</tt> or see the appropriate kernel documentation at

Чтобы сменить кодировку имен файлов, используйте.

The format of the convmv</tt> command is as follows:

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

For changing the contents of files, use the iconv</tt> 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.

Системная консоль
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.

В файле переменной   должна соответствовать раскладка Unicode.

Ncurses и 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:

Также понадобится пересобрать пакеты, зависящие от них, раз изменения USE	вступили в силу. Используемая утилита входит в пакет.

KDE, GNOME и Xfce
Все основные графические оболочки полностью совместимы с Юникодом и не требуют дополнительной настройки, кроме той, что описана в этом документе. Все это	благодаря тому, графические библиотеки (Qt и GTK+2) совместимы с UTF-8. Следовательно, все приложения, работающие на основе этих библиотек, также должны поддерживать UTF-8 без дополнительных настроек.

On GTK+ based applications, the key sequence for hexadecimal Unicode input is Ctrl-Shift-u.

Исключением в этом правиле является Xlib и GTK+1. GTK+1 требует iso-10646-1 FontSpec в ~/.gtkrc, например. Также приложения, использующие Xlib или Xaw, нуждаются в подобной FontSpec, иначе они не буду работать.

Если приложение одинаково поддерживает Qt и GTK+2, то обычно GTK+2 GUI выдает лучшие результаты при работе с Юникодом.

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 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.

Эмуляторы терминала, использующие Xft и поддерживающие Юникод найти сложнее. Кроме Konsole и gnome-terminal, лучшим выбором в Portage будет, , , или просто , собранный с USE-флагом   и запускаемый как. тоже поддерживает UTF-8, если запускается с параметром, или в файле  есть следующая строчка:

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

GNU Emacs с версии 23 и XEmacs версии 21.5 имеют полную поддержку UTF-8. GNU Emacs 24 также поддерживает редактирование текста в обоих направлениях.

Nano полностью поддерживает UTF-8 начиная с версии 1.3.6.

Командные оболочки
Currently,  provides full Unicode support through the GNU readline library. Z Shell (zsh</tt>) offers Unicode support with the  USE flag.

The C shell, tcsh</tt> and ksh</tt> do not provide UTF-8 support at all.

Irssi
Irssi полностью поддерживает UTF-8, хотя для этого требуется дополнительная настройка пользователем.

Для каналов, где не-ASCII символы чаще всего передаются в не-UTF-8 кодировках, может пригодиться команда  для перекодировки символов. Наберите  для большего количества информации.

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.

Для более детальной информации смотри Mutt Wiki.

Man
Man pages are an integral part of any Linux machine. To ensure that any unicode in the man pages render correctly, edit and replace a line as shown below.

links и elinks
These are commonly used text-based browsers, and we shall see how we can enable UTF-8 support on them. On elinks</tt> and links</tt>, 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 elinks</tt> or links</tt> 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 links</tt> one may have to do a repeat + and then press to save. The config file option, is shown below.

Samba
Samba это набор программ, реализующих протокол SMB (Server Message Block) для UNIX-систем (Mac, Linux и FreeBSD). Этот протокол также иногда упоминается как Common Internet File System (CIFS). Samba содержит также систему NetBIOS, используемую для предоставления доступа к файлам в Windows-сетях.

Добавьте следующее в раздел [global]:

Проверка работоспособности
Есть множество сайтов, использующих UTF-8. ,, , и все браузеры, основанные на движке Mozilla (включая Firefox), поддерживают UTF-8. Konqueror и Opera тоже полностью поддерживают UTF-8.

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 a character for the symbol or glyph that the UTF-8 wants. 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

Input methods
Dead keys may be used to input characters in X that are not included on the keyboard. These work by pressing the right key (or in some countries, ) and an optional key from the non-alphabetical section of the keyboard to the left of the return key at once, releasing them, and then pressing a letter. The dead key should modify it. Input can be further modified by using the key at the same time as pressing the  and modifier.

To enable dead keys in X, a layout needs to be selected that supports it. Most European layouts already have dead keys with the default variant. However, this is not true of North American layouts. Although there is a degree of inconsistency between layouts, the easiest solution seems to be to use a layout in the form "en_US" rather than "us", for example. The layout is set in like so:

This change will come into effect when the X server is restarted. To apply the change now, use the setxkbmap</tt> tool, for example, setxkbmap en_US</tt>.

It is probably easiest to describe dead keys with examples. Although the results are locale dependent, the concepts should remain the same regardless of locale. The examples contain UTF-8, so to view them tell the browser to view the page as UTF-8, or have a UTF-8 locale already configured.

Если одновременно нажать и, отпустить, а затем нажать , получим символ 'ä'. Если одновременно нажать и, а затем , получим 'ë'. Если одновременно нажать и, затем , получим 'á'. Если одновременно нажать и, затем , получим 'é'.

Если одновременно нажать, и , отпустить их и нажать , получим скандинавскую 'å'. Аналогично, при одновременном нажатии, и , отпускании только  и повторном ее нажатии, мы получим '°'. Хотя этот символ (U+02DA) и похож на символ градуса (U+00B0), на самом деле он им не является.

Клавиша может быть использована вместе с одной из алфавитных клавиш. Например, комбинация и  выводит греческую строчную букву мю. и выводит немецкий символ эсцет и т. д. Множество европейцев захотят установить сочетание и  (или  в зависимости от наклеек или гравировок на клавишах), выводящее символ Евро, '€' (так как на их клавиатурах он нанесен).

External resources

 * The Wikipedia entry for Unicode
 * The Wikipedia entry for UTF-8
 * Unicode.org
 * UTF-8.com
 * RFC 3629
 * RFC 2277
 * Characters vs. Bytes
 * Locales and Internationalization

Системные конфигурационные файлы (в /etc)
Большинство системных конфигурационных файлов (например, ) не поддерживают UTF-8. Рекомендуется придерживаться набора символов ASCII при работе с этими файлами.