理解CentOS7.4/docker terminal 编码 locale中文支持

  • 获取编码格式

    From How to get terminal’s Character Encoding, the terminal uses environment variables to determine which character set to use, therefore you can determine it by looking at those variables: echo $LANG

  • locale

    Locales are used in Linux to define which language and character set (encoding) user see in the terminal.

    Locale is defined in the following format:

    _.[@]

    ITEMs FORMAT
    LANGUAGE ISO 639 language code
    TERRITORY ISO 3166 country code
    CODESET Character set or encoding identifier, like ISO-8859-1 or UTF-8

    locale是一个综合性的概念,是根据计算机用户所使用的语言、所在国家或者地区、当地的文化传统所定义的一个软件运行时的语言环境。

    这个语言环境可以按照所涉及的不同领域划分成几大类:用户所使用的语言符号及其分类(LC_CTYPE),数字 (LC_NUMERIC),比较和排序习惯(LC_COLLATE),时间显示格式(LC_TIME),货币单位(LC_MONETARY),信息主要是提示信息,错误信息, 状态信息, 标题, 标签, 按钮和菜单等(LC_MESSAGES),姓名书写方式(LC_NAME),地址书写方式(LC_ADDRESS),电话号码书写方式 (LC_TELEPHONE),度量衡表达方式(LC_MEASUREMENT),默认纸张尺寸大小(LC_PAPER)和locale对自身包含信息的概述(LC_IDENTIFICATION)。

    这些locale定义文件放在/usr/share/i18n/locales目录下

  • locale issues

    There are two questions: how to check and change the current locale and language settings from the command line in Linux.

    • check current locale and language settings
      # current locale and language settings
      $ locale 
      # list all enabled locales
      $ locale -a
      # list available encoding
      $ locale -m
      # locale and language settings are defined in the LANG variable
      $ echo $LANG
      
    • change the current locale and language settings
      • add new locale

        Before a locale can be enabled on the system, it must be generated.

        If you didn’t find the desired language or encoding in the list of enabled locales(locale -a), you can search for them in the list of all supported locales and install whatever you need.

        # for centos
        $ localedef --list-archive # list the all supported(available for generation) locales
        $ localedef -- list-archive | grep zh_CN.UTF-8 # find the desired locale 这一句好像无效
        $ sudo localedef -c -i zh_CN(locale定义文件) -f UTF-8(字符集) zh_CN.UTF-8 # generate it 
        $ locale -a | grep zh_CN.utf8 # available
        
      • already have useable locale

        To set the required locale and language for the current sesion - it is just needed to redefine LANG variable.

        $ LANG=zh_CN.utf8 # =两边不能有空格
        

        此方法设置后,echo $LANG 虽然显示zh_CN.utf8,但实际运行过程中中文还是乱码,而且locale命令下LANG未改变。

        $ export 
        
    • Define locale and language permanently

      Set the required value of the LANG variable in a user’s bash profile and the needed locale and language settings will be automatically loaded upon the each session.

      Put export LANG=zh_CN.utf8 line to the ~./bashrc or ~/.profile files.

      then source ~/.profile

  • Dockerfile

    修改基础镜像中文locale支持

    RUN yum -y update \
        && yum install kde-l10n-Chinese -y \
        && yum reinstall glibc-common -y \
        && localedef -c -f UTF-8 -i zh_CN zh_CN.UTF-8 \
        && echo 'LANG="zh_CN.UTF-8"' > /etc/locale.conf \
        && source /etc/locale.conf
    ENV LC_ALL=zh_CN.UTF-8 \
        LANG=zh_CN.UTF-8
    
  • Reference

  1. Linux: Define Locale and Language Settings

  2. 关于locale的设定,为什么要设定locale

  3. CentOS7及Docker配置中文字符集问题

你可能感兴趣的:(Linux,Docker)