> 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.1-jie-ben-di-hua-huan-jing-bian-liang.md).

# 15.1 本地化环境变量

本地化环境变量（`LANG`、`LC_*`、`MM_CHARSET`）决定系统界面语言、字符编码与排序规则，本节阐述各场景下的配置方法。

## 本地化环境变量配置文件路径

### 显示管理器配置路径

| 显示管理器            | 配置文件路径           |
| ---------------- | ---------------- |
| SDDM、LightDM、GDM | **\~/.xprofile** |
| LightDM、GDM      | **\~/.profile**  |
| SDDM             | 用户登录 Shell 的配置文件 |

### Shell 配置路径

| Shell | 配置文件路径                                  |
| ----- | --------------------------------------- |
| sh    | **\~/.profile**                         |
| bash  | **\~/.bash\_profile** 或 **\~/.profile** |
| zsh   | **\~/.zprofile**                        |
| csh   | **\~/.cshrc**                           |

## 本地化相关的环境变量

`LC_*` 系列变量是 UNIX 及类 UNIX 操作系统中用于实现国际化（Internationalization，i18n）与本地化（Localization，l10n）的核心环境变量。`LC_COLLATE`、`LC_CTYPE`、`LC_MESSAGES`、`LC_MONETARY`、`LC_NUMERIC`、`LC_TIME` 以及 `LC_ALL` 和 `LANG` 由 POSIX（IEEE Std 1003.1，即 ISO/IEC 9945）标准定义。`LC_ADDRESS`、`LC_NAME`、`LC_PAPER`、`LC_TELEPHONE`、`LC_MEASUREMENT`、`LC_IDENTIFICATION` 为 glibc 的 GNU 扩展（自 glibc 2.2 起引入），ISO/IEC TR 30112:2014（技术报告，已 withdrawn，后由 ISO/IEC 30112:2020 国际标准替代）制定时参考了这些既有实践（参见 Debian manpages. locale(7)\[EB/OL]. \[2026-04-17]. <https://manpages.debian.org/unstable/manpages/locale.7.en.html>）。这些变量控制文本字符编码、日期时间格式、货币符号、界面语言等多个维度的本地化行为。

| 变量                  | 说明                                        |
| ------------------- | ----------------------------------------- |
| `LC_COLLATE`        | 定义字符串排序的规则                                |
| `LC_CTYPE`          | 定义字符集和字符类型判断规则，例如字母、数字、标点符号等              |
| `LC_MONETARY`       | 定义货币格式和货币符号                               |
| `LC_MESSAGES`       | 定义程序运行时输出信息的语言                            |
| `LC_NUMERIC`        | 定义数字格式，例如小数点和千位分隔符                        |
| `LC_TIME`           | 定义日期和时间格式                                 |
| `LC_ADDRESS`        | 定义地址的格式                                   |
| `LC_NAME`           | 定义人名的格式                                   |
| `LC_PAPER`          | 定义默认纸张尺寸                                  |
| `LC_TELEPHONE`      | 定义电话号码的格式                                 |
| `LC_MEASUREMENT`    | 定义度量衡单位的格式（公制或英制）                         |
| `LC_IDENTIFICATION` | 定义 locale 自身的元数据（locale 名称、语言、地域、货币等标识信息） |
| `MM_CHARSET`        | 设置 MIME 字符集，供应用程序在处理多语言内容时确定字符编码          |

### 特殊变量说明

| 变量         | 说明                                                                                                                                                                                                                                                               |
| ---------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `LC_ALL`   | 设置该变量将同时覆盖所有其他 `LC_*` 变量的值                                                                                                                                                                                                                                       |
| `LANG`     | 用于设置默认的语言和字符集，通常在没有其他 `LC_*` 变量设置时提供区域设置信息。如果同时设置了 `LANG` 和 `LC_*` 变量，`LC_*` 变量将覆盖 `LANG` 变量中相应的设置                                                                                                                                                               |
| `LANGUAGE` | 主要用于为 GNU gettext 等本地化库指定界面消息（例如命令行提示、错误信息、菜单文本等）的首选语言。GNU gettext 对 `LANGUAGE` 的优先级高于 `LC_ALL` 和 `LANG`。它一般不会影响日期、数字、货币等格式，这些格式类本地化仍由相应的 `LC_TIME`、`LC_NUMERIC`、`LC_MONETARY` 等 `LC_*` 变量或 `LANG` 控制。如果未设置 `LANGUAGE`，程序通常会回退到 `LC_MESSAGES` 或其他区域设置变量来确定界面消息语言 |

#### 参考文献

* GNU gettext Manual. The LANGUAGE variable\[EB/OL]. \[2026-06-08]. <https://www.gnu.org/software/gettext/manual/html_node/The-LANGUAGE-variable.html>.

### 常见配置方案

可通过多种方式实现中文环境：

1. 仅设置 `LC_MESSAGES` 为 `zh_CN.UTF-8` 即可实现中文界面（已在 SDDM/Xfce 环境下验证）
2. 常见做法是将 `LANG`、`LC_ALL`、`LANGUAGE` 三个环境变量均设置为 `zh_CN.UTF-8`
3. 在系统界面保持英文的同时使用中文输入法，仅设置输入法相关变量，其余环境变量保持默认英文设置

不同软件读取本地化变量时的优先级存在差异，因此建议将三个环境变量均设置为 `zh_CN.UTF-8`，以确保语言设置的一致性。

### 不同配置的效果不同

仅设置 `LC_MESSAGES` 时，该设置仅影响界面和提示信息，不影响其他格式的输出。以 sh 为例：

```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=zh_CN.UTF-8
LC_ALL=
$ date	# 显示时间和日期
Fri Apr 21 21:14:43 UTC 2023
$ export LC_TIME=zh_CN.UTF-8
$ date
2023年 4月21日 星期五 21时15分07秒 UTC
```

默认：

* `LC_TIME` 环境变量值为 `C.UTF-8`
* `date` 命令输出 `Fri Apr 21 21:14:43 UTC 2023`
* `LC_TIME` 环境变量的值设置为 `zh_CN.UTF-8`
* `date` 命令输出为 `2023年 4月21日 星期五 21时15分07秒 UTC`

> **注意**
>
> 保持 `date` 命令的英文输出在某些脚本中可能非常重要。其他由 `LC_*` 变量控制的信息也存在类似情况。

## 课后习题

1. 在 FreeBSD 系统中配置不同 Shell（sh、bash、zsh、csh）的本地化环境变量，分别验证 `date` 命令在英文和中文环境下的输出。
2. 构建一个仅设置 `LC_MESSAGES=zh_CN.UTF-8` 的环境，测试常用命令（如 ls、pkg、man）的中文提示可用性。
3. 分析 `LANG`、`LC_ALL` 和各 `LC_*` 变量之间的优先级关系，举例说明在不同设置组合下的实际行为。


---

# 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.1-jie-ben-di-hua-huan-jing-bian-liang.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.
