Contents of /xml/htdocs/doc/en/utf-8.xml

Parent Directory Parent Directory | Revision Log Revision Log

Revision 1.1 - (show annotations) (download) (as text)
Thu Feb 3 12:36:39 2005 UTC (9 years, 10 months ago) by neysx
Branch: MAIN
File MIME type: application/xml
#77242 UTF-8 guide

1 <?xml version='1.0' encoding="UTF-8"?>
2 <!-- $Header$ -->
3 <!DOCTYPE guide SYSTEM "/dtd/guide.dtd">
5 <guide link="/doc/en/utf-8.xml">
6 <title>Using UTF-8 with Gentoo</title>
8 <author title="Author">
9 <mail link="slarti@gentoo.org">Thomas Martin</mail>
10 </author>
11 <author title="Contributor">
12 <mail link="devil@gentoo.org.ua">Alexander Simonov</mail>
13 </author>
15 <abstract>
16 This guide shows you how to set up and use the UTF-8 Unicode character set with
17 your Gentoo Linux system, after explaining the benefits of Unicode and more
18 specifically UTF-8.
19 </abstract>
21 <license />
23 <version>1.0</version>
24 <date>2005-02-03</date>
26 <chapter>
27 <title>Character Encodings</title>
28 <section>
29 <title>What is a Character Encoding?</title>
30 <body>
32 <p>
33 Computers do not understand text themselves. Instead, every character is
34 represented by a number. Traditionally, each set of numbers used to represent
35 alphabets and characters (known as a coding system, encoding or character set)
36 was limited in size due to limitations in computer hardware.
37 </p>
39 </body>
40 </section>
41 <section>
42 <title>The History of Character Encodings</title>
43 <body>
45 <p>
46 The most common (or at least the most widely accepted) character set is
47 <b>ASCII</b> (American Standard Code for Information Interchange). It is widely
48 held that ASCII is the most successful software standard ever. Modern ASCII
49 was standardised in 1986 (ANSI X3.4, RFC 20, ISO/IEC 646:1991, ECMA-6) by the
50 American National Standards Institute.
51 </p>
53 <p>
54 ASCII is strictly seven-bit, meaning that it uses bit patterns representable
55 with seven binary digits, which provides a range of 0 to 127 in decimal. These
56 include 32 non-visible control characters, most between 0 and 31, with the
57 final control character, DEL or delete at 127. Characters 32 to 126 are
58 visible characters: a space, punctuation marks, Latin letters and numbers.
59 </p>
61 <p>
62 The eighth bit in ASCII was originally used as a parity bit for error checking.
63 If this is not desired, it is left as 0. This means that, with ASCII, each
64 character is represented by a single byte.
65 </p>
67 <p>
68 Although ASCII was enough for communication in modern English, in other
69 European languages that include accented characters, things were not so easy.
70 The ISO 8859 standards were developed to meet these needs. They were backwards
71 compatible with ASCII, but instead of leaving the eighth bit blank, they used
72 it to allow another 127 characters in each encoding. ISO 8859's limitations
73 soon came to light, and there are currently 15 variants of the ISO 8859
74 standard (8859-1 through to 8859-15). Outside of the ASCII-compatible byte
75 range of these character sets, there is often conflict between the letters
76 represented by each byte. To complicate interoperability between character
77 encodings further, Windows-1252 is used in some versions of Microsoft Windows
78 instead for Western European languages. This is a superset of ISO 8859-1,
79 however it is different in several ways. These sets do all retain ASCII
80 compatibility, however.
81 </p>
83 <p>
84 The necessary development of completely different single-byte encodings for
85 non-Latin alphabets, such as EUC (Extended Unix Coding) which is used for
86 Japanese and Korean (and to a lesser extent Chinese) created more confusion,
87 while other operating systems still used different character sets for the same
88 languages, for example, Shift-JIS and ISO-2022-JP. Users wishing to view
89 cyrillic glyphs had to choose between KOI8-R for Russian and Bulgarian or
90 KOI8-U for Ukrainian, as well as all the other cyrillic encodings such as the
91 unsuccessful ISO 8859-5, and the common Windows-1251 set. All of these
92 character sets broke most compatibility with ASCII (although KOI8 encodings
93 place cyrillic characters in Latin order, so in case the eighth bit is
94 stripped, text is still decipherable on an ASCII terminal through case-reversed
95 transliteration.)
96 </p>
98 <p>
99 This has led to confusion, and also to an almost total inability for
100 multilingual communication, especially across different alphabets. Enter
101 Unicode.
102 </p>
104 </body>
105 </section>
106 <section>
107 <title>What is Unicode?</title>
108 <body>
110 <p>
111 Unicode throws away the traditional single-byte limit of character sets, and
112 even with two bytes per-character this allows a maximum 65,536 characters.
113 Although this number is extremely high when compared to seven-bit and eight-bit
114 encodings, it is still not enough for a character set designed to be used for
115 symbols and scripts used only by scholars, and symbols that are only used in
116 mathematics and other specialised fields.
117 </p>
119 <p>
120 Unicode has been mapped in many different ways, but the two most common are
121 <b>UTF</b> (Unicode Transformation Format) and <b>UCS</b> (Universal Character
122 Set). A number after UTF indicates the number of bits in one unit, while the
123 number after UCS indicates the number of bytes. UTF-8 has become the most
124 widespread means for the interchange of Unicode text as a result of its
125 eight-bit clean nature, and it is the subject of this document.
126 </p>
128 </body>
129 </section>
130 <section>
131 <title>UTF-8</title>
132 <body>
134 <p>
135 UTF-8 is a variable-length character encoding, which in this instance means
136 that it uses 1 to 4 bytes per symbol. So, the first UTF-8 byte is used for
137 encoding ASCII, giving the character set full backwards compatibility with
138 ASCII. UTF-8 means that ASCII and Latin characters are interchangeable with
139 little increase in the size of the data, because only the first bit is used.
140 Users of Eastern alphabets such as Japanese, who have been assigned a higher
141 byte range are unhappy, as this results in as much as a 50% redundancy in their
142 data.
143 </p>
145 </body>
146 </section>
147 <section>
148 <title>What UTF-8 Can Do for You</title>
149 <body>
151 <p>
152 UTF-8 allows you to work in a standards-compliant and internationally accepted
153 multilingual environment, with a comparitively low data redundancy. UTF-8 is
154 the preferred way for transmitting non-ASCII characters over the Internet,
155 through Email, IRC or almost any other medium. Despite this, many people regard
156 UTF-8 in online communication as abusive. It is always best to be aware of the
157 attitude towards UTF-8 in a specific channel, mailing list or Usenet group
158 before using <e>non-ASCII</e> UTF-8.
159 </p>
161 </body>
162 </section>
163 </chapter>
165 <chapter>
166 <title>Setting up UTF-8 with Gentoo Linux</title>
167 <section>
168 <title>Finding or Creating UTF-8 Locales</title>
169 <body>
171 <p>
172 Now that you understand the principles behind Unicode, you're ready to start
173 using UTF-8 with your system.
174 </p>
176 <p>
177 The preliminary requirement for UTF-8 is to have a version of glibc installed
178 that has national language support. The recommend means to do this is the
179 <path>/etc/locales.build</path> file in combination with the <c>userlocales</c>
180 USE flag. It is beyond the scope of this document to explain the usage of this
181 file though, luckily, the usage of this file is well documented in the comments
182 within it. It is also explained in the <uri
183 link="/doc/en/guide-localization.xml#doc_chap3_sect3"> Gentoo Localisation
184 Guide</uri>.
185 </p>
187 <p>
188 Next, we'll need to decide whether a UTF-8 locale is already available for our
189 language, or whether we need to create one.
190 </p>
192 <pre caption="Checking for an existing UTF-8 locale">
193 <comment>(Replace "en_GB" with your desired locale setting)</comment>
194 # <i>locale -a | grep 'en_GB'</i>
195 en_GB
196 en_GB.utf8
197 </pre>
199 <p>
200 From the output of this command line, we need to take the result with a suffix
201 similar to <c>.utf8</c>. If there is no result with a suffix similar to
202 <c>.utf8</c>, we need to create a UTF-8 compatible locale.
203 </p>
205 <note>
206 Only execute the following code listing if you do not have a UTF-8 locale
207 available for your language.
208 </note>
210 <pre caption="Creating a UTF-8 locale">
211 <comment>(Replace "en_GB" with your desired locale setting)</comment>
212 # <i>localedef -i en_GB -f UTF-8 en_GB.utf8</i>
213 </pre>
215 </body>
216 </section>
217 <section>
218 <title>Setting the Locale</title>
219 <body>
221 <p>
222 Although by now you might be determined to use UTF-8 system wide, the author
223 does not recommend setting UTF-8 for the root user. Instead, it is best to set
224 the locale in your user's <path>~/.profile</path> (or, if you are using a C
225 shell, <path>~/.login</path>).
226 </p>
228 <note>
229 If you are not sure which file to use, use <path>~/.profile</path>. Also, if
230 you are unsure which code listing to use, use the Bourne version.
231 </note>
233 <pre caption="Setting the locale with environment variables (Bourne version)">
234 export LANG="en_GB.utf8"
235 export LC_ALL="en_GB.utf8"
236 </pre>
238 <pre caption="Setting the locale with environment variables (C shell version)">
239 setenv LANG "en_GB.utf8"
240 setenv LC_ALL "en_GB.utf8"
241 </pre>
243 <p>
244 Now, logout and back in to apply the change. We want these environment
245 variables in our entire environment, so it is best to logout and back in, or at
246 the very least to source <path>~/.profile</path> or <path>~/.login</path> in
247 the console from which you have started other processes.
248 </p>
250 </body>
251 </section>
252 </chapter>
254 <chapter>
255 <title>Application Support</title>
256 <section>
257 <body>
259 <p>
260 When Unicode first started gaining momentum in the software world, multibyte
261 character sets were not well suited to languages like C, in which many of the
262 day-to-day programs people use are written. Even today, some programs are not
263 able to handle UTF-8 properly. Fortunately, most are!
264 </p>
266 </body>
267 </section>
268 <section>
269 <title>Filenames, NTFS, and FAT</title>
270 <body>
272 <p>
273 There are several NLS options in the Linux kernel configuration menu, but it is
274 important to not become confused! For the most part, the only thing you need to
275 do is to build UTF-8 NLS support into your kernel, and change the default NLS
276 option to utf8.
277 </p>
279 <pre caption="Kernel configuration steps for UTF-8 NLS">
280 File Systems --&gt;
281 Native Language Support --&gt;
282 (utf8) Default NLS Option
283 &lt;*&gt; NLS UTF8
284 <comment>(Also &lt;*&gt; other character sets that are in use in
285 your FAT filesystems or Joilet CD-ROMs.)</comment>
286 </pre>
288 <p>
289 If you plan on mounting NTFS partitions, you may need to specify an <c>nls=</c>
290 option with mount. For more information, see <c>man mount</c>.
291 </p>
293 <p>
294 For changing the encoding of filenames, <c>app-text/convmv</c> can be used.
295 </p>
297 <pre caption="Example usage of convmv">
298 # <i>emerge --ask app-text/convmv</i>
299 # <i>convmv -f current-encoding -t utf-8 filename</i>
300 </pre>
302 <p>
303 For changing the <e>contents</e> of files, use the <c>iconv</c> utility,
304 bundled with <c>glibc</c>:
305 </p>
307 <pre caption="Example usage of iconv">
308 <comment>(substitute iso-8859-1 with the charset you are converting from)</comment>
309 <comment>(Check the output is sane)</comment>
310 # <i>iconv -f iso-8859-1 -t utf-8 filename</i>
311 <comment>(Convert a file, you must create another file)</comment>
312 # <i>iconv -f iso-8859-1 -t utf-8 filename > newfile</i>
313 </pre>
315 <p>
316 <c>app-text/recode</c> can also be used for this purpose.
317 </p>
319 </body>
320 </section>
321 <section>
322 <title>The System Console</title>
323 <body>
325 <impo>
326 You need >=sys-apps/baselayout-1.11.9 for Unicode on the console.
327 </impo>
329 <p>
330 To enable UTF-8 on the console, you should edit <path>/etc/rc.conf</path> and
331 set <c>UNICODE="yes"</c>, and also read the comments in that file -- it is
332 important to have a font that has a good range of characters if you plan on
333 making the most of Unicode.
334 </p>
336 <p>
337 The <c>KEYMAP</c> variable, set in <path>/etc/conf.d/keymaps</path>, should
338 have a Unicode keymap specified. To do this, simply prepend the keymap already
339 specified there with -u.
340 </p>
342 <pre caption="Example /etc/conf.d/keymaps snippet">
343 <comment>(Change "uk" to your local layout)</comment>
344 KEYMAP="-u uk"
345 </pre>
347 </body>
348 </section>
349 <section>
350 <title>Ncurses and Slang</title>
351 <body>
353 <note>
354 Ignore any mention of Slang in this section if you do not have it installed or
355 do not use it.
356 </note>
358 <p>
359 It is wise to add <c>unicode</c> to your global USE flags in
360 <path>/etc/make.conf</path>, and then to remerge <c>sys-libs/ncurses</c> and
361 also <c>sys-libs/slang</c> if appropriate:
362 </p>
364 <pre caption="Emerging ncurses and slang">
365 <comment>(We avoid putting these libraries in our world file with --oneshot)</comment>
366 # <i>emerge --oneshot --verbose --ask sys-libs/ncurses sys-libs/slang</i>
367 </pre>
369 <p>
370 We also need to rebuild packages that link to these, now the USE changes have
371 been applied.
372 </p>
374 <pre caption="Rebuilding of programs that link to ncurses or slang">
375 # <i>revdep-rebuild --soname libncurses.so.5</i>
376 # <i>revdep-rebuild --soname libslang.so.1</i>
377 </pre>
379 </body>
380 </section>
381 <section>
382 <title>KDE, GNOME and Xfce</title>
383 <body>
385 <p>
386 All of the major desktop environments have full Unicode support, and will
387 require no further setup than what has already been covered in this guide. This
388 is because the underlying graphical toolkits (Qt or GTK+2) are UTF-8 aware.
389 Subsequently, all applications running on top of these toolkits should be
390 UTF-8-aware out of the box.
391 </p>
393 <p>
394 The exceptions to this rule come in Xlib and GTK+1. GTK+1 requires a
395 iso-10646-1 FontSpec in the ~/.gtkrc, for example
396 <c>-misc-fixed-*-*-*-*-*-*-*-*-*-*-iso10646-1</c>. Also, applications using
397 Xlib or Xaw will need to be given a similar FontSpec, otherwise they will not
398 work.
399 </p>
401 <note>
402 If you have a version of the gnome1 control center around, use that instead.
403 Pick any iso10646-1 font from there.
404 </note>
406 <pre caption="Example ~/.gtkrc (for GTK+1) that defines a Unicode compatible font">
407 style "user-font"
408 {
409 fontset="-misc-fixed-*-*-*-*-*-*-*-*-*-*-iso10646-1"
410 }
411 widget_class "*" style "user-font"
412 </pre>
414 <p>
415 If an application has support for both a Qt and GTK+2 GUI, the GTK+2 GUI will
416 generally give better results with Unicode.
417 </p>
419 </body>
420 </section>
421 <section>
422 <title>X11 and Fonts</title>
423 <body>
425 <impo>
426 <c>x11-base/xorg-x11</c> has far better support for Unicode than
427 <c>x11-base/xfree</c> and it is <e>highly</e> recommend that you adopt this X
428 server.
429 </impo>
431 <p>
432 TrueType fonts have support for Unicode, and most of the fonts that ship with
433 Xorg have impressive character support, although, obviously, not every single
434 glyph available in Unicode has been created for that font. To build fonts
435 (including the Bitstream Vera set) with support for East Asian letters with X,
436 make sure you have the <c>cjk</c> USE flag set. Many other applications utilise
437 this flag, so it may be worthwhile to add it as a permanent USE flag.
438 </p>
440 <p>
441 Also, several font packages in Portage are Unicode aware.
442 </p>
444 <pre caption="Optional: Install some more Unicode-aware fonts">
445 # <i>emerge terminus-font intlfonts freefonts cronyx-fonts corefonts</i>
446 </pre>
448 </body>
449 </section>
450 <section>
451 <title>Window Managers and Terminal Emulators</title>
452 <body>
454 <p>
455 Window managers not built on GTK or Qt generally have very good Unicode
456 support, as they often use the Xft library for handling fonts. If your window
457 manager does not use Xft for fonts, you can still use the FontSpec mentioned in
458 the previous section as a Unicode font.
459 </p>
461 <p>
462 Terminal emulators that use Xft and support Unicode are harder to come by.
463 Aside from Konsole and gnome-terminal, the best options in Portage are
464 <c>x11-terms/rxvt-unicode</c>, <c>xfce-extra/terminal</c>,
465 <c>app-gnustep/terminal</c>, <c>x11-terms/mlterm</c>, <c>x11-terms/mrxvt</c> or
466 plain <c>x11-terms/xterm</c> when built with the <c>unicode</c> USE flag and
467 invoked as <c>uxterm</c>. <c>app-misc/screen</c> supports UTF-8 too, when
468 invoked as <c>screen -u</c> or the following is put into the
469 <path>~/.screenrc</path>:
470 </p>
472 <pre caption="~/.screenrc for UTF-8">
473 defutf8 on
474 </pre>
476 </body>
477 </section>
478 <section>
479 <title>Vim, Emacs, Xemacs and Nano</title>
480 <body>
482 <p>
483 Vim, Emacs and Xemacs provide full UTF-8 support, and also have builtin
484 detection of UTF-8 files. For further information in Vim, use <c>:help
485 mbyte.txt</c>.
486 </p>
488 <p>
489 Nano currently does not provide support for UTF-8, although it has been planned
490 for a long time. With luck, this will change in future. At the time of writing,
491 UTF-8 support is in Nano's CVS, and should be included in the next release.
492 </p>
494 </body>
495 </section>
496 <section>
497 <title>Shells</title>
498 <body>
500 <p>
501 Currently, <c>bash</c> provides full Unicode support through the GNU readline
502 library. Z Shell users are in a somewhat worse position -- no parts of the
503 shell have Unicode support, although there is a concerted effort to add
504 multibyte character set support underway at the moment.
505 </p>
507 <p>
508 The C shell, <c>tcsh</c> and <c>ksh</c> do not provide UTF-8 support at all.
509 </p>
511 </body>
512 </section>
513 <section>
514 <title>Irssi</title>
515 <body>
517 <p>
518 Irssi has complete UTF-8 support, although it does require a user to set an
519 option.
520 </p>
522 <pre caption="Enabling UTF-8 in Irssi">
523 /set term_charset UTF-8
524 </pre>
526 <p>
527 For channels where non-ASCII characters are often exchanged in non-UTF-8
528 charsets, the <c>/recode</c> command may be used to convert the characters.
529 Type <c>/help recode</c> for more information.
530 </p>
532 </body>
533 </section>
534 <section>
535 <title>Mutt</title>
536 <body>
538 <p>
539 The Mutt mail user agent has very good Unicode support. To use UTF-8 with Mutt,
540 put the following in your <path>~/.muttrc</path>:
541 </p>
543 <pre caption="~/.muttrc for UTF-8">
544 set send_charset="utf8" <comment>(outgoing character set)</comment>
545 set charset="utf8" <comment>(display character set)</comment>
546 </pre>
548 <note>
549 You may still see '?' in mail you read with Mutt. This is a result of people
550 using Latin (ISO 8859) or another charset for email transmission. It is best to
551 tell them to use UTF-8 for mail, and point them to the IETF RFC 2277 (see
552 References at the end of this document). Also note that in some lists,
553 subscribers may not like UTF-8. Be sure that the group or person you are
554 communicating with does not mind UTF-8.
555 </note>
557 <p>
558 Further information is available from the <uri
559 link="http://wiki.mutt.org/index.cgi?MuttFaq/Charset"> Mutt WikiWiki</uri>.
560 </p>
562 </body>
563 </section>
564 <section>
565 <title>Testing it all out</title>
566 <body>
568 <p>
569 There are numerous UTF-8 test websites around. <c>net-www/w3m</c>,
570 <c>net-www/links</c>, <c>net-www/elinks</c>, <c>net-www/lynx</c> and all
571 Mozilla based browsers (including Firefox) support UTF-8. Konqueror has full
572 UTF-8 support too.
573 </p>
575 <p>
576 When using one of the text-only web browsers, make absolutely sure you are
577 using a Unicode-aware terminal.
578 </p>
580 <p>
581 If you see certain characters displayed as boxes with letters or numbers
582 inside, this means that your font does not have a character for the symbol or
583 glyph that the UTF-8 wants. Instead, it displays a box with the hex code of the
584 UTF-8 symbol.
585 </p>
587 <ul>
588 <li>
589 <uri link="http://www.w3.org/2001/06/utf-8-test/UTF-8-demo.html">A W3C
590 UTF-8 Test Page</uri>
591 </li>
592 <li>
593 <uri link="http://titus.uni-frankfurt.de/indexe.htm?/unicode/unitest.htm">
594 A UTF-8 test page provided by the University of Frankfurt</uri>
595 </li>
596 </ul>
598 </body>
599 </section>
600 <section>
601 <title>Input Methods</title>
602 <body>
604 <p>
605 <e>Dead keys</e> may be used to input characters in X that are not included on
606 your keyboard. These work by pressing your right Alt key (or in some countries,
607 AltGr) and an optional key from the non-alphabetical section of the keyboard to
608 the left of the return key at once, releasing them, and then pressing a letter.
609 The dead key should modify it. Input can be further modified by using the Shift
610 key at the same time as pressing the AltGr and modifier.
611 </p>
613 <p>
614 To enable dead keys in X, you need a layout that supports it. Most European
615 layouts already have dead keys with the default variant. However, this is not
616 true of North American layouts. Although there is a degree of inconsistency
617 between layouts, the easiest solution seems to be to use a layout in the form
618 "en_US" rather than "us", for example. The layout is set in
619 <path>/etc/X11/xorg.conf</path> like so:
620 </p>
622 <pre caption="/etc/X11/xorg.conf snippet">
623 Section "InputDevice"
624 Identifier "Keyboard0"
625 Driver "kbd"
626 Option "XkbLayout" "en_US" <comment># Rather than just "us"</comment>
627 <comment>(Other Xkb options here)</comment>
628 EndSection
629 </pre>
631 <note>
632 The preceding change only needs to be applied if you are using a North American
633 layout, or another layout where dead keys do not seem to be working. European
634 users should have working dead keys as is.
635 </note>
637 <p>
638 This change will come into effect when your X server is restarted. To apply the
639 change now, use the <c>setxkbmap</c> tool, for example, <c>setxkbmap en_US</c>.
640 </p>
642 <p>
643 It is probably easiest to describe dead keys with examples. Although the
644 results are locale dependent, the concepts should remain the same regardless of
645 locale. The examples contain UTF-8, so to view them you need to either tell
646 your browser to view the page as UTF-8, or have a UTF-8 locale already
647 configured.
648 </p>
650 <p>
651 When I press AltGr and [ at once, release them, and then press a, 'ä' is
652 produced. When I press AltGr and [ at once, and then press e, 'ë' is produced.
653 When I press AltGr and ; at once, 'á' is produced, and when I press AltGr and ;
654 at once, release them, and then press e, 'é' is produced.
655 </p>
657 <p>
658 By pressing AltGr, Shift and [ at once, releasing them, and then pressing a, a
659 Scandinavian 'å' is produced. Similarly, when I press AltGr, Shift and [ at
660 once, release <e>only</e> the [, and then press it again, '˚' is produced.
661 Although it looks like one, this (U+02DA) is not the same as a degree symbol
662 (U+00B0). This works for other accents produced by dead keys — AltGr and [,
663 releasing only the [, then pressing it again makes '¨'.
664 </p>
666 <p>
667 AltGr can be used with alphabetical keys alone. For example, AltGr and m, a
668 Greek lower-case letter mu is produced: 'µ'.
669 </p>
671 </body>
672 </section>
673 <section>
674 <title>Resources</title>
675 <body>
677 <ul>
678 <li>
679 <uri link="http://www.wikipedia.com/wiki/Unicode">The Wikipedia entry for
680 Unicode</uri>
681 </li>
682 <li>
683 <uri link="http://www.wikipedia.com/wiki/UTF-8">The Wikipedia entry for
684 UTF-8</uri>
685 </li>
686 <li><uri link="http://www.unicode.org">Unicode.org</uri></li>
687 <li><uri link="http://www.utf-8.com">UTF-8.com</uri></li>
688 <li><uri link="http://www.ietf.org/rfc/rfc3629.txt">RFC 3629</uri></li>
689 <li><uri link="http://www.ietf.org/rfc/rfc2277.txt">RFC 2277</uri></li>
690 </ul>
692 </body>
693 </section>
694 </chapter>
695 </guide>

  ViewVC Help
Powered by ViewVC 1.1.20