簡單解決Ubuntu修改locale的問題

2021-09-04 04:38:05 字數 3750 閱讀 4083

本文針對的問題是「ubuntu 安裝中文語言包」「ubuntu server中文問題」,「ubuntu更改語言環境」,「ubuntu locale的設定」,「cannot change locale (zh_cn.utf-8)」,「linux中文亂碼」,「linux字符集的修改」,「linux亂碼的解決辦法」等問題,提供一站式解決。如果系統顯示中文異常,例如出現顯示中文亂碼等,可以參考本文章。關於centos系統的修改辦法,請參考文章末尾的描述。

問題描述

終端顯示中文正常,應用程式裡面顯示中文異常。

錯誤表現

$ 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.

$ man locale
man: can't set the locale; make sure $lc_* and $lang are correct

$ 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_*****="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=

$ localectl list-locales
c.utf-8    

en_us.utf8    

zh_cn.utf8

問題解決辦法

要想達到應用程式中正常顯示中文字元的目的,要下面的三個變數應該如下,

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

locale --all-locales 列出當前系統中所有支援的locale

localectl list-locales 列出當前系統中所有可用的locale

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

# 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_*****="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)。

簡單解決Ubuntu修改locale的問題

問題描述 終端顯示中文正常,應用程式裡面顯示中文異常。錯誤表現 sudo dpkg reconfigure locales perl warning setting locale failed.perl warning please check that your locale settings l...

簡單解決Ubuntu修改locale的問題

原始出處 作者資訊和本宣告。否則將追究法律責任。本文針對的問題是 ubuntu 安裝中文語言包 ubuntu server中文問題 ubuntu更改語言環境 ubuntu locale的設定 cannot change locale zh cn.utf 8 linux中文亂碼 linux字符集的修改...

Ubuntu修改locale問題解決

本文針對的問題是 ubuntu安裝中文語言包 ubuntu server中文問題 ubuntu更改語言環境 ubuntu locale的設定 cannot change locale zh cn.utf 8 linux中文亂碼 linux字符集的修改 linux亂碼的解決辦法 等問題,提供一站式解決...