【问题标题】:Fully change language (including Culture) for the current PowerShell session完全更改当前 PowerShell 会话的语言(包括文化)
【发布时间】:2020-07-13 09:10:51
【问题描述】:

我的 Win 10 系统有西班牙语。 我的意思是用英语完全操作 PowerShell 会话。 在我尝试的所有操作中(见下文),我设法将当前会话的 UICulture 更改为 en-US,但不是 Culture

有什么方法可以永久更改当前 PowerShell 会话的 Culture


更改Culture(没有成功)

    > $( Get-Culture ; Get-UICulture ; [System.Threading.Thread]::CurrentThread.CurrentCulture ; [System.Threading.Thread]::CurrentThread.CurrentUICulture ; [CultureInfo]::CurrentCulture ; [CultureInfo]::CurrentUICulture ; ) | Format-Table -Property LCID,Name,DisplayName,IsNeutralCulture,UseUserOverride,IsReadOnly
     LCID Name  DisplayName             IsNeutralCulture UseUserOverride IsReadOnly
     ---- ----  -----------             ---------------- --------------- ----------
    11274 es-AR Spanish (Argentina)                False            True      False
     1033 en-US English (United States)            False            True      False
    11274 es-AR Spanish (Argentina)                False            True      False
     1033 en-US English (United States)            False            True      False
    11274 es-AR Spanish (Argentina)                False            True      False
     1033 en-US English (United States)            False            True      False 
    > Set-Culture 'en-US'
    > [Threading.Thread]::CurrentThread.CurrentCulture='en-US'
    > $( Get-Culture ; Get-UICulture ; [System.Threading.Thread]::CurrentThread.CurrentCulture ; [System.Threading.Thread]::CurrentThread.CurrentUICulture ; [CultureInfo]::CurrentCulture ; [CultureInfo]::CurrentUICulture ; ) | Format-Table -Property LCID,Name,DisplayName,IsNeutralCulture,UseUserOverride,IsReadOnly
     LCID Name  DisplayName             IsNeutralCulture UseUserOverride IsReadOnly
     ---- ----  -----------             ---------------- --------------- ----------
    11274 es-AR Spanish (Argentina)                False            True      False
     1033 en-US English (United States)            False            True      False
    11274 es-AR Spanish (Argentina)                False            True      False
     1033 en-US English (United States)            False            True      False
    11274 es-AR Spanish (Argentina)                False            True      False
     1033 en-US English (United States)            False            True      False

注意:我推断System.Threading.ThreadThreading.Thread是一样的。


我尝试过的其他事情

回答 herehere。 由于它们是 SO 帖子,我想不在这里发布它们会减少混乱。但如果觉得方便,我可以添加它们。

我发现了一些其他帖子,都重复了相同的命令。


编辑

设置文化的方法:

S1。 Win 设置 -> 时间和语言 -> 区域 -> 区域格式

S2。 Set-Culture <culture>

S3。 [CultureInfo]::CurrentCulture=<culture>

S4。 [Threading.Thread]::CurrentThread.CurrentCulture=<culture>

获取文化的方法:

G1。同S1。

G2。 Get-Culture

G3。 [CultureInfo]::CurrentCulture

G4。 [Threading.Thread]::CurrentThread.CurrentCulture

我发现了什么:

  1. S3/G3 和 S4/G4 似乎涉及相同的会话设置/环境变量/注册表项等 (?),并且完全等效。
  2. 方法 S1 在新会话中影响(G2、G3、G4)。
  3. 方法 S2 立即影响 G1,并在新会话中影响 (G3, G4)。
  4. 方法(S3、S4)会立即(微不足道)影响(G3、G4),但仅在当前线程中,而不是会话中,并且(1、2)根本没有。
  5. 方法 S1 中可用的选项比方法 S2 中的选项更丰富。我可以在 S1 中更改为“西班牙语(阿根廷)”、“英语(美国)”等,它会在 PS 中得到体现。但如果我在 S1 中更改为“Spansh (Brazil)”,那么 PS 将显示 en-US,就好像它是一种后备文化一样。

【问题讨论】:

    标签: powershell culture currentuiculture uiculture


    【解决方案1】:

    以下函数将更改 PowerShell 5.1 中当前 PowerShell 会话的 CultureUICulture

    function Set-PowerShellLanguage {
        
        Param (
            [Parameter(Mandatory)] 
            [System.Globalization.CultureInfo] $CultureInfo
        )
    
        if ($CultureInfo -notin (Get-WinUserLanguageList | % {$_.LanguageTag})) {
            Write-Error "Language pack for $CultureInfo is not installed."
            return
        }
        [System.Reflection.Assembly]::Load('System.Management.Automation').GetType('Microsoft.PowerShell.NativeCultureResolver').GetField('m_Culture', 'NonPublic, Static').SetValue($null, $CultureInfo)
        [System.Reflection.Assembly]::Load('System.Management.Automation').GetType('Microsoft.PowerShell.NativeCultureResolver').GetField('m_uiCulture', 'NonPublic, Static').SetValue($null, $CultureInfo)
    }
    

    实际上是already linked answermkelement0's comment的合并。

    示例用法:

    PS C:\> Get-Culture
    
    LCID             Name             DisplayName
    ----             ----             -----------
    1033             en-US            English (United States)
    
    
    PS C:\> Get-UICulture
    
    LCID             Name             DisplayName
    ----             ----             -----------
    1033             en-US            English (United States)
    
    
    PS C:\> Set-PowerShellLanguage 'es-AR'
    PS C:\> Get-Culture
    
    LCID             Name             DisplayName
    ----             ----             -----------
    11274            es-AR            Spanish (Argentina)
    
    
    PS C:\> Get-UICulture
    
    LCID             Name             DisplayName
    ----             ----             -----------
    11274            es-AR            Spanish (Argentina)
    
    
    PS C:\>
    

    【讨论】:

    • 太棒了! UICultures 是否有与Get-WinUserLanguageList 等效的名称?可用文化的相应列表可能(可能)不同......我只是在猜测,我不知道。
    • @sancho.sReinstateMonicaCellio 我认为存在误解。 Get-WinUserLanguageList 返回已安装的语言包,即 CultureInfoCultureUICulture 之间没有区别。这更像是您在上面的函数中为CultureUICulture 都使用了一个CultureInfo。我重构了一些名称以减少歧义。
    • 请注意,这不适用于 PowerShell v1。我将尝试为低级版本找到解决方法。此外,以下适用于 PowerShell Core 6.0 及更高版本: [System.Threading.Thread]::CurrentThread.CurrentUICulture = $culture; [System.Threading.Thread]::CurrentThread.CurrentCulture = $culture
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-15
    • 1970-01-01
    • 2018-03-29
    • 2013-09-04
    • 2021-12-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多