gpt4 book ai didi

bash - 如何在当前终端 session 中设置语言环境?

转载 作者:行者123 更新时间:2023-11-29 08:57:08 26 4
gpt4 key购买 nike

我正在尝试通过更改 LANG 变量来更改 urxvt 当前 session 中的编码。但是,它似乎并不立即适用。这是我所做的:

可用语言环境:

$ locale -a
C
en_US.utf8
POSIX
ru_RU.koi8r
ru_RU.utf8

在设置新语言环境之前:

$ echo "а" | od -t x1
0000000 d0 b0 0a # good! UTF-8
# | a ||NL|

之后:

$ export LANG=ru_RU.KOI8-R
$ echo "а" | od -t x1
0000000 d0 b0 0a # hm..expect 'c1 0a'

通过运行 $ urxvt & fork 新的 urxvt 实例并最终得到我想要的:

$ echo "а" | od -t x1
0000000 c1 0a

为什么 LANG 一开始不改变行为?

最佳答案

有两个因素:

  • 您可能正在使用带有内置回显的 shell(并且没有通知 shell 您正在更改语言环境)
  • LANG 不是检查的第一个环境变量。根据locale(7) , LC_ALLLC_CTYPE 将首先被检查:
       If the second argument to setlocale(3) is an empty string, "", for       the default locale, it is determined using the following steps:       1.     If there is a non-null environment variable LC_ALL, the value              of LC_ALL is used.       2.     If an environment variable with the same name as one of the              categories above exists and is non-null, its value is used for              that category.       3.     If there is a non-null environment variable LANG, the value of              LANG is used.

For the latter, look at the output from the locale command, which lists all of the environment variables which would be used:

$ export LANG=ru_RU.KOI8-R
$ locale
LANG=ru_RU.KOI8-R
LANGUAGE=
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=en_US.UTF-8

仅仅改变 LANG 不应该改变其他变量,但改变 LC_ALL 通常会改变。

$ export LC_ALL=ru_RU.KOI8-R
$ locale
LANG=ru_RU.KOI8-R
LANGUAGE=
LC_CTYPE="ru_RU.KOI8-R"
LC_NUMERIC="ru_RU.KOI8-R"
LC_TIME="ru_RU.KOI8-R"
LC_COLLATE="ru_RU.KOI8-R"
LC_MONETARY="ru_RU.KOI8-R"
LC_MESSAGES="ru_RU.KOI8-R"
LC_PAPER="ru_RU.KOI8-R"
LC_NAME="ru_RU.KOI8-R"
LC_ADDRESS="ru_RU.KOI8-R"
LC_TELEPHONE="ru_RU.KOI8-R"
LC_MEASUREMENT="ru_RU.KOI8-R"
LC_IDENTIFICATION="ru_RU.KOI8-R"
LC_ALL=ru_RU.KOI8-R

关于bash - 如何在当前终端 session 中设置语言环境?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43953328/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com