--- title: 章 22. 在地化 - i18n/L10n 使用與安裝 part: 部 III. 系統管理 prev: books/handbook/virtualization next: books/handbook/cutting-edge showBookMenu: true weight: 26 params: path: "/books/handbook/l10n/" --- [[l10n]] = 在地化 - i18n/L10n 使用與安裝 :doctype: book :toc: macro :toclevels: 1 :icons: font :sectnums: :sectnumlevels: 6 :sectnumoffset: 22 :partnums: :source-highlighter: rouge :experimental: :images-path: books/handbook/l10n/ ifdef::env-beastie[] ifdef::backend-html5[] :imagesdir: ../../../../images/{images-path} endif::[] ifndef::book[] include::shared/authors.adoc[] include::shared/mirrors.adoc[] include::shared/releases.adoc[] include::shared/attributes/attributes-{{% lang %}}.adoc[] include::shared/{{% lang %}}/teams.adoc[] include::shared/{{% lang %}}/mailing-lists.adoc[] include::shared/{{% lang %}}/urls.adoc[] toc::[] endif::[] ifdef::backend-pdf,backend-epub3[] include::../../../../../shared/asciidoctor.adoc[] endif::[] endif::[] ifndef::env-beastie[] toc::[] include::../../../../../shared/asciidoctor.adoc[] endif::[] [[l10n-synopsis]] == 概述 FreeBSD 計劃的使用者及貢獻者分佈在世界各地,也因此 FreeBSD 支援多語系,讓使用者可以使用非英文語言來檢視、輸入或處理資料。使用者可以選擇大多數主要語言,包含但不限於以下語言:中文、德文、日文、韓文、法文、俄文及越南文。 `國際化` (Internationalization) 一詞可以縮寫為 i18n,即 第一個字母到最後一個字母間的字母數量。L10n 也使用同樣的命名規則,但源自 `在地化` (Localization)。 i18n/L10n 的方法、協定及應用程式讓使用者可以自己選擇使用的語言。 本章會討論 FreeBSD 的國際化及在地化功能。在閱讀本章之後,您會了解: * 語系名稱如何組成。 * 如何設定登入 Shell 的語系。 * 如何設定 Console 給非英文語言的使用者。 * 如果設定 Xorg 使用不同語言。 * 如何找到支援 i18n 的應用程式。 * 那裡可以找到更多設定特定語言的資訊。 在開始閱讀這章之前,您需要: * 了解如何 crossref:ports[ports,安裝其他第三方應用程式]。 [[using-localization]] == 使用語系 語系設定值由三個元件所組成:語言代號、城市代號及編碼。語系名稱組成的方式如下: [.programlisting] .... LanguageCode_CountryCode.Encoding .... _LanguageCode_ 與 _CountryCode_ 用來表示城市及特定語言。<> 提供了幾個 __LanguageCode_CountryCode__ 的範例: [[locale-lang-country]] .常用語言及城市代碼 [cols="1,1", frame="none", options="header"] |=== | 語言代號_城市代號 | 說明 |en_US |英文,美國 |ru_RU |俄文,俄國 |zh_TW |繁體中文,台灣 |=== 完整可用的語系清單可用以下指令查詢: [source,shell] .... % locale -a | more .... 查詢目前使用的語系設定: [source,shell] .... % locale .... 語言特定的字元集如 ISO8859-1, ISO8859-15, KOI8-R 及 CP437 在 man:multibyte[3] 有詳細說明。可用的字元集可在 http://www.iana.org/assignments/character-sets[IANA Registry] 查詢。 某些語言,如中文或日文,無法使用 ASCII 字元表示,會需要使用寬 (Wide) 字元或多位元組 (Multibyte) 字元來擴充的語言編碼。EUC 與 Big5 即是使用寬子元或多位元組字元的例子。舊的應用程式會誤判這些字元為控制字元,新的應用程式則通常可以辨識這些字元,依實作的需要,使用者可能需要開啟寬字元或多位元組字元支援或者使用正確的字元設定來編譯應用程式。 [NOTE] ==== FreeBSD 使用 Xorg 相容的語系編碼。 ==== 本節剩餘的部份將說明各種在 FreeBSD 系統上設定語系的方法。下一節將會探討如何尋找以及編譯使用 i18n 支援的應用程式。 [[setting-locale]] === 設定登入 Shell 的語系 語系設定可在使用者的 [.filename]#~/.login_conf# 或使用者的 Shell 的啟動檔設定:[.filename]#~/.profile#, [.filename]#~/.bashrc# 或 [.filename]#~/.cshrc#。 有兩個環境變數需要設定: * `LANG` 用來設定語系 * `MM_CHARSET` 用來設定應用程式所使用的 MIME 字元集 除了使用者的 Shell 設定外,這些變數也應針對特定應用程式設定以及 Xorg 設定。 兩種可以完成所需變數設定的方法有:<> 法 (較建議) 及 <> 法。以下兩節將示範如何使用這兩個方法。 [[login-class]] ==== 登入類別 (Login Class) 法 第一種方式,同時也是建議使用的方法,它可以對任何可能的 Shell 設定需要的語系及 MIME 字元集變數。此設定也可由每位使用者自行設定或者由超級管理者為所有使用者設定。 以下精簡範例示範在一個使用者的家目錄中的 [.filename]#.login_conf# 設定 Latin-1 編碼使用的兩個環境變數: [.programlisting] .... me:\ :charset=ISO-8859-1:\ :lang=de_DE.ISO8859-1: .... 以下使用者的 [.filename]#~/.login_conf# 範例設定了繁體中文於 BIG-5 編碼使用到的環境變數。有一部份應用程式無法正確處理中文、日文及韓文的語系變數,因此需要額外多做一些設定: [.programlisting] .... #Users who do not wish to use monetary units or time formats #of Taiwan can manually change each variable me:\ :lang=zh_TW.Big5:\ :setenv=LC_ALL=zh_TW.Big5,LC_COLLATE=zh_TW.Big5,LC_CTYPE=zh_TW.Big5,LC_MESSAGES=zh_TW.Big5,LC_MONETARY=zh_TW.Big5,LC_NUMERIC=zh_TW.Big5,LC_TIME=zh_TW.Big5:\ :charset=big5:\ :xmodifiers="@im=gcin": #Set gcin as the XIM Input Server .... 或者,超級使用者可以設定所有系統使用者的語系。以下在 [.filename]#/etc/login.conf# 中的變數可用來設定語系及 MIME 字元集: [.programlisting] .... language_name|Account Type Description:\ :charset=MIME_charset:\ :lang=locale_name:\ :tc=default: .... 若套用之前的 Latin-1 編碼範例如下: [.programlisting] .... german|German Users Accounts:\ :charset=ISO-8859-1:\ :lang=de_DE.ISO8859-1:\ :tc=default: .... 請參考 man:login.conf[5] 以取得更多有關這些變數的詳細資訊。請注意,它已經含有預先定義的 _russian_ class。 每次編輯 [.filename]#/etc/login.conf# 之後,請記得要執行以下指令來更新登入類別的能力資料庫(Capability database): [source,shell] .... # cap_mkdb /etc/login.conf .... ===== 變更登入類別的工具 除了手動編輯 [.filename]#/etc/login.conf# 之外,尚有需多工具可用來為新建立的使用者設定語系。 當使用 `vipw` 來新增使用者時,可指定 _language_ 來設定語系: [.programlisting] .... user:password:1111:11:language:0:0:User Name:/home/user:/bin/sh .... 當使用 `adduser` 來新增使用者時,可對所有使用者或指定的使用者事先設定預設的語言。 若所有新的使用者都使用同樣的語言,可在 [.filename]#/etc/adduser.conf# 設定 `defaultclass=_language_`。 要在建立使用者時覆蓋預設的設定,可在出現此提示時輸入需要的語系: [source,shell] .... Enter login class: default []: .... 或執行 `adduser` 時指定語系: [source,shell] .... # adduser -class language .... 若使用 `pw` 來新增使用者,則可指定語系如下: [source,shell] .... # pw useradd user_name -L language .... To change the login class of an existing user, `chpass` can be used. Invoke it as superuser and provide the username to edit as the argument. [source,shell] .... # chpass user_name .... [[startup-file]] ==== Shell 啟動檔 (Startup File) 法 第二種方法,較不建議使用,因每一種使用到的 Shell 都需要手動設定,而每一種 Shell 都有不同的設定檔以及語法。例如將一位使用者的 `sh` shell 設定為德語,需要將下列行加到 [.filename]#~/.profile#,若要設定給使用該 Shell 的所有使用者則必須將下列行加到 [.filename]#/etc/profile# 或 [.filename]#/usr/shared/skel/dot.profile#: [.programlisting] .... LANG=de_DE.ISO8859-1; export LANG MM_CHARSET=ISO-8859-1; export MM_CHARSET .... 然而,在 `csh` shell 所使用的設定檔名稱及語法不同。同樣的設定需加入下列行至 [.filename]#~/.csh.login#, [.filename]#/etc/csh.login# 或 [.filename]#/usr/shared/skel/dot.login#: [.programlisting] .... setenv LANG de_DE.ISO8859-1 setenv MM_CHARSET ISO-8859-1 .... 更複雜一點的情況,Xorg 的 [.filename]#~/.xinitrc# 語系設定會依使用的 Shell 而有所不同。第一個例子是針對 `sh` shell 而第二個則是針對 `csh` shell: [.programlisting] .... LANG=de_DE.ISO8859-1; export LANG .... [.programlisting] .... setenv LANG de_DE.ISO8859-1 .... [[setting-console]] === Console 設定 已有許多語系的字型可在 Console 使用,要查看可用的字型清單,可輸入 `ls /usr/shared/syscons/fonts`。要設定 Console 的字型,可在 [.filename]#/etc/rc.conf# 指定去掉 [.filename]#.fnt# 字尾的字型名稱 _font_name_: [.programlisting] .... font8x16=font_name font8x14=font_name font8x8=font_name .... 鍵盤對應表 (Keymap) 及螢幕對應表 (Screenmap) 用可加入下行到 [.filename]#/etc/rc.conf# 來設定: [.programlisting] .... scrnmap=screenmap_name keymap=keymap_name keychange="fkey_number sequence" .... 要查看可用的螢幕對應表,可輸入 `ls /usr/shared/syscons/scrnmaps`。在設定螢幕對應表 _screenmap_name_ 時請去掉 [.filename]#.scm# 字尾。在 VGA Adapter 的字型字元矩陣擴充位元 8 到位元 9 時會需要使用螢幕對應表與相關的字型對應來解決,因此若螢幕字型使用位元 8 的欄位,字母會移出虛擬繪圖區 (Pseudographics area)。 要查看可用的鍵盤對應表,可輸入 `ls /usr/shared/syscons/keymaps`。在設定鍵盤對應表 _keymap_name_ 時請去掉 [.filename]#.kbd# 字尾。若要不重開機測試鍵盤對應用可使用 man:kbdmap[1]。 `keychange` 項目用在當功能鍵序列無法定義在鍵盤對應表時,可設定對應選擇終對機類型的功能鍵。 接下來,在 [.filename]#/etc/ttys# 為所有虛擬終端機項目設定正確的 Console 終端機類型。<> 摘要了可用的終端機類型: [[locale-charset]] .已定義供特定字元集使用的終端機類型 [cols="1,1", frame="none", options="header"] |=== | 字元集 | 終端機類型 |ISO8859-1 or ISO8859-15 |`cons25l1` |ISO8859-2 |`cons25l2` |ISO8859-7 |`cons25l7` |KOI8-R |`cons25r` |KOI8-U |`cons25u` |CP437 (VGA 預設值) |`cons25` |US-ASCII |`cons25w` |=== 對於使用寬字元或多位元組字元的語言,需從 Port 套件集安裝支援該語言的 Console。 可用的 Port 摘要在 <>。安裝完成之後,請參考 Port 的 [.filename]#pkg-message# 或操作手冊來取得設定及使用說明。 [[locale-console]] .Port 套件集中可用的 Console [cols="1,1", frame="none", options="header"] |=== | 語言 | Port 位置 |繁體中文 (BIG-5) |package:chinese/big5con[] |中文/日文/韓文 |package:chinese/cce[] |中文/日文/韓文 |package:chinese/zhcon[] |日文 |package:chinese/kon2[] |日文 |package:japanese/kon2-14dot[] |日文 |package:japanese/kon2-16dot[] |=== 若在 [.filename]#/etc/rc.conf# 有開啟 moused,可能會需要額外的設定。預設 man:syscons[4] 驅動程式的滑鼠游標會佔用字元集 `0xd0`-`0xd3` 的範圍,若語言有使用到此範圍,可加入以下行到 [.filename]#/etc/rc.conf# 來移動游標的範圍: [.programlisting] .... mousechar_start=3 .... === Xorg 設定 crossref:x11[x11,X Window 系統] 會說明如何安裝並設定 Xorg。當要設定 Xorg 在地化時,可從 FreeBSD Port 套件集中取得其他可用的字型及輸入法。應用程式特定的 i18n 設定像是字型與選單,可以在 [.filename]#~/.Xresources# 中調校且可允許使用者在圖型化應用程式選單檢視其所選擇的語言。 X 輸入法 (X Input Method, XIM) 協定是 Xorg 針對輸入非英語字元的標準。<> 摘要了在 FreeBSD 套件集中可用的輸入法應用程式。也可使用其他如 Fcitx 及 Uim 應用程式。 [[locale-xim]] .可用的輸入法 [cols="1,1", frame="none", options="header"] |=== | 語言 | 輸入法 |中文 |package:chinese/gcin[] |中文 |package:chinese/ibus-chewing[] |中文 |package:chinese/ibus-pinyin[] |中文 |package:chinese/oxim[] |中文 |package:chinese/scim-fcitx[] |中文 |package:chinese/scim-pinyin[] |中文 |package:chinese/scim-tables[] |日文 |package:japanese/ibus-anthy[] |日文 |package:japanese/ibus-mozc[] |日文 |package:japanese/ibus-skk[] |日文 |package:japanese/im-ja[] |日文 |package:japanese/kinput2[] |日文 |package:japanese/scim-anthy[] |日文 |package:japanese/scim-canna[] |日文 |package:japanese/scim-honoka[] |日文 |package:japanese/scim-honoka-plugin-romkan[] |日文 |package:japanese/scim-honoka-plugin-wnn[] |日文 |package:japanese/scim-prime[] |日文 |package:japanese/scim-skk[] |日文 |package:japanese/scim-tables[] |日文 |package:japanese/scim-tomoe[] |日文 |package:japanese/scim-uim[] |日文 |package:japanese/skkinput[] |日文 |package:japanese/skkinput3[] |日文 |package:japanese/uim-anthy[] |韓文 |package:korean/ibus-hangul[] |韓文 |package:korean/imhangul[] |韓文 |package:korean/nabi[] |韓文 |package:korean/scim-hangul[] |韓文 |package:korean/scim-tables[] |越南文 |package:vietnamese/xvnkb[] |越南文 |package:vietnamese/x-unikey[] |=== [[l10n-compiling]] == 尋找 i18n 應用程式 i18n 應用程式會使用 i18n 工具包做為程式庫開發。這讓開發人員可以寫一個簡單的檔案並翻譯顯示的選單及文字至各種語言。 https://www.FreeBSD.org/ports/[FreeBSD Port 套件集]中含有許多內建支援寬字元或多位元組字元的應用程式可支援各種語言。該類型的應用程式在名稱上會註明 `i18n` 以易於辨識。雖然如此,但不一定支援您所需要的語言。 有一部份應用程式可以使用指定的字元集來編譯。通常會在 Port 的 [.filename]#Makefile# 中設定,或者傳送參數給 configure。請參考各 FreeBSD Port 原始碼中的 i18n 說明文件以取得更多有關需要的設定值資訊或 Port 的 [.filename]#Makefile# 來了解在編譯時有那些可以使用的編譯選項。 [[lang-setup]] == 特定語言的語系設定 This section provides configuration examples for localizing a FreeBSD system for the Russian language. It then provides some additional resources for localizing other languages. [[ru-localize]] === 俄語 (KOI8-R 編碼) This section shows the specific settings needed to localize a FreeBSD system for the Russian language. Refer to <> for a more complete description of each type of setting. To set this locale for the login shell, add the following lines to each user's [.filename]#~/.login_conf#: [.programlisting] .... me:My Account:\ :charset=KOI8-R:\ :lang=ru_RU.KOI8-R: .... To configure the console, add the following lines to [.filename]#/etc/rc.conf#: [.programlisting] .... keymap="ru.utf-8" scrnmap="utf-82cp866" font8x16="cp866b-8x16" font8x14="cp866-8x14" font8x8="cp866-8x8" mousechar_start=3 .... For each `ttyv` entry in [.filename]#/etc/ttys#, use `cons25r` as the terminal type. To configure printing, a special output filter is needed to convert from KOI8-R to CP866 since most printers with Russian characters come with hardware code page CP866. FreeBSD includes a default filter for this purpose, [.filename]#/usr/libexec/lpr/ru/koi2alt#. To use this filter, add this entry to [.filename]#/etc/printcap#: [.programlisting] .... lp|Russian local line printer:\ :sh:of=/usr/libexec/lpr/ru/koi2alt:\ :lp=/dev/lpt0:sd=/var/spool/output/lpd:lf=/var/log/lpd-errs: .... Refer to man:printcap[5] for a more detailed explanation. To configure support for Russian filenames in mounted MS-DOS(TM) file systems, include `-L` and the locale name when adding an entry to [.filename]#/etc/fstab#: [.programlisting] .... /dev/ad0s2 /dos/c msdos rw,-Lru_RU.KOI8-R 0 0 .... Refer to man:mount_msdosfs[8] for more details. To configure Russian fonts for Xorg, install the package:x11-fonts/xorg-fonts-cyrillic[] package. Then, check the `"Files"` section in [.filename]#/etc/X11/xorg.conf#. The following line must be added _before_ any other `FontPath` entries: [.programlisting] .... FontPath "/usr/local/lib/X11/fonts/cyrillic" .... Additional Cyrillic fonts are available in the Ports Collection. To activate a Russian keyboard, add the following to the `"Keyboard"` section of [.filename]#/etc/xorg.conf#: [.programlisting] .... Option "XkbLayout" "us,ru" Option "XkbOptions" "grp:toggle" .... Make sure that `XkbDisable` is commented out in that file. For `grp:toggle` use kbd:[Right Alt], for `grp:ctrl_shift_toggle` use kbd:[Ctrl+Shift]. For `grp:caps_toggle` use kbd:[CapsLock]. The old kbd:[CapsLock] function is still available in LAT mode only using kbd:[Shift+CapsLock]. `grp:caps_toggle` does not work in Xorg for some unknown reason. If the keyboard has "Windows(TM)" keys, and some non-alphabetical keys are mapped incorrectly, add the following line to [.filename]#/etc/xorg.conf#: [.programlisting] .... Option "XkbVariant" ",winkeys" .... [NOTE] ==== The Russian XKB keyboard may not work with non-localized applications. Minimally localized applications should call a `XtSetLanguageProc (NULL, NULL, NULL);` function early in the program. ==== See http://koi8.pp.ru/xwin.html[http://koi8.pp.ru/xwin.html] for more instructions on localizing Xorg applications. For more general information about KOI8-R encoding, refer to http://koi8.pp.ru/[http://koi8.pp.ru/]. === 其他特定語言資源 This section lists some additional resources for configuring other locales. Traditional Chinese for Taiwan:: The FreeBSD-Taiwan Project has a Chinese HOWTO for FreeBSD at http://netlab.cse.yzu.edu.tw/\~statue/freebsd/zh-tut/[http://netlab.cse.yzu.edu.tw/~statue/freebsd/zh-tut/]. Greek Language Localization:: A complete article on Greek support in FreeBSD is available https://www.FreeBSD.org/doc/el/articles/greek-language-support/[here], in Greek only, as part of the official FreeBSD Greek documentation. Japanese and Korean Language Localization:: For Japanese, refer to http://www.jp.FreeBSD.org/[http://www.jp.FreeBSD.org/], and for Korean, refer to http://www.kr.FreeBSD.org/[http://www.kr.FreeBSD.org/]. Non-English FreeBSD Documentation:: Some FreeBSD contributors have translated parts of the FreeBSD documentation to other languages. They are available through links on the https://www.FreeBSD.org/[FreeBSD web site] or in [.filename]#/usr/shared/doc#.