CentOS/Ubuntu locale 中添加中文支持
使用容器 CentOS与Ubuntu 默认 locale
中无 zh_CN.UTF-8
CentOS
进入容器
shell 1
docker run --rm -it --name centos centos:7 bash
通过
locale -a
可看到当前环境中无中文相关(zh_CN.UTF-8
)shell >folded 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54locale -a
C
POSIX
en_AG
en_AG.utf8
en_AU
en_AU.iso88591
en_AU.utf8
en_BW
en_BW.iso88591
en_BW.utf8
en_CA
en_CA.iso88591
en_CA.utf8
en_DK
en_DK.iso88591
en_DK.utf8
en_GB
en_GB.iso88591
en_GB.iso885915
en_GB.utf8
en_HK
en_HK.iso88591
en_HK.utf8
en_IE
en_IE.iso88591
en_IE.iso885915@euro
en_IE.utf8
en_IE@euro
en_IN
en_IN.utf8
en_NG
en_NG.utf8
en_NZ
en_NZ.iso88591
en_NZ.utf8
en_PH
en_PH.iso88591
en_PH.utf8
en_SG
en_SG.iso88591
en_SG.utf8
en_US
en_US.iso88591
en_US.iso885915
en_US.utf8
en_ZA
en_ZA.iso88591
en_ZA.utf8
en_ZM
en_ZM.utf8
en_ZW
en_ZW.iso88591
en_ZW.utf8使用
echo
时可发现命令行中中文出现乱码添加中文环境包
shell 1
2
3yum -y groupinstall Fonts
yum -y install kde-l10n-Chinese
yum -y reinstall glibc-common转化语言环境和字符集
shell 1
localedef -c -f UTF-8 -i zh_CN zh_CN.UTF-8
设定默认
locale
为中文UTF-8shell 1
2export LANG=zh_CN.UTF-8
export LC_ALL=zh_CN.UTF-8设置环境变量前
shell 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15locale
LANG=
LC_CTYPE="POSIX"
LC_NUMERIC="POSIX"
LC_TIME="POSIX"
LC_COLLATE="POSIX"
LC_MONETARY="POSIX"
LC_MESSAGES="POSIX"
LC_PAPER="POSIX"
LC_NAME="POSIX"
LC_ADDRESS="POSIX"
LC_TELEPHONE="POSIX"
LC_MEASUREMENT="POSIX"
LC_IDENTIFICATION="POSIX"
LC_ALL=设置
locale
为zh_CN.UTF-8
后shell 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15locale
LANG=
LC_CTYPE="zh_CN.UTF-8"
LC_NUMERIC="zh_CN.UTF-8"
LC_TIME="zh_CN.UTF-8"
LC_COLLATE="zh_CN.UTF-8"
LC_MONETARY="zh_CN.UTF-8"
LC_MESSAGES="zh_CN.UTF-8"
LC_PAPER="zh_CN.UTF-8"
LC_NAME="zh_CN.UTF-8"
LC_ADDRESS="zh_CN.UTF-8"
LC_TELEPHONE="zh_CN.UTF-8"
LC_MEASUREMENT="zh_CN.UTF-8"
LC_IDENTIFICATION="zh_CN.UTF-8"
LC_ALL=zh_CN.UTF-8使用
echo
测试
Dockerfile 示例:
Dockerfile 1
2
3
4
5
6
7
8
9
10ARG VERSION=7
FROM centos:${VERSION}
RUN yum -y groupinstall Fonts \
&& yum -y install kde-l10n-Chinese \
&& yum -y reinstall glibc-common \
&& localedef -c -f UTF-8 -i zh_CN zh_CN.UTF-8
ENV LANG=zh_CN.UTF-8 \
LC_ALL=zh_CN.UTF-8
Ubuntu
进入容器
shell 1
docker run --rm -it --name ubuntu ubuntu:14.04 bash
通过
locale -a
可看到当前环境中无中文相关(zh_CN.UTF-8
)shell 1
2
3
4locale -a
C
C.UTF-8
POSIX安装中文支持,如果需要繁体可以安装
language-pack-zh-hant
包shell 1
apt -y install language-pack-zh-hans-base language-pack-zh-hans
更新对应的
locale
文件,可以看到更新至最新的语言文件shell 1
2
3
4
5dpkg-reconfigure locales
Generating locales...
zh_CN.UTF-8... up-to-date
zh_SG.UTF-8... up-to-date
Generation complete.设定默认
locale
为中文UTF-8shell 1
2
3export LANG=zh_CN.UTF-8
export LANGUAGE=zh_CN:zh:en_US:en
export LC_ALL=zh_CN.UTF-8设置环境变量前
shell 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16locale
LANG=
LANGUAGE=
LC_CTYPE="POSIX"
LC_NUMERIC="POSIX"
LC_TIME="POSIX"
LC_COLLATE="POSIX"
LC_MONETARY="POSIX"
LC_MESSAGES="POSIX"
LC_PAPER="POSIX"
LC_NAME="POSIX"
LC_ADDRESS="POSIX"
LC_TELEPHONE="POSIX"
LC_MEASUREMENT="POSIX"
LC_IDENTIFICATION="POSIX"
LC_ALL=设置
locale
为zh_CN.UTF-8
后shell 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16locale
LANG=zh_CN.UTF-8
LANGUAGE=zh_CN:zh:en_US:en
LC_CTYPE="zh_CN.UTF-8"
LC_NUMERIC="zh_CN.UTF-8"
LC_TIME="zh_CN.UTF-8"
LC_COLLATE="zh_CN.UTF-8"
LC_MONETARY="zh_CN.UTF-8"
LC_MESSAGES="zh_CN.UTF-8"
LC_PAPER="zh_CN.UTF-8"
LC_NAME="zh_CN.UTF-8"
LC_ADDRESS="zh_CN.UTF-8"
LC_TELEPHONE="zh_CN.UTF-8"
LC_MEASUREMENT="zh_CN.UTF-8"
LC_IDENTIFICATION="zh_CN.UTF-8"
LC_ALL=zh_CN.UTF-8
Dockerfile 示例:
Dockerfile 1
2
3
4
5
6
7
8
9ARG VERSION=14.04
FROM ubuntu:${VERSION}
RUN apt -y install language-pack-zh-hans-base language-pack-zh-hans \
&& dpkg-reconfigure locales
ENV LANG=zh_CN.UTF-8 \
LANGUAGE=zh_CN:zh:en_US:en \
LC_ALL=zh_CN.UTF-8
- 本文标题:CentOS/Ubuntu locale 中添加中文支持
- 本文作者:akiya
- 本文链接:https://little-star.love/posts/a98e83bc/
- 版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!