章 22. 在地化 - i18n/L10n 使用與安裝

This translation may be out of date. To help with the translations please access the FreeBSD translations instance.

22.1. 概述

FreeBSD 計劃的使用者及貢獻者分佈在世界各地,也因此 FreeBSD 支援多語系,讓使用者可以使用非英文語言來檢視、輸入或處理資料。使用者可以選擇大多數主要語言,包含但不限於以下語言:中文、德文、日文、韓文、法文、俄文及越南文。

國際化 (Internationalization) 一詞可以縮寫為 i18n,即 第一個字母到最後一個字母間的字母數量。L10n 也使用同樣的命名規則,但源自 在地化 (Localization)。 i18n/L10n 的方法、協定及應用程式讓使用者可以自己選擇使用的語言。

本章會討論 FreeBSD 的國際化及在地化功能。在閱讀本章之後,您會了解:

  • 語系名稱如何組成。

  • 如何設定登入 Shell 的語系。

  • 如何設定 Console 給非英文語言的使用者。

  • 如果設定 Xorg 使用不同語言。

  • 如何找到支援 i18n 的應用程式。

  • 那裡可以找到更多設定特定語言的資訊。

在開始閱讀這章之前,您需要:

22.2. 使用語系

語系設定值由三個元件所組成:語言代號、城市代號及編碼。語系名稱組成的方式如下:

LanguageCode_CountryCode.Encoding

LanguageCodeCountryCode 用來表示城市及特定語言。常用語言及城市代碼 提供了幾個 LanguageCode_CountryCode 的範例:

表 1. 常用語言及城市代碼
語言代號_城市代號說明

en_US

英文,美國

ru_RU

俄文,俄國

zh_TW

繁體中文,台灣

完整可用的語系清單可用以下指令查詢:

% locale -a | more

查詢目前使用的語系設定:

% locale

語言特定的字元集如 ISO8859-1, ISO8859-15, KOI8-R 及 CP437 在 multibyte(3) 有詳細說明。可用的字元集可在 IANA Registry 查詢。

某些語言,如中文或日文,無法使用 ASCII 字元表示,會需要使用寬 (Wide) 字元或多位元組 (Multibyte) 字元來擴充的語言編碼。EUC 與 Big5 即是使用寬子元或多位元組字元的例子。舊的應用程式會誤判這些字元為控制字元,新的應用程式則通常可以辨識這些字元,依實作的需要,使用者可能需要開啟寬字元或多位元組字元支援或者使用正確的字元設定來編譯應用程式。

FreeBSD 使用 Xorg 相容的語系編碼。

本節剩餘的部份將說明各種在 FreeBSD 系統上設定語系的方法。下一節將會探討如何尋找以及編譯使用 i18n 支援的應用程式。

22.2.1. 設定登入 Shell 的語系

語系設定可在使用者的 ~/.login_conf 或使用者的 Shell 的啟動檔設定:~/.profile, ~/.bashrc~/.cshrc

有兩個環境變數需要設定:

  • LANG 用來設定語系

  • MM_CHARSET 用來設定應用程式所使用的 MIME 字元集

除了使用者的 Shell 設定外,這些變數也應針對特定應用程式設定以及 Xorg 設定。

兩種可以完成所需變數設定的方法有:登入類別 (Login class) 法 (較建議) 及 啟動檔 (Startup file) 法。以下兩節將示範如何使用這兩個方法。

22.2.1.1. 登入類別 (Login Class) 法

第一種方式,同時也是建議使用的方法,它可以對任何可能的 Shell 設定需要的語系及 MIME 字元集變數。此設定也可由每位使用者自行設定或者由超級管理者為所有使用者設定。

以下精簡範例示範在一個使用者的家目錄中的 .login_conf 設定 Latin-1 編碼使用的兩個環境變數:

me:\
	:charset=ISO-8859-1:\
	:lang=de_DE.ISO8859-1:

以下使用者的 ~/.login_conf 範例設定了繁體中文於 BIG-5 編碼使用到的環境變數。有一部份應用程式無法正確處理中文、日文及韓文的語系變數,因此需要額外多做一些設定:

#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

或者,超級使用者可以設定所有系統使用者的語系。以下在 /etc/login.conf 中的變數可用來設定語系及 MIME 字元集:

language_name|Account Type Description:\
	:charset=MIME_charset:\
	:lang=locale_name:\
	:tc=default:

若套用之前的 Latin-1 編碼範例如下:

german|German Users Accounts:\
	:charset=ISO-8859-1:\
	:lang=de_DE.ISO8859-1:\
	:tc=default:

請參考 login.conf(5) 以取得更多有關這些變數的詳細資訊。請注意,它已經含有預先定義的 russian class。

每次編輯 /etc/login.conf 之後,請記得要執行以下指令來更新登入類別的能力資料庫(Capability database):

# cap_mkdb /etc/login.conf
22.2.1.1.1. 變更登入類別的工具

除了手動編輯 /etc/login.conf 之外,尚有需多工具可用來為新建立的使用者設定語系。

當使用 vipw 來新增使用者時,可指定 language 來設定語系:

user:password:1111:11:language:0:0:User Name:/home/user:/bin/sh

當使用 adduser 來新增使用者時,可對所有使用者或指定的使用者事先設定預設的語言。

若所有新的使用者都使用同樣的語言,可在 /etc/adduser.conf 設定 defaultclass=language

要在建立使用者時覆蓋預設的設定,可在出現此提示時輸入需要的語系:

Enter login class: default []:

或執行 adduser 時指定語系:

# adduser -class language

若使用 pw 來新增使用者,則可指定語系如下:

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

# chpass user_name

22.2.1.2. Shell 啟動檔 (Startup File) 法

第二種方法,較不建議使用,因每一種使用到的 Shell 都需要手動設定,而每一種 Shell 都有不同的設定檔以及語法。例如將一位使用者的 sh shell 設定為德語,需要將下列行加到 ~/.profile,若要設定給使用該 Shell 的所有使用者則必須將下列行加到 /etc/profile/usr/shared/skel/dot.profile

LANG=de_DE.ISO8859-1; export LANG
MM_CHARSET=ISO-8859-1; export MM_CHARSET

然而,在 csh shell 所使用的設定檔名稱及語法不同。同樣的設定需加入下列行至 ~/.csh.login, /etc/csh.login/usr/shared/skel/dot.login

setenv LANG de_DE.ISO8859-1
setenv MM_CHARSET ISO-8859-1

更複雜一點的情況,Xorg 的 ~/.xinitrc 語系設定會依使用的 Shell 而有所不同。第一個例子是針對 sh shell 而第二個則是針對 csh shell:

LANG=de_DE.ISO8859-1; export LANG
setenv LANG de_DE.ISO8859-1

22.2.2. Console 設定

已有許多語系的字型可在 Console 使用,要查看可用的字型清單,可輸入 ls /usr/shared/syscons/fonts。要設定 Console 的字型,可在 /etc/rc.conf 指定去掉 .fnt 字尾的字型名稱 font_name

font8x16=font_name
font8x14=font_name
font8x8=font_name

鍵盤對應表 (Keymap) 及螢幕對應表 (Screenmap) 用可加入下行到 /etc/rc.conf 來設定:

scrnmap=screenmap_name
keymap=keymap_name
keychange="fkey_number sequence"

要查看可用的螢幕對應表,可輸入 ls /usr/shared/syscons/scrnmaps。在設定螢幕對應表 screenmap_name 時請去掉 .scm 字尾。在 VGA Adapter 的字型字元矩陣擴充位元 8 到位元 9 時會需要使用螢幕對應表與相關的字型對應來解決,因此若螢幕字型使用位元 8 的欄位,字母會移出虛擬繪圖區 (Pseudographics area)。

要查看可用的鍵盤對應表,可輸入 ls /usr/shared/syscons/keymaps。在設定鍵盤對應表 keymap_name 時請去掉 .kbd 字尾。若要不重開機測試鍵盤對應用可使用 kbdmap(1)

keychange 項目用在當功能鍵序列無法定義在鍵盤對應表時,可設定對應選擇終對機類型的功能鍵。

接下來,在 /etc/ttys 為所有虛擬終端機項目設定正確的 Console 終端機類型。已定義供特定字元集使用的終端機類型 摘要了可用的終端機類型:

表 2. 已定義供特定字元集使用的終端機類型
字元集終端機類型

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 套件集中可用的 Console。安裝完成之後,請參考 Port 的 pkg-message 或操作手冊來取得設定及使用說明。

表 3. Port 套件集中可用的 Console
語言Port 位置

繁體中文 (BIG-5)

chinese/big5con

中文/日文/韓文

chinese/cce

中文/日文/韓文

chinese/zhcon

日文

chinese/kon2

日文

japanese/kon2-14dot

日文

japanese/kon2-16dot

若在 /etc/rc.conf 有開啟 moused,可能會需要額外的設定。預設 syscons(4) 驅動程式的滑鼠游標會佔用字元集 0xd0-0xd3 的範圍,若語言有使用到此範圍,可加入以下行到 /etc/rc.conf 來移動游標的範圍:

mousechar_start=3

22.2.3. Xorg 設定

X Window 系統 會說明如何安裝並設定 Xorg。當要設定 Xorg 在地化時,可從 FreeBSD Port 套件集中取得其他可用的字型及輸入法。應用程式特定的 i18n 設定像是字型與選單,可以在 ~/.Xresources 中調校且可允許使用者在圖型化應用程式選單檢視其所選擇的語言。

X 輸入法 (X Input Method, XIM) 協定是 Xorg 針對輸入非英語字元的標準。可用的輸入法 摘要了在 FreeBSD 套件集中可用的輸入法應用程式。也可使用其他如 Fcitx 及 Uim 應用程式。

表 4. 可用的輸入法
語言輸入法

中文

chinese/gcin

中文

chinese/ibus-chewing

中文

chinese/ibus-pinyin

中文

chinese/oxim

中文

chinese/scim-fcitx

中文

chinese/scim-pinyin

中文

chinese/scim-tables

日文

japanese/ibus-anthy

日文

japanese/ibus-mozc

日文

japanese/ibus-skk

日文

japanese/im-ja

日文

japanese/kinput2

日文

japanese/scim-anthy

日文

japanese/scim-canna

日文

japanese/scim-honoka

日文

japanese/scim-honoka-plugin-romkan

日文

japanese/scim-honoka-plugin-wnn

日文

japanese/scim-prime

日文

japanese/scim-skk

日文

japanese/scim-tables

日文

japanese/scim-tomoe

日文

japanese/scim-uim

日文

japanese/skkinput

日文

japanese/skkinput3

日文

japanese/uim-anthy

韓文

korean/ibus-hangul

韓文

korean/imhangul

韓文

korean/nabi

韓文

korean/scim-hangul

韓文

korean/scim-tables

越南文

vietnamese/xvnkb

越南文

vietnamese/x-unikey

22.3. 尋找 i18n 應用程式

i18n 應用程式會使用 i18n 工具包做為程式庫開發。這讓開發人員可以寫一個簡單的檔案並翻譯顯示的選單及文字至各種語言。

FreeBSD Port 套件集中含有許多內建支援寬字元或多位元組字元的應用程式可支援各種語言。該類型的應用程式在名稱上會註明 i18n 以易於辨識。雖然如此,但不一定支援您所需要的語言。

有一部份應用程式可以使用指定的字元集來編譯。通常會在 Port 的 Makefile 中設定,或者傳送參數給 configure。請參考各 FreeBSD Port 原始碼中的 i18n 說明文件以取得更多有關需要的設定值資訊或 Port 的 Makefile 來了解在編譯時有那些可以使用的編譯選項。

22.4. 特定語言的語系設定

This section provides configuration examples for localizing a FreeBSD system for the Russian language. It then provides some additional resources for localizing other languages.

22.4.1. 俄語 (KOI8-R 編碼)

This section shows the specific settings needed to localize a FreeBSD system for the Russian language. Refer to Using Localization 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 ~/.login_conf:

me:My Account:\
	:charset=KOI8-R:\
	:lang=ru_RU.KOI8-R:

To configure the console, add the following lines to /etc/rc.conf:

keymap="ru.utf-8"
scrnmap="utf-82cp866"
font8x16="cp866b-8x16"
font8x14="cp866-8x14"
font8x8="cp866-8x8"
mousechar_start=3

For each ttyv entry in /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, /usr/libexec/lpr/ru/koi2alt. To use this filter, add this entry to /etc/printcap:

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 printcap(5) for a more detailed explanation.

To configure support for Russian filenames in mounted MS-DOS™ file systems, include -L and the locale name when adding an entry to /etc/fstab:

/dev/ad0s2      /dos/c  msdos   rw,-Lru_RU.KOI8-R 0 0

Refer to mount_msdosfs(8) for more details.

To configure Russian fonts for Xorg, install the x11-fonts/xorg-fonts-cyrillic package. Then, check the "Files" section in /etc/X11/xorg.conf. The following line must be added before any other FontPath entries:

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 /etc/xorg.conf:

Option "XkbLayout"   "us,ru"
Option "XkbOptions"  "grp:toggle"

Make sure that XkbDisable is commented out in that file.

For grp:toggle use Right Alt, for grp:ctrl_shift_toggle use Ctrl+Shift. For grp:caps_toggle use CapsLock. The old CapsLock function is still available in LAT mode only using Shift+CapsLock. grp:caps_toggle does not work in Xorg for some unknown reason.

If the keyboard has "Windows™" keys, and some non-alphabetical keys are mapped incorrectly, add the following line to /etc/xorg.conf:

Option "XkbVariant" ",winkeys"

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 for more instructions on localizing Xorg applications. For more general information about KOI8-R encoding, refer to http://koi8.pp.ru/.

22.4.2. 其他特定語言資源

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

Greek Language Localization

A complete article on Greek support in FreeBSD is available 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/, and for Korean, refer to 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 FreeBSD web site or in /usr/shared/doc.


最後修改於: December 11, 2021 由 Sergio Carlavilla Delgado