> For the complete documentation index, see [llms.txt](https://book.bsdcn.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://book.bsdcn.org/di-15-zhang-ben-di-hua-yu-shu-ru-fa/di-15.2-jie-te-ding-yu-yan-de-qu-yu-pei-zhi.md).

# 15.2 特定语言的区域配置

本节阐述在 FreeBSD 系统中配置区域设置的多种方法。

## 使用本地化

本地化设置基于三个组件：语言代码、区域代码和编码。区域设置名称由这些部分构成，如下所示：

```sh
语言代码（小写）_区域代码（大写）.编码
```

**语言代码** 和 **区域代码** 用于确定区域和特定的语言变体。以下是一些 **语言代码\_区域代码** 的示例：

**常见语言和区域代码**

| 语言代码\_区域代码\_编码 | 说明         |
| -------------- | ---------- |
| en\_US.UTF-8   | 美国（英语）     |
| zh\_CN.UTF-8   | 中国（简体中文）   |
| zh\_TW\.UTF-8  | 中国台湾（繁体中文） |
| zh\_HK.UTF-8   | 中国香港（繁体中文） |

键入以下命令可查看完整的可用区域设置列表：

```sh
$ locale -a | more
C
C.UTF-8
POSIX
af_ZA.ISO8859-1
af_ZA.ISO8859-15
af_ZA.UTF-8
am_ET.UTF-8
ar_AE.UTF-8
ar_EG.UTF-8
ar_JO.UTF-8
ar_MA.UTF-8
ar_QA.UTF-8
ar_SA.UTF-8
be_BY.CP1131
be_BY.CP1251
be_BY.ISO8859-5
be_BY.UTF-8
bg_BG.CP1251
bg_BG.UTF-8
ca_AD.ISO8859-1
ca_AD.ISO8859-15
ca_AD.UTF-8
ca_ES.ISO8859-1
ca_ES.ISO8859-15
ca_ES.UTF-8
--More--(byte 314) # 此处按回车键可继续浏览，按 q 键可退出
```

使用 `locale` 命令查看当前系统的本地化设置：

```sh
$ locale
LANG=C.UTF-8
LC_CTYPE="C.UTF-8"
LC_COLLATE="C.UTF-8"
LC_TIME="C.UTF-8"
LC_NUMERIC="C.UTF-8"
LC_MONETARY="C.UTF-8"
LC_MESSAGES="C.UTF-8"
LC_ALL=
```

中文或日语等语言无法用 ASCII 字符表示，需要使用宽字符或多字节字符的扩展语言编码，建议使用 **UTF-8**。

> **注意**
>
> FreeBSD 使用的区域编码与 Xorg 兼容。

## 设置登录 shell 的区域设置

区域设置可以在用户的 **\~/.login\_conf** 文件中或用户 shell 的启动文件中配置：**\~/.profile**、**\~/.bashrc** 或 **\~/.cshrc**。

需要设置两个环境变量：

* `LANG`：设置区域设置
* `MM_CHARSET`：设置应用程序使用的 MIME 字符集

除了用户的 shell 配置外，这些变量还需在特定应用程序和 Xorg 的配置中设置。

有两种方法可以设置所需的变量：推荐的方法是通过登录分级，另一种是通过启动文件。

### 登录分级方法

这是推荐的方法，它为每种 shell 设置所需的区域设置和 MIME 字符集环境变量。此设置可由每位用户单独完成，也可由超级用户为所有用户统一配置。

以下是一个最简单的示例，在个人用户的 **\~/.login\_conf** 中为简体中文（UTF-8 编码）设置两个变量：

```ini
me:\
	:charset=UTF-8:\
	:lang=zh_CN.UTF-8:
```

以下是一个用户的 **\~/.login\_conf** 示例，为简体中文 UTF-8 编码设置变量。一些应用程序未能正确处理中文、日文和韩文的区域设置变量，因此需要更多变量以确保其正常运行：

```ini
# 如不使用中国大陆的货币与时间格式，可手动调整对应变量
me:\
	:lang=zh_CN.UTF-8:\
	:setenv=LC_ALL=zh_CN.UTF-8:\
	:charset=UTF-8:
```

> **注意**
>
> 此 `setenv` 与 csh 无关。

另外，超级用户可以为系统上的所有用户配置本地化设置。以下变量可在 **/etc/login.conf** 中配置，用于设置区域和 MIME 字符集：

```ini
语言|账户说明:\
	:charset=MIME 字符集:\
	:lang=区域:\
	:tc=default: # 继承其他默认选项
```

例如，之前提到的简体中文示例在 **/etc/login.conf** 中可配置如下：

```sh
chinese|chinese Users Accounts:\
	:charset=UTF-8:\
	:lang=zh_CN.UTF-8:\
	:tc=default:
```

> **技巧**
>
> 该文件包含了预定义的 *russian* 分级文件。

每次编辑 **/etc/login.conf** 后，需要执行以下命令以重新生成登录能力数据库：

```sh
# cap_mkdb /etc/login.conf
```

> **注意**
>
> 最终用户需要在 **\~/.login\_conf** 上运行 `cap_mkdb` 命令才能使更改生效。

#### 更改登录分级的工具

除了手动编辑 **/etc/login.conf** 外，还可以使用一些工具为新创建的用户设置区域设置。

使用 `vipw` 添加新用户时，可以指定 **chinese** 分级将区域设置为简体中文（UTF-8 编码）：

```ini
用户名:密码:1111:11:chinese:0:0:User Name:/home/user:/bin/sh
```

例如要为用户 ykla 分配 **chinese** 分级，目前未分配任何自定义用户分级：

```ini
ykla:$6$SqMJXrv5aC6Wq.by$nmbZs078aHNBVyh9noLFouJsGHyFSvQIzH0W4zpdfXuPtGtt.FHgWfXDHVBa.g9P
0eZ32UwfByzRKdVnTaO7W.:1001:1001::0:0:User &:/home/ykla:/bin/sh
```

分配后，则为：

```ini
ykla:$6$SqMJXrv5aC6Wq.by$nmbZs078aHNBVyh9noLFouJsGHyFSvQIzH0W4zpdfXuPtGtt.FHgWfXDHVBa.g9P
0eZ32UwfByzRKdVnTaO7W.:1001:1001:chinese:0:0:User &:/home/ykla:/bin/sh
```

使用 `adduser` 添加新用户时，可以为所有新用户预配置默认语言，或为单个用户指定语言。

若所有新用户使用相同的语言（假设为简体中文），可以在 **/etc/adduser.conf** 中设置 `defaultclass=chinese`。

创建用户时如需覆盖此设置，可在以下提示符处输入所需的登录类别：

```sh
Login class [default]:
```

或者在调用 `adduser` 时指定区域设置：

```sh
# adduser -class chinese
Username:
```

如果使用 `pw` 添加新用户 test，则按以下方式指定区域设置：

```sh
# pw useradd test -L chinese
```

要更改既有用户的登录分级，可以使用 `chpass` 或者 `pw usermod`。以 root 调用并提供要编辑的用户名作为参数：

```sh
# chpass ykla
```

```sh
# pw usermod ykla -L chinese
```

验证修改结果：

```sh
# pw usershow ykla
ykla:$6$SqMJXrv5aC6Wq.by$nmbZs078aHNBVyh9noLFouJsGHyFSvQIzH0W4zpdfXuPtGtt.FHgWfXDHVBa.g9P0eZ32UwfByzRKdVnTaO7W.:1001:1001:chinese:0:0:User &:/home/ykla:/bin/sh
```

### Shell 启动文件方法

这种方法不推荐使用，因为每种 shell 都需要手动配置，每个 shell 配置文件和语法各异。例如，要为 `sh` shell 设置简体中文，可以将以下行添加到 **\~/.profile** 文件中，仅为该用户设置 shell。也可以将这些行添加到 **/etc/profile** 或 **/usr/share/skel/dot.profile** 中，为所有用户设置该 shell：

```sh
LANG=zh_CN.UTF-8; export LANG
MM_CHARSET=UTF-8; export MM_CHARSET
```

但 `csh` shell 的配置文件名称和语法不同。以下是在 **\~/.login**、**/etc/csh.login** 或 **/usr/share/skel/dot.login** 中的等效设置：

```sh
setenv LANG zh_CN.UTF-8
setenv MM_CHARSET UTF-8
```

配置 Xorg 的语法也取决于所使用的 shell，情况更为复杂。在 **\~/.xinitrc** 中，`sh` shell 和 `csh` shell 的配置示例如下：

```sh
LANG=zh_CN.UTF-8; export LANG
```

```sh
setenv LANG zh_CN.UTF-8
```

## 控制台设置

VT 控制台默认使用 `xterm` 作为终端类型。

### 字体

FreeBSD VT 控制台原生支持 CJK 字符集（中日韩统一表意文字），加载字体即可显示中文。所需字体格式为 `.fnt`：这是一种二进制字体文件，而非码表加 PNG 图片的集合。

使用命令切换控制台字体为 **test.fnt**（仅当前会话生效，重启后恢复默认字体）：

```sh
$ vidcontrol -f test.fnt
```

FreeBSD 基本系统提供了一款工具，可将 bdf 或 hex 格式转换为 fnt 文件，其中 `-o` 为必选参数：

```sh
$ vtfontcvt [ -h 高度 ] [ -v ] [ -w 宽度 ] -o 输出文件.fnt 字体路径
```

中文字体示例：

![未加入字体的 tty](/files/k7lXAgh6HmulfWQXgItC)

```sh
# 下载 b16 字体文件
fetch https://people.freebsd.org/~emaste/newcons/b16.fnt

# 切换控制台字体为 b16
vidcontrol -f b16.fnt
```

> **技巧**
>
> 如果上述链接失效，请访问 <https://github.com/FreeBSD-Ask/fnt-fonts> 下载字体。

![设置完成后的 tty](/files/cWGatiJF15xPsZwPJehC)

上述命令仅临时生效，如需永久生效，应加入 **/etc/rc.conf** 文件：

```ini
# 设置控制台所有屏幕使用 b16 字体
allscreens_flags="-f /root/b16.fnt"
```

## 键盘布局

键盘布局文件位于 **/usr/share/vt/keymaps/**：

```sh
INDEX.keymaps			il.kbd
am.kbd				is.acc.kbd
be.acc.kbd			is.kbd
be.kbd				it.kbd

……省略一部分输出……

gr.101.acc.kbd			us.emacs.kbd
gr.elot.acc.kbd			us.intl.acc.kbd
gr.kbd				us.kbd
hr.kbd				us.macbook.kbd
hu.101.kbd			us.unix.kbd
hu.102.kbd
```

例如要将当前的布局改为国际美式键盘，在 **/etc/rc.conf** 文件中加入：

```ini
keymap="us.intl.acc"
```

> **技巧**
>
> 在指定 **键盘布局** 时，请勿包含后缀名 **.kbd**。

重启后生效。

要在不重启的情况下测试键盘布局，可以使用 `kbdmap` 命令交互式选择键盘布局：

![使用 kbdmap 选择键盘布局](/files/DnC3AC4NO8Lr6yoD11PN)

## 输入法

**可用输入法** 总结了 FreeBSD Ports 中可用的输入法应用程序。

**Fcitx 5 可用输入法表**

| 语言        | 输入法                                                                                        |
| --------- | ------------------------------------------------------------------------------------------ |
| 简体中文（拼音）  | [chinese/fcitx5-chinese-addons](https://www.freshports.org/chinese/fcitx5-chinese-addons/) |
| 繁体中文（注音）  | [chinese/fcitx5-chewing](https://www.freshports.org/chinese/fcitx5-chewing/)               |
| 繁体中文（注音）  | [chinese/fcitx5-mcbopomofo](https://www.freshports.org/chinese/fcitx5-mcbopomofo/)         |
| 中文（RIME）  | [chinese/fcitx5-rime](https://www.freshports.org/chinese/fcitx5-rime/)                     |
| 中文（形码输入法） | [chinese/fcitx5-table-extra](https://www.freshports.org/chinese/fcitx5-table-extra/)       |
| 日语（Anthy） | [japanese/fcitx5-anthy](https://www.freshports.org/japanese/fcitx5-anthy/)                 |
| 日语（SKK）   | [japanese/fcitx5-skk](https://www.freshports.org/japanese/fcitx5-skk/)                     |
| 日语（SKK）   | [japanese/fcitx5-cskk](https://www.freshports.org/japanese/fcitx5-cskk/)                   |
| 朝鲜语       | [korean/fcitx5-hangul](https://www.freshports.org/korean/fcitx5-hangul/)                   |
| 非中文输入法码表  | [chinese/fcitx5-table-other](https://www.freshports.org/chinese/fcitx5-table-other/)       |

**IBus 可用输入法表**

| 语言        | 输入法                                                                          |
| --------- | ---------------------------------------------------------------------------- |
| 简体中文（拼音）  | [chinese/ibus-libpinyin](https://www.freshports.org/chinese/ibus-libpinyin/) |
| 繁体中文（注音）  | [chinese/ibus-chewing](https://www.freshports.org/chinese/ibus-chewing/)     |
| 中文（RIME）  | [chinese/ibus-rime](https://www.freshports.org/chinese/ibus-rime/)           |
| 日语（Anthy） | [japanese/ibus-anthy](https://www.freshports.org/japanese/ibus-anthy/)       |
| 日语（Mozc）  | [japanese/ibus-mozc](https://www.freshports.org/japanese/ibus-mozc/)         |
| 日语（SKK）   | [japanese/ibus-skk](https://www.freshports.org/japanese/ibus-skk/)           |
| 朝鲜语       | [korean/ibus-hangul](https://www.freshports.org/korean/ibus-hangul/)         |

## 故障排除与未竟事宜

### 如何手动生成中文字体的 fnt 文件

<https://github.com/usonianhorizon/vt-fnt> 提供的方法较为复杂，可生成 bdf 文件，但会出现与文中相同的错误提示。该项目探索了 FreeBSD 控制台字体的生成方法。文中提及的 FontForge 软件提供 Windows 版本，下载地址为 <https://fontforge.org/en-US/downloads/windows-dl/>。

## 参考文献

* FreeBSD Project. rc.conf\[EB/OL]. \[2026-03-25]. <https://man.freebsd.org/cgi/man.cgi?query=rc.conf&sektion=5>. 该手册页详细说明了 rc.conf 系统配置文件的语法与选项。
* Mariusz. vidcontrol font and color via /etc/rc.conf problem\[EB/OL]. \[2026-03-25]. <https://forums.freebsd.org/threads/vidcontrol-font-and-color-via-etc-rc-conf-problem.81696/>. 该讨论帖探讨了控制台字体与颜色配置的相关问题。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://book.bsdcn.org/di-15-zhang-ben-di-hua-yu-shu-ru-fa/di-15.2-jie-te-ding-yu-yan-de-qu-yu-pei-zhi.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
