CentOS/Ubuntu locale 中添加中文支持

CentOS/Ubuntu locale 中添加中文支持

CentOS/Ubuntu 中中文乱码问题解决

使用容器 CentOSUbuntu 默认 locale 中无 zh_CN.UTF-8

CentOS

  1. 进入容器

    shell
    1
    # docker run --rm -it --name centos centos:7 bash
  2. 通过 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
    54
    # locale -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 时可发现命令行中中文出现乱码

  3. 添加中文环境包

    shell
    1
    2
    3
    # yum -y groupinstall Fonts
    # yum -y install kde-l10n-Chinese
    # yum -y reinstall glibc-common
  4. 转化语言环境和字符集

    shell
    1
    # localedef -c -f UTF-8 -i zh_CN zh_CN.UTF-8
  5. 设定默认 locale 为中文UTF-8

    shell
    1
    2
    # export 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
      15
      # locale
      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=
    • 设置 localezh_CN.UTF-8

      shell
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      # locale
      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 测试

  6. Dockerfile 示例:

    Dockerfile
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    ARG 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

  1. 进入容器

    shell
    1
    # docker run --rm -it --name ubuntu ubuntu:14.04 bash
  2. 通过 locale -a 可看到当前环境中无中文相关(zh_CN.UTF-8

    shell
    1
    2
    3
    4
    # locale -a
    C
    C.UTF-8
    POSIX
  3. 安装中文支持,如果需要繁体可以安装 language-pack-zh-hant

    shell
    1
    # apt -y install language-pack-zh-hans-base language-pack-zh-hans
  4. 更新对应的 locale 文件,可以看到更新至最新的语言文件

    shell
    1
    2
    3
    4
    5
    # dpkg-reconfigure locales
    Generating locales...
    zh_CN.UTF-8... up-to-date
    zh_SG.UTF-8... up-to-date
    Generation complete.
  5. 设定默认 locale 为中文UTF-8

    shell
    1
    2
    3
    # export 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
      16
      # locale
      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=
    • 设置 localezh_CN.UTF-8

      shell
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      # locale
      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
  6. Dockerfile 示例:

    Dockerfile
    1
    2
    3
    4
    5
    6
    7
    8
    9
    ARG 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
评论

:D 一言句子获取中...

加载中,最新评论有1分钟缓存...