# Linux 的 locale

**URL:** <https://www.isharkfly.com/t/linux-locale/16952>\
**Category:** DevOps\
**Tags:** linux\
**Created:** [February 18, 2025, 5:11am UTC](https://www.isharkfly.com/t/linux-locale/16952 "2025-02-18T05:11:17Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![hex](https://www.isharkfly.com/user_avatar/www.isharkfly.com/hex/32/21953_2.png) [@hex](https://www.isharkfly.com/u/hex)\
**Post date:** [February 18, 2025, 5:11am UTC](https://www.isharkfly.com/t/linux-locale/16952/1 "2025-02-18T05:11:17Z")

</div>

locale 是 Linux 系统中多语言环境的接口，每个 locale 都定义了地区、语言和字符集。locale 的语法规则是：\<语言\>\_\<地区\>.\<字符集编码\>\<@修正值\>，如 zh\_CN.UTF-8 中，zh 表示中文，CN 表示中国大陆，UTF-8 表示字符集。

## 查看可用的字符集

`locale -a` 可以列出所有可用的语言环境/字符集，加 `-v` 选项可以输出更详细的信息。

```auto
ubuntu@ns524146:~$ locale -av
locale: en_US.utf8 archive: /usr/lib/locale/locale-archive
-------------------------------------------------------------------------------
    title | English locale for the USA
   source | Free Software Foundation, Inc.
  address | https://www.gnu.org/software/libc/
    email | bug-glibc-locales@gnu.org
 language | American English
territory | United States
 revision | 1.0
     date | 2000-06-24
  codeset | UTF-8

locale: C.utf8 directory: /usr/lib/locale/C.utf8
-------------------------------------------------------------------------------
    title | C locale
    email | bug-glibc-locales@gnu.org
 revision | 2.1
     date | 2022-01-30
  codeset | UTF-8
ubuntu@ns524146:~$ 

```

这些 locale 在 ubuntu 系统中的默认存放位置为：`/usr/share/i18n/locales`

## 检查中文字符集

```auto
$ locale -a | grep ^zh

```

![2025-02-18_00-09-10](https://www.isharkfly.com/images/transparent.png)

如果没有任何输出，则说明字符集没有安装。

## 安装中文字符集

运行命令进行安装。

```auto
sudo apt install language-pack-zh-hans

```

 ![2025-02-18_00-10-26](https://cdn.isharkfly.com/com-isharkfly-www/discourse-uploads/original/3X/6/c/6c839b888ef08c317a08468fde6961455a164146.jpeg)

## 启用语言集

通过修改 `/etc/locale.gen` 文件，将 `#zh_CN.UTF-8 UTF-8` 前面的注释去掉保存，并执行 `sudo locale-gen` 命令使之生效。

 ![2025-02-18_00-15-05](https://cdn.isharkfly.com/com-isharkfly-www/discourse-uploads/original/3X/2/b/2b9e4c9f6a0156732d6519547f05b0335fdc2fc5.jpeg)
