簡單解決Ubuntu修改locale的問題

本文針對的問題是「Ubuntu 安裝中文語言包」「Ubuntu Server中文問題」,「Ubuntu更改語言環境」,「Ubuntu locale的設定」,「cannot change locale (zh_CN.UTF-8)」,「Linux中文亂碼」,「Linux字符集的修改」,「Linux亂碼的解決辦法」等問題,提供一站式解決。若是系統顯示中文異常,例如出現顯示中文亂碼等,能夠參考本文章。關於CentOS系統的修改辦法,請參考文章末尾的描述。bash

問題描述ide

終端顯示中文正常,應用程序裏面顯示中文異常。spa

錯誤表現get

$ sudo dpkg-reconfigure locales

perl: warning: Setting locale failed.    
perl: warning: Please check that your locale settings:    
    LANGUAGE = "zh_CN:zh:en_US:en",    
    LC_ALL = "zh_CN.UTF-8",    
    LANG = "zh_CN.UTF-8"    
    are supported and installed on your system.    
perl: warning: Falling back to the standard locale ("C").    
locale: Cannot set LC_CTYPE to default locale: No such file or directory    
locale: Cannot set LC_MESSAGES to default locale: No such file or directory    
locale: Cannot set LC_ALL to default locale: No such file or directory    
/bin/bash: warning: setlocale: LC_ALL: cannot change locale (zh_CN.UTF-8)    
Generating locales...    
  en_US.UTF-8... /usr/sbin/locale-gen: line 177: warning: setlocale: LC_ALL: cannot change locale (zh_CN.UTF-8)    
up-to-date    
Generation complete.

it

$ man locale

man: can't set the locale; make sure $LC_* and $LANG are correct
io

$ locale

locale: Cannot set LC_CTYPE to default locale: No such file or directory    
locale: Cannot set LC_MESSAGES to default locale: No such file or directory    
locale: Cannot set LC_ALL to default locale: No such file or directory    
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=

class

$ localectl list-locales

C.UTF-8    
en_US.utf8    
zh_CN.utf8

變量

問題解決辦法亂碼

要想達到應用程序中正常顯示中文字符的目的,要下面的三個變量應該以下,date

LANG=zh_CN.UTF-8   
LANGUAGE=zh_CN:zh:en_US:en   
LC_ALL=LC_ALL=zh_CN.UTF-8

但問題在於「LC_ALL to default locale: No such file or directory」和「LC_ALL: cannot change locale (zh_CN.UTF-8) 」,所以從新安裝中文語言包。

執行下面命令

sudo apt-get -y install language-pack-zh-hans

sudo apt-get -y install language-pack-zh-hans language-pack-zh-hans-base

若是想支持繁體字,則執行

sudo apt-get -y install language-pack-zh-hant

sudo apt-get -y install language-pack-zh-hant language-pack-zh-hant-base

再執行locale命令設置當前locale環境

locale

相關的文件和參考

相關手冊:man locale(1), locale(5), locale(7)

相關文件:/usr/share/i18n/SUPPORTED

相關命令:

locale --all-locales 列出當前系統中全部支持的locale

localectl list-locales 列出當前系統中全部可用的locale

localectl set-locales LOCALE... 修改當前系統中的locale,與locale功能相同,與export上文提到的三個變量相同

對於CentOS,能夠直接編輯/etc/sysconfig/i18n文件,將LANG="en_US.UTF-8"設置成LANG="zh_CN.UTF-8",除此以外,CentOS下locale命令的顯示結果以下:

# locale

LANG=en_US.UTF-8    
LC_CTYPE="en_US.UTF-8"    
LC_NUMERIC="en_US.UTF-8"    
LC_TIME="en_US.UTF-8"    
LC_COLLATE="en_US.UTF-8"    
LC_MONETARY="en_US.UTF-8"    
LC_MESSAGES="en_US.UTF-8"    
LC_PAPER="en_US.UTF-8"    
LC_NAME="en_US.UTF-8"    
LC_ADDRESS="en_US.UTF-8"    
LC_TELEPHONE="en_US.UTF-8"    
LC_MEASUREMENT="en_US.UTF-8"    
LC_IDENTIFICATION="en_US.UTF-8"    
LC_ALL=

其中,LC_ALL=爲空,但並非表示這裏不對,而是LC_ALL=實際上等於LC_COLLATE、LC_CTYPE、LC_MONETARY、LC_MESSAGES、LC_NUMERIC、LC_TIME,參見locale(7)。

相關文章
相關標籤/搜索