暂时将powershell语言更改为英语? [英] Temporarily change powershell language to English?

查看:144
本文介绍了暂时将powershell语言更改为英语?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一些使用系统(powershell)命令输出的软件,但没有预见到除英语之外的其他语言的输出会有所不同.

I wrote some software that uses the output of system (powershell) commands, but did not foresee that the output would be different for languages other than English.

有没有办法将 Powershell 中的语言暂时更改为英语仅用于单个 Powershell 会话?

Is there a way to temporarily change the language in Powershell to English for just that one, single powershell session?

  • 如果它很重要,我希望运行的特定 powershell 代码是 netstat -n -a

我遇到了一些更改 powershell 语言的方法(例如 这里此处).但是我要注意不要永久更改它!(那会很糟糕)

I have come across some ways to change powershell language (e.g. here, here). But I want to be careful not to change it permanently! (that would be bad)

推荐答案

  • (a) 对于外部程序,例如netstat.exe,不幸的是没有方法(我知道)在会话中更改 UI 语言:

    • (a) For external programs such as netstat.exe, there is unfortunately no way (that I know of) to change the UI language in-session:

      • 在 Windows Server 2012/Windows 8 及更高版本上,Set-WinUILanguageOverride cmdlet 允许您(持续)更改当前用户的系统范围的 UI 语言,但这仅在未来登录会话中生效 - 也就是说,需要注销并重新登录或重新启动.

      • On Windows Server 2012 / Windows 8 and above, the Set-WinUILanguageOverride cmdlet allows you to (persistently) change the system-wide UI language for the current user, but that only takes effect in future logon sessions - that is, logging off and back on or a reboot are required.

      顺便说一句:在 Windows Server 2012/Windows 8 及更高版本上,还有 Set-Culture cmdlet,但它的目的是改变UI 文化(显示语言),但仅限于特定于文化的设置,例如日期、数字和货币格式.它也会永久更改当前用户的设置,但只需要一个新的会话(进程)即可使更改生效.

      As an aside: On Windows Server 2012 / Windows 8 and above, there is also the Set-Culture cmdlet, but its purpose is not to change the UI culture (display language), but only culture-specific settings such as date, number, and currency formats. It too changes the setting persistently for the current user, but only requires a new session (process) for the change to take effect.

      (b) 对于 PowerShell 命令.NET 类型一个会话中(非持久)解决方案 - 假设命令具有文化感知能力并且带有本地化字符串:

      (b) For PowerShell commands and .NET types, there is an in-session (non-persistent) solution - assuming the commands are culture-aware and come with localized strings:

      • Complementarily, you may want to set [cultureinfo]::CurrentCulture (note the missing UI part) as well, which determines the culture-specific number, date, ... formatting.
      • In older versions of PowerShell / .NET, you'll have to set these properties on [System.Threading.Thread]::CurrentThread instead; e.g.,
        [System.Threading.Thread]::CurrentThread.CurrentUICulture = 'en-US'

      请参阅帮助函数Use-Culture 的底部部分,该函数包装了此功能,以便在不同的文化暂时生效时执行代码.

      See the bottom section for helper function Use-Culture that wraps this functionality for execution of code while a different culture is temporarily in effect.

      注意事项:

      • PowerShell [Core] 本身尚未本地化,从 v7.0 开始此 GitHub 问题正在跟踪进度;但是,以下解决方案确实适用于附带本地化消息和帮助内容的第三方模块.

      • PowerShell [Core] itself is not localized yet as of v7.0; progress is being tracked in this GitHub issue; however, the solution below does work with third-party modules that ship with localized messages and help content.

      由于 Windows PowerShell 中的错误(PowerShell [Core] v6+ 受到影响),在-会话更改为 [cultureinfo]::CurrentUICulture[cultureinfo]::CurrentCulture 在命令提示符下自动重置,只要命令完成执行;但是,对于给定的脚本,更改对整个脚本及其被调用者仍然有效 - 请参阅此答案.

      Due to a bug in Windows PowerShell (PowerShell [Core] v6+ is not affected), in-session changes to [cultureinfo]::CurrentUICulture and [cultureinfo]::CurrentCulture are automatically reset at the command prompt, whenever a command finishes executing; however, for a given script the changes remain in effect for the entire script and its callees - see this answer.

      退一步:

      我编写了一些使用系统(powershell)命令输出的软件,但没有预见到除英语之外的其他语言的输出会有所不同.

      I wrote some software that uses the output of system (powershell) commands, but did not foresee that the output would be different for languages other than English.

      这正是通常值得寻找PowerShell-native解决方案而不是调用外部程序的原因:

      This is precisely why it's generally worth looking for PowerShell-native solutions as opposed to calling external programs:

      不必像netstat.exe那样解析——可能是本地化的——文本,例如,PowerShell命令返回对象 您可以以独立于文化的方式可靠地访问其属性.

      Instead of having to parse - possibly localized - text, as with netstat.exe, for instance, PowerShell commands return objects whose properties you can robustly access in a culture-independent fashion.

      具体来说,Mathias R. Jessen 建议查看 Get-NetTCPConnection 作为 netstat 的 PowerShell 替代方案.exe(适用于 Windows Server 2012/Windows 8 及更高版本).

      Specifically, Mathias R. Jessen suggests looking at Get-NetTCPConnection as a PowerShell alternative to netstat.exe (available on Windows Server 2012 / Windows 8 and above).

      为了帮助(b),下面是helper函数Use-Culture,你可以使用它来执行给定的脚本在给定(UI)文化的上下文中阻止({ ... }):

      # Windows PowerShell: emit an error message in *French* (culture 'fr-FR')
      # Note: Does not yet work as of PowerShell [Core] 7.0
      PS> Use-Culture fr-FR { try { 1/0 } catch { "Localized message: $_" } }
      Localized message: Tentative de division par zéro.
      

      请注意,文化变化 - 对于文化和 UI 文化 - 都是命令范围;也就是说,命令完成后,之前的设置再次生效.

      Note that the culture change - for both the culture and the UI culture - is command-scoped; that is, on completion of the command the previous settings take effect again.

      注意:代码改编自 这篇令人尊敬的博文.

      # Runs a script block in the context of the specified culture, without changing 
      # the session's culture persistently.
      # Handy for quickly testing the behavior of a command in the context of a different culture.
      # Example: 
      #   Use-Culture fr-FR { Get-Date }
      function Use-Culture
      {    
        param(
          [Parameter(Mandatory)] [cultureinfo] $Culture,
          [Parameter(Mandatory)] [scriptblock] $ScriptBlock
        )
        # Note: In Windows 10, a culture-info object can be created from *any* string.
        #        However, an identifier that does't refer to a *predefined* culture is 
        #        reflected in .LCID containing 4096 (0x1000)
        if ($Culture.LCID -eq 4096) { Throw "Unrecognized culture: $($Culture.DisplayName)" }
      
        # Save the current culture / UI culture values.
        $PrevCultures = [Threading.Thread]::CurrentThread.CurrentCulture, [Threading.Thread]::CurrentThread.CurrentUICulture
      
        try {
          # (Temporarily) set the culture and UI culture for the current thread.
          [Threading.Thread]::CurrentThread.CurrentCulture = [Threading.Thread]::CurrentThread.CurrentUICulture = $Culture
      
          # Now invoke the given code.
          & $ScriptBlock
      
        }    
        finally {
          # Restore the previous culture / UI culture values.
          [Threading.Thread]::CurrentThread.CurrentCulture = $PrevCultures[0]
          [Threading.Thread]::CurrentThread.CurrentUICulture = $PrevCultures[1]
        }
      }
      

      这篇关于暂时将powershell语言更改为英语?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆