[英]Change error message langage in powershell
我正在尝试在 powershell 中以英文显示错误,以便我可以更轻松地在线搜索它们。
出现错误时,它会以法语显示,如下所示:
PS C:\Users\Olivier\lpthw> type nul > ex2.py
type : Impossible de trouver le chemin d'accès « C:\Users\Olivier\lpthw\nul », car il n'existe pas.
Au caractère Ligne:1 : 1
+ type nul > ex2.py
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\Users\Olivier\lpthw\nul:String) [Get-Content], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand
PowerShell(核心)7+ :
在您的 session 中执行[cultureinfo]::CurrentUICulture = 'en-US'
(参见System.Globalization.CultureInfo.CurrentUICulture
)以使 PowerShell 从那时起发出英文消息;
要为所有会话预设它,请将该行添加到您的$PROFILE
文件[1] 。
Caveat: As of v7.0 , only English is supported, because PowerShell has not yet been localized in the way that Windows PowerShell is - progress is being tracked in GitHub issue #666 .
但是,一旦本地化完成,您就可以使用上述方法将会话切换为英语(给定语言),即使您的系统上使用了不同的 UI 语言(见下文)。
请参阅下文,了解如何更改 Windows整体的显示语言,始终不变。
Windows PowerShell :
由于可以说是一个错误(因为在 PowerShell 核心中修复),您不能直接为整个 session 更改 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 不再积极开发,因此不太可能修复。
但是,有两种解决方法:
要么:将此不受支持但有效的破解放在您的$PROFILE
文件[1]中,由这个答案提供(简化):
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
这是不支持的,因为它使用反射来调用非公共类型。 也就是说,鉴于 Windows PowerShell 不再处于积极开发阶段,因此可以安全地假设黑客将继续有效。
在 hack 生效后, 自动$PSUICulture
变量不会反映有效的 UI 文化,因为它的值是在 session 启动时静态确定的。 但是, Get-UICulture
可以。
该 hack 在 PowerShell (Core) 7+ 中不起作用,但您可以简单地将[cultureinfo]::CurrentUICulture = 'en-US'
放置在$PROFILE
中,如顶部所示。
或者:通过设置应用程序或Set-WinUILanguageOverride
cmdlet(Windows 8+ / 服务器 Windows 2012+)整体更改 Windows 的显示语言; 这种更改是持久的,需要注销或重新启动。
注意事项:
这意味着所有 GUI 元素(例如菜单)将在所有应用程序(支持本地化和所选语言)中使用所选语言。
此外,默认键盘布局将切换到所选语言。
鉴于需要注销或重新启动,这种方法对于尝试不同的语言也不方便。
先决条件:不管当前的限制/错误如何,为了从根本上切换到不同的 UI 文化(显示语言),它必须已经安装为显示语言,通过设置应用程序( Settings
> Time & Language
> Language
); 使用美国英语( en-US
) 不必担心,因为它预装了 Windows。
[1] (current-user, current-host) 配置文件,其完整路径反映在自动$PROFILE
变量中,您的系统上可能尚不存在。
• 要按需创建它,请运行if (-not (Test-Path $PROFILE)) { New-Item -Force $PROFILE }
• 例如,要使用 Visual Studio Code 对其进行编辑,请运行code $PROFILE
,或者,如果未安装自定义文本编辑器,请使用notepad $PROFILE
无论哪种方式,如果文件的内容有可能(可能随着时间的推移)包含非 ASCII 字符(例如é
),请务必将文件另存为UTF-8 和 BOM ,它适用于 PowerShell (Core) 7+ 和Windows PowerShell。
有关详细信息,请参阅概念about_Profiles帮助主题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.