Fully change language (including Culture) for the current PowerShell session

后端 未结 1 1810
情深已故
情深已故 2021-01-20 22:05

My Win 10 system has Spanish language. I mean to fully operate a PowerShell session in English. Across everything I tried (see below), I managed to change the UICultur

相关标签:
1条回答
  • 2021-01-20 22:37

    The following function will change both Culture and UICulture for the current PowerShell session in PowerShell 5.1:

    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)
    }
    

    It is actually a merge of the already linked answer and mkelement0's comment.

    Example usage:

    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:\>
    
    0 讨论(0)
提交回复
热议问题