簡體   English   中英

更改 powershell 中的錯誤消息語言

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM