PowerShell(核心)7+:
Execute [cultureinfo]::CurrentUICulture = 'en-US'
(see System.Globalization.CultureInfo.CurrentUICulture https://learn.microsoft.com/en-US/dotnet/api/System.Globalization.CultureInfo.CurrentUICulture) 在您的会话中使 PowerShell 从那时起发出英文消息。
To preset it for all your PowerShell sessions, add the line to your $PROFILE
file[1]. (See below for how to change the display language for your Windows sessions as a whole, invariably persistently.)
Caveat: As of v7.2.x, 只能说英语支持,因为 PowerShell 尚未本地化以 Windows PowerShell 的方式 - 跟踪进度GitHub 问题 #666 https://github.com/PowerShell/PowerShell/issues/666.
但是,一旦本地化完成,您就可以使用上述方法将会话切换为英语(或任何给定语言),即使您的系统上使用的是不同的 UI 语言(见下文)。
Windows PowerShell:
由于可以说是一个错误(已在 PowerShell Core 中修复),您无法直接在整个会话中“坚持”更改 UI 语言:
# !! Change of UI culture is effective only for a single command line
PS> [cultureinfo]::CurrentUICulture = 'en-US'; 1 / 0
Attempted to divide by zero.
...
(甚至添加[cultureinfo]::CurrentUICulture = 'en-US'
给你的$PROFILE
文件没有帮助。)
鉴于 Windows PowerShell 不再被积极开发,修复的可能性不大。
然而,有两个解决方法:
-
Either: Place this unsupported, but effective hack in your $PROFILE
file[1], courtesy of this answer https://stackoverflow.com/a/59118816/45375 (streamlined):
function Set-PowerShellUICulture {
param([Parameter(Mandatory)] [cultureinfo] $culture)
[System.Reflection.Assembly]::Load('System.Management.Automation').
GetType('Microsoft.PowerShell.NativeCultureResolver').GetField('m_uiCulture', 'NonPublic, Static').
SetValue($null, $culture)
}
# Example call: Set the UI culture to 'en-US' (US English)
# Use a value that `[cultureinfo]::new()` understands.
Set-PowerShellUICulture en-US
-
Caveats:
-
This is 不支持的,因为它使用反射来调用非公开的类型。也就是说,鉴于 Windows PowerShell 不再处于积极开发状态,可以肯定的是,该黑客攻击将继续发挥作用。
-
随着黑客攻击的生效,自动的$PSUICulture多变的 https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Automatic_Variables#psuiculture does not反映有效的 UI 文化,因为它的值是在会话启动时静态确定的。然而,Get-UICulture https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/get-uiculture does.
-
黑客确实not在 PowerShell (Core) 7+ 中工作,但您可以简单地放置[cultureinfo]::CurrentUICulture = 'en-US'
,如顶部所示,在您的$PROFILE
.
-
Or: 更改 Windows 的显示语言作为一个整体,通过“设置”应用程序或Set-WinUILanguageOverride https://learn.microsoft.com/en-us/powershell/module/international/set-winuilanguageoverridecmdlet(Windows 8+/服务器 Windows 2012+);这样的改变是执着的 and 需要注销或重新启动.
先决条件:无论当前的限制/错误如何,为了从根本上切换到不同的 UI 文化(显示语言)成为可能,它必须已经是已安装作为显示语言,通过“设置”应用程序(Settings
> Time & Language
> Language
); with 美国英语 (en-US
)这不是问题,因为它是随 Windows 预装的。
[1] The (current-user, current-host) profile file, whose full path is reflected in the automatic $PROFILE
variable, may not yet exist on your system.
• To create it on demand, run if (-not (Test-Path $PROFILE)) { New-Item -Force $PROFILE }
• To edit it with Visual Studio Code, for instance, run code $PROFILE
or, if no custom text editor is installed, use
notepad $PROFILE
Either way, if there's a chance that the file's content will (possibly over time) contain non-ASCII characters (e.g., é
), be sure to save the file as UTF-8 with BOM, which works in both PowerShell (Core) 7+ and Windows PowerShell.
For more information, see the conceptual about_Profiles https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Profiles help topic.