PowerShell的更改范围符合多个命令-UICulture [英] PowerShell scope of changes in lines with multiple commands - UICulture

查看:156
本文介绍了PowerShell的更改范围符合多个命令-UICulture的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在修改PS 5.1期间,与问题目标有关完全更改当前PowerShell会话的语言 我观察到奇怪"的情况.行为:

During my tinkering with PS 5.1 related to the objective of question Fully change language for the current PowerShell session I observed a "strange" behavior:

> [system.threading.thread]::currentthread.currentculture ; [system.threading.thread]::currentthread.currentuiculture ;
LCID             Name             DisplayName
----             ----             -----------
1033             en-US            English (United States)
1033             en-US            English (United States)
> function Set-CultureWin([System.Globalization.CultureInfo] $culture) { [System.Threading.Thread]::CurrentThread.CurrentUICulture = $culture ; [System.Threading.Thread]::CurrentThread.CurrentCulture = $culture } ; Set-CultureWin es-ES ; [system.threading.thread]::currentthread.currentculture ; [system.threading.thread]::currentthread.currentuiculture ;
LCID             Name             DisplayName
----             ----             -----------
3082             es-ES            Español (España)
3082             es-ES            Español (España)
> [system.threading.thread]::currentthread.currentculture ; [system.threading.thread]::currentthread.currentuiculture ;
LCID             Name             DisplayName
----             ----             -----------
1033             en-US            English (United States)
1033             en-US            English (United States)

(上面的功能是从此处获得的). 到处都用[cultureinfo]::替换[system.threading.thread]::currentthread.会产生完全相同的结果.

(the function above was obtained from here). Replacing [system.threading.thread]::currentthread. with [cultureinfo]:: everywhere produced exactly the same results.

我发现奇怪的是:

    我得出的结论是,线程"的边界是不规则的. (当前线程)似乎是执行命令行.
    不过,在此处,它被引用为会话范围的(非持久性)解决方案". (针对其OP >暂时将powershell语言更改为英语?). 该答案甚至发布了一个包装函数,以将更改范围限制为正在执行的命令.
    该信息显然反对,其中指出"[Threading.Thread]::CurrentThread.CurrentUICulture仅影响当前的单线". (与我的结论一致).
  1. I conclude that the boundary of a "thread" (the currentthread) appears to be the execution command line.
    Nevertheless, here it is quoted as "a session-scoped (non-persistent) solution" (in response to its OP Temporarily change powershell language to English?). That answer even posted a wrapper function to limit the scope of the change to the executing command.
    This information apparently opposes this, which states "[Threading.Thread]::CurrentThread.CurrentUICulture only affects to current one-liner" (coinciding with my conclusion).

即使Set-Culture仅在启动新会话后才生效,我也可以仅针对一个线程"立即获得Culture的更改.
我可能会得出结论,那就是它的工作原理.

I can obtain an immediate change of Culture for just that one "thread", even if Set-Culture only takes effect after launching a new session.
I might conclude that's how it works.

如何合理化这两点? (我欢迎权威文档.)

How can these two points be rationalized? (I would welcome authoritative documentation).

推荐答案

由于 Windows PowerShell 中的 bug (PowerShell [Core] v6 +不受 的影响),在会话期间对[cultureinfo]::CurrentUICulture[cultureinfo]::CurrentCulture所做的更改将在启动时自动重置为 每当命令完成执行时,在命令提示符下>> [1] .

Due to a bug in Windows PowerShell (PowerShell [Core] v6+ is not affected), in-session changes to [cultureinfo]::CurrentUICulture and [cultureinfo]::CurrentCulture are automatically reset to the values at startup time[1] at the command prompt, whenever a command finishes executing.

不幸的是,尝试修改$PROFILE中的区域性也会受到同样的影响.

Trying to modify the culture(s) in $PROFILE is equally affected, unfortunately.

但是,对于给定的脚本,脚本内区域性更改对于整个脚本及其被调用方(在主线程上)仍然有效-请参见

However, for a given script, in-script culture changes remain in effect for the entire script as well as its callees (on the main thread) - see this answer.

唯一的解决方法在启动PowerShell之前更改当前用户的操作系统级别语言/区域设置

The only workaround is to change the current user's OS-level language / locale settings before starting PowerShell.

以编程方式

  • you can change the current user's display language (UI culture, reflected in [cultureinfo]::CurrentUICulture) with Set-WinUILanguageOverride

您可以使用

you can change the locale (culture, reflected in [cultureinfo]::CurrentCulture) with Set-Culture

请注意,这两个命令都至少需要Windows 8/Windows Server 2012,并且仅在未来会话中生效.

Note that both commands require at least Windows 8 / Windows Server 2012 and only take effect in future sessions.

[1] PowerShell 进程启动时的值,由用户的操作系统级别的语言和区域设置确定.

[1] The values at startup of the PowerShell process, as determined by the user's OS-level language and locale settings.

这篇关于PowerShell的更改范围符合多个命令-UICulture的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆