理解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:

    <LANGUAGE>_<TERRITORY>.<CODESET>[@<MODIFIERS>]

    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配置中文字符集問題

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章