powershell - 完全更改当前 PowerShell session 的语言(包括文化)

标签 powershell culture currentuiculture uiculture

我的 Win 10 系统有西类牙语。 我的意思是用英语完全运行一个 PowerShell session 。 在我尝试的所有内容中(见下文),我设法更改了 UICultureen-US对于当前 session ,但不是 Culture .

有什么方法可以永久更改 Culture对于当前的 PowerShell session ?


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

最佳答案

以下函数将更改 PowerShell 5.1 中当前 PowerShell session 的 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 answer的合并和 mkelement0'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:\>

关于powershell - 完全更改当前 PowerShell session 的语言(包括文化),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62872708/

相关文章:

c# - 如何找到 Culture 的默认日期格式

c# - 按货币获取文化

c# - Global.asax 和 MasterPage 之间会发生什么?

c# - 创建线程时如何修复 CurrentUICulture

.net - Windows XP 的 CurrentUICulture 的初始值在哪里设置?

azure - Start-ComplianceSearch 上的 TaskCanceledException

powershell - Powershell if语句

git - 如何进行git克隆并进入创建的目录

powershell - 使用 Powershell V2 清空回收站

c# - ASP.NET - 转换本地化数字