Ubuntu设置系统语言环境

打开配置文件的时候发现注释里的中文都是乱码,网上搜罗一番,发现是系统语言环境是英文的,需要安装中文语言包,并设置到系统环境变量里

一般出现中文乱码的原因有两种情况:

  1. 设置的默认语言不支持显示中文
  2. 系统没有安装中文语言包

解决方案

1. 查看当前系统语言

echo $LANG

root@rock64:~# echo $LANG

2. 查看系统安装的语言包

`locale -a`

```bash
root@rock64:~# locale -a
C
C.UTF-8
POSIX
en_US.utf8
```

发现并没有中文的语言包

3. 安装中文语言包

`apt install language-pack-zh-hans`

```bash
root@rock64:~# apt install language-pack-zh-hans
Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package install
```

此时我们发现包管理仓库里并没有这个包,这里我们更一下包管理器后重新执行

```bash
root@rock64:~# apt update
```

```bash
root@rock64:~# apt install language-pack-zh-hans
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  language-pack-zh-hans-base
The following NEW packages will be installed:
  language-pack-zh-hans language-pack-zh-hans-base
0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.
Need to get 1694 kB of archives.
After this operation, 6726 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://ports.ubuntu.com/ubuntu-ports bionic-updates/main arm64 language-pack-zh-hans-base all 1:18.04+20180712 [1692 kB]
Get:2 http://ports.ubuntu.com/ubuntu-ports bionic-updates/main arm64 language-pack-zh-hans all 1:18.04+20180712 [1948 B]
Fetched 1694 kB in 4s (423 kB/s)           
Selecting previously unselected package language-pack-zh-hans-base.
(Reading database ... 49508 files and directories currently installed.)
Preparing to unpack .../language-pack-zh-hans-base_1%3a18.04+20180712_all.deb ...
Unpacking language-pack-zh-hans-base (1:18.04+20180712) ...
Selecting previously unselected package language-pack-zh-hans.
Preparing to unpack .../language-pack-zh-hans_1%3a18.04+20180712_all.deb ...
Unpacking language-pack-zh-hans (1:18.04+20180712) ...
Setting up language-pack-zh-hans (1:18.04+20180712) ...
Setting up language-pack-zh-hans-base (1:18.04+20180712) ...
Generating locales (this might take a while)...
  zh_CN.UTF-8... done
  zh_SG.UTF-8... done
Generation complete.
```

4. 查看是否安装成功

```bash
root@rock64:~# locale -a
C
C.UTF-8
POSIX
en_US.utf8
zh_CN.utf8
zh_SG.utf8
```

此时我们发现已经多了两个中文的语言包了。

5. 设置系统语言环境

`export LANG="zh_CN.UTF-8"`

```bash
// export 的方式只对当前终端生效
$ export LANG="zh_CN.UTF-8"

//  /etc/profile 文件中添加export LANG="zh_CN.UTF-8" 对所有用户生效
$ vim /etc/profile
```