Setting locale failed:
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
LANGUAGE = "zh_CN:zh",
LC_ALL = (unset),
LC_ADDRESS = "zh_CN",
LC_NAME = "zh_CN",
LC_MONETARY = "zh_CN",
LC_PAPER = "zh_CN",
LC_IDENTIFICATION = "zh_CN",
LC_TELEPHONE = "zh_CN",
LC_MEASUREMENT = "zh_CN",
LC_TIME = "zh_CN",
LC_NUMERIC = "zh_CN",
LANG = "zh_CN.UTF-8"
are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
那是因为执行某些命令时,都会去执行一个update-locale的命令,用来更新locale
这个命令是一个脚本,用perl写的,可以用whereis update-locale
查到,位置在/usr/sbin/update-locale
14:21:24[~]~$ whereis update-locale
update-locale: /usr/sbin/update-locale /usr/share/man/man8/update-locale.8.gz
上述报错并不是因为update-locale命令而引起,我们也还是看看update-locale这个文件
#! /usr/bin/perl -w
use strict;
use Getopt::Long;
my $progname = "update-locale";
my $locale_file = "/etc/default/locale"; //加载了这个配置
my $help = 0;
my $reset = 0;
# Kept for compatibility reasons
my $remove = 0;
my $no_checks = 0;
GetOptions(
'reset' => \$reset,
'remove' => \$remove,
'locale-file=s' => \$locale_file,
'no-checks' => \$no_checks,
'h|help' => \$help,
);
sub usage
{
my $rc = shift;
print STDERR "Usage: $progname [OPTIONS] [LANG=locale] [LC_NUMERIC=locale] ...
Options:
--help display this message and exit
--reset ignore variables defined in the locale file
--locale-file=FILE file containing locale variables
(Default: /etc/default/locale)
--no-checks do not perform sanity checks on locale variables
";
exit $rc;
}
$help && usage(0);
# Process command-line arguments 处理命令行参数
my %arg = ();
my $content = '';
my $mode = 0644;
if (-r $locale_file)
{
# Keep file mode 保持文件模式
$mode = (stat($locale_file))[2] & 07777;
# Read current values 读取当前值
open(IN, "<", $locale_file)
or die "$progname: Unable to read $locale_file: $!\n";
while (<IN>)
{
$content .= $_;
next unless m/^(\w+)=(.*)/;
$arg{$1} = $2 unless $reset;
}
close(IN)
or die "$progname: Unable to close $locale_file: $!\n";
$content =~ s/^(\s*\w+=)/#$1/mg;
$content .= "\n" unless $content =~ m/\n$/s;
}
else
{
$content = "# File generated by $progname\n";
}
for (@ARGV)
{
if (m/(.*?)=(.*)/)
{
$arg{$1} = $2;
}
else
{
delete $arg{$_};
}
}
my $env = '';
my ($key, $value);
while (($key, $value) = each %arg)
{
$env .= " $key=$value";
$content =~ s/^#\s*$key=.*/$key=$value/m or
$content .= "$key=$value\n";
}
# Sanity checks 健全性检查
if ($no_checks == 0)
{
# Check that this locale does exist 检查此语言环境是否存在
my $charset = `LANG= LC_CTYPE= LC_NUMERIC= LC_TIME= LC_COLLATE= LC_MONETARY= LC_MESSAGES= LC_PAPER= LC_NAME= LC_ADDRESS= LC_TELEPHONE= LC_MEASUREMENT= LC_IDENTIFICATION= LC_ALL= $env locale charmap 2>&1`;
die "*** $progname: Error: invalid locale settings: $env\n"
if ($charset =~ m/Cannot set/);
# If LANGUAGE is set, its first value must be compatible with LC_MESSAGES
# 如果设置了 LANGUAGE,它的第一个值必须与 LC_MESSAGES 兼容
if (defined $arg{LANGUAGE})
{
my $language = $arg{LANGUAGE};
$language =~ s/["']//g;
$language =~ s/[.:_].*//;
my $msg = '';
my $var = '';
for (qw(LANG LC_MESSAGES LC_ALL))
{
if (defined $arg{$_})
{
$var = $_;
$msg = $arg{$_};
}
}
$msg =~ s/["']//g;
if ($msg !~ m/^$language/ && $var ne ''
&& $msg ne 'C' && $msg ne 'POSIX'
&& $language ne 'C' && $language ne 'POSIX')
{
print "*** $progname: Warning: LANGUAGE ($arg{LANGUAGE}) is not compatible with $var ($msg). Disabling it.\n";
$content =~ s/^(\s*LANGUAGE=)/#$1/mg;
}
}
}
# Write locale file 写入语言环境文件
open(OUT, ">", $locale_file)
or die "$progname: Unable to write $locale_file: $!\n";
print OUT $content;
close(OUT)
or die "$progname: Unable to close $locale_file: $!\n";
chmod($mode, $locale_file)
or die "$progname: Unable to chmod $locale_file: $!\n";
1;
这是我的配置
LANG="zh_CN.UTF-8"
LANGUAGE="zh_CN:en"
LC_NUMERIC="zh_CN.UTF-8"
LC_TIME="zh_CN.UTF-8"
LC_MONETARY="zh_CN.UTF-8"
LC_PAPER="zh_CN.UTF-8"
LC_IDENTIFICATION="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"
也可以su到root查看系统locale设置
14:52:17[~]~$ su
Password:
root@linux:/home/aaa# cd /root/
root@linux:~# locale //查看系统locale设置
LANG=zh_CN.UTF-8
LANGUAGE=zh_CN:zh
LC_CTYPE="C"
LC_NUMERIC="C"
LC_TIME="C"
LC_COLLATE="C"
LC_MONETARY="C"
LC_MESSAGES="C"
LC_PAPER="C"
LC_NAME="C"
LC_ADDRESS="C"
LC_TELEPHONE="C"
LC_MEASUREMENT="C"
LC_IDENTIFICATION="C"
root@linux:~# locale -a //查看系统已安装的locale
C
C.UTF-8
POSIX
en_AG
en_AG.utf8
en_AU.utf8
en_BW.utf8
en_CA.utf8
en_DK.utf8
en_GB.utf8
en_HK.utf8
en_IE.utf8
en_IL
en_IL.utf8
en_IN
en_IN.utf8
en_NG
en_NG.utf8
en_NZ.utf8
en_PH.utf8
en_SG.utf8
en_US.utf8
en_ZA.utf8
en_ZM
en_ZM.utf8
en_ZW.utf8
zh_CN
zh_CN.gb2312
zh_CN.utf8
zh_SG.utf8
定位错误为
- LC_ALL没设置值,去设置值
vi /etc/default/locale
或者
vi .bashrc
在文件尾加入
export LC_ALL=C 或者"zh_CN"
保存后执行,可以再重启
source .bashrc
- LC_CTYPE和LC_MESSAGES的值zh_CN.UTF-8系统未安装,可以使用以下命令测试:
perl -e exit
其实,真正的原因是perl为系统使用zh_CN.UTF-8,但系统不知道zh_CN.UTF-8是什么东西
解决方法也很简单
apt-get install language-pack-zh-hans
或者
cd /usr/share/locales
./install-language-pack zh_CN
安装一个中文语言,系统就知道zh_CN.UTF-8了,这个时候用perl就不会报错了
附上install-language-pack
#!/bin/sh -e
if [ -z "$1" ]; then
echo "Usage: $0 <language code> <class> [<version>]"
exit 0
fi
# install locales for base packages (not for gnome/kde)
if [ -z "$2" ]; then
# Update requested locales if locales-all is not installed
if dpkg-query -s locales-all >/dev/null 2>&1 ; then
echo "locales-all installed, skipping locales generation"
else
/usr/sbin/locale-gen --keep-existing "$1"
fi
fi
# ensure that .desktop caches are up to date
dpkg-trigger gmenucache || true