在 PowerShell 中更改错误消息语言

gou*_*taa 3 windows powershell

我正在尝试在 powershell 中以英文显示错误,以便我可以更轻松地在线搜索它们。

\n

当出现错误时,它会以法语显示,如下所示:

\n
PS C:\\Users\\Olivier\\lpthw> type nul > ex2.py\ntype : Impossible de trouver le chemin d'acc\xc3\xa8s \xc2\xab\xc2\xa0C:\\Users\\Olivier\\lpthw\\nul\xc2\xa0\xc2\xbb, car il n'existe pas.\nAu caract\xc3\xa8re Ligne:1 : 1\n+ type nul > ex2.py\n+ ~~~~~~~~~~~~~~~~~\n    + CategoryInfo          : ObjectNotFound: (C:\\Users\\Olivier\\lpthw\\nul:String) [Get-Content], ItemNotFoundException\n    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand\n
Run Code Online (Sandbox Code Playgroud)\n

mkl*_*nt0 8

PowerShell(核心)7+

\n

在您的会话中执行[cultureinfo]::CurrentUICulture = \'en-US\'(请参阅 参考资料System.Globalization.CultureInfo.CurrentUICulture)以使 PowerShell 从那时起发出英文消息。

\n

要为所有 PowerShell 会话预设它,请将该行添加到$PROFILE文件[1]中。(请参阅下文,了解如何始终持续地更改整个 Windows 会话的显示语言

\n

警告:v7.2.x开始,仅支持英语,因为 PowerShell 尚未以Windows PowerShell 的方式进行本地化 - 正在GitHub 问题 #666中跟踪进度。

\n

但是,一旦本地化完成,您将能够使用上述命令将会话切换为英语(或任何给定语言),即使您的系统上使用的是不同的 UI 语言(见下文)。

\n
\n

Windows PowerShell

\n

由于可以说是一个错误(已在 PowerShell Core 中修复),您无法直接在整个会话中“坚持”更改 UI 语言

\n
# !! Change of UI culture is effective only for a single command line\nPS> [cultureinfo]::CurrentUICulture = \'en-US\'; 1 / 0\nAttempted to divide by zero.\n...\n
Run Code Online (Sandbox Code Playgroud)\n

(甚至添加[cultureinfo]::CurrentUICulture = \'en-US\'到您的$PROFILE文件也没有帮助。)

\n

鉴于 Windows PowerShell 不再被积极开发,修复的可能性不大。

\n

但是,有两种解决方法

\n
    \n
  • 要么将这个不受支持但有效的黑客放在您的$PROFILE文件中[1],由这个答案提供提供(简化):

    \n
    function Set-PowerShellUICulture { \n  param([Parameter(Mandatory)] [cultureinfo] $culture) \n\n  [System.Reflection.Assembly]::Load(\'System.Management.Automation\').\n    GetType(\'Microsoft.PowerShell.NativeCultureResolver\').GetField(\'m_uiCulture\', \'NonPublic, Static\').\n      SetValue($null, $culture)\n\n}\n\n# Example call: Set the UI culture to \'en-US\' (US English)\n#               Use a value that `[cultureinfo]::new()` understands.\nSet-PowerShellUICulture en-US\n
    Run Code Online (Sandbox Code Playgroud)\n
      \n
    • 注意事项:\n
        \n
      • 这是不支持的,因为它使用反射来调用非公共类型。也就是说,鉴于 Windows PowerShell 不再处于积极开发状态,可以肯定的是,该黑客攻击将继续发挥作用。

        \n
      • \n
      • 在黑客攻击生效后,自动$PSUICulture变量无法反映有效的 UI 文化,因为它的值是在会话启动时静态确定的。然而,确实如此Get-UICulture

        \n
      • \n
      • 该黑客在 PowerShell (Core) 7+ 中不起作用,但您可以简单地将[cultureinfo]::CurrentUICulture = \'en-US\',如顶部所示,放在您的$PROFILE.

        \n
      • \n
      \n
    • \n
    \n
  • \n
  • 或者:通过“设置”应用程序或cmdlet(Windows 8+ / Server Windows 2012+)更改整个Windows 的显示语言;Set-WinUILanguageOverride此类更改是持久的需要注销或重新启动

    \n
      \n
    • 注意事项

      \n
        \n
      • 这意味着所有 GUI 元素(例如菜单)都将采用所选语言应用程序(支持本地化和所选语言)

        \n
      • \n
      • 另外,默认键盘布局将切换为所选语言。

        \n
      • \n
      • 由于需要注销或重启,这种方法也不方便尝试不同的语言。

        \n
      • \n
      \n
    • \n
    \n
  • \n
\n
\n

先决条件:无论当前的限制/错误如何,为了从根本上切换到不同的 UI 文化(显示语言),必须已通过“设置”应用程序 ( Settings>> Time & Language)将其安装为显示语言Language;与美国英语(en-US担心

\n
\n

[1] (当前用户,当前主机) 配置文件(其完整路径反映在自动$PROFILE变量中)可能尚不存在于您的系统上。
\n\xe2\x80\xa2 要按需创建它,请运行if (-not (Test-Path $PROFILE)) { New-Item -Force $PROFILE }
\n\xe2\x80\xa2 要使用 Visual Studio Code 对其进行编辑,请运行,code $PROFILE或者,如果未安装自定义文本编辑器,请使用
\n notepad $PROFILE
\n这样,如果文件的内容有可能(可能随着时间的推移)包含非 ASCII 字符(例如\xc3\xa9),请务必将文件保存为带有 BOM 的 UTF-8,这在 PowerShell (核心)7+ 和 Windows PowerShell。
\n有关详细信息,请参阅概念性about_Profiles帮助主题。

\n