在我修补与问题目标有关的 PS 5.1完全更改当前 PowerShell 会话的语言期间, 我观察到一个“奇怪”的行为:
> [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)
(上面的函数是从这里获得的)。[system.threading.thread]::currentthread.
用任何地方替换[cultureinfo]::
产生完全相同的结果。
我发现奇怪的是:
我得出结论,“线程”(当前线程)的边界似乎是执行命令行。
尽管如此,这里它被引用为“会话范围(非持久)解决方案”(响应其 OP Temporarily change powershell language to English?)。该答案甚至发布了一个包装函数来限制对执行命令的更改范围。
该信息显然反对这一点,它指出“[Threading.Thread]::CurrentThread.CurrentUICulture
仅影响当前的单线”(与我的结论一致)。Culture
即使Set-Culture
仅在启动新会话后生效 ,我也可以立即更改那个“线程”。
我可能会得出结论,这就是它的工作原理。
这两点如何合理化? (我欢迎权威文档)。