windows powershell:改变当前会话的文化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7052765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 08:40:57  来源:igfitidea点击:

powershell : changing the culture of current session

windowspowershellwindows-vistapowershell-2.0

提问by tugberk

I am using powershell on windows vista. How do I change the culture of current session? My computer's culture is tr-TR so I am getting the error messages on Turkish. I would like to change to EN?

我在 windows vista 上使用 powershell。如何更改当前会话的文化?我的计算机的文化是 tr-TR,所以我收到土耳其语的错误消息。我想改成英文?

any chance?

任何机会?

回答by manojlds

Have a look here: http://blogs.msdn.com/b/powershell/archive/2006/04/25/583235.aspx

看看这里:http: //blogs.msdn.com/b/powershell/archive/2006/04/25/583235.aspx

and here: http://poshcode.org/2226:

在这里: http: //poshcode.org/2226

function Set-Culture([System.Globalization.CultureInfo] $culture)
{
    [System.Threading.Thread]::CurrentThread.CurrentUICulture = $culture
    [System.Threading.Thread]::CurrentThread.CurrentCulture = $culture
}


Additional Info

附加信息

To find which values can be used for $culture:

要查找哪些值可用于$culture

  • This will give you a list of Culture Types:

    [Enum]::GetValues([System.Globalization.CultureTypes])
    
  • Selecting one of the above types (e.g. AllCultures) you can then list the available values of that type:

    [System.Globalization.CultureInfo]::GetCultures( [System.Globalization.CultureTypes]::AllCultures )
    
  • You can then use the Name or Number of the culture you're interested in with the GetCultureInfomethod to retrieve the value you're after:

    $culture = [System.Globalization.CultureInfo]::GetCultureInfo(1033)
    $culture = [System.Globalization.CultureInfo]::GetCultureInfo('en-US')
    
  • 这将为您提供文化类型列表:

    [Enum]::GetValues([System.Globalization.CultureTypes])
    
  • 选择上述类型之一(例如 AllCultures),您可以列出该类型的可用值:

    [System.Globalization.CultureInfo]::GetCultures( [System.Globalization.CultureTypes]::AllCultures )
    
  • 然后,您可以使用您感兴趣的文化的名称或编号与GetCultureInfo方法来检索您所追求的值:

    $culture = [System.Globalization.CultureInfo]::GetCultureInfo(1033)
    $culture = [System.Globalization.CultureInfo]::GetCultureInfo('en-US')
    

NB: Thanks to implicit conversion, you could just pass the culture name or number (i.e. as a string or integer) to the Set-Culturemethod which would automatically be converted to the expected CultureInfo value.

注意:由于隐式转换,您可以将区域性名称或数字(即作为字符串或整数)传递给Set-Culture方法,该方法将自动转换为预期的 CultureInfo 值。

回答by TNT

As the accepted solution by @manojlds actually doesn't work (PS 5.1 on Windows 10) here what works for me (found on github):

由于@manojlds 接受的解决方案实际上不起作用(Windows 10 上的 PS 5.1),这里对我有用(在github 上找到):

$culture = [System.Globalization.CultureInfo]::CreateSpecificCulture("en-US")
$assembly = [System.Reflection.Assembly]::Load("System.Management.Automation")
$type = $assembly.GetType("Microsoft.PowerShell.NativeCultureResolver")
$field = $type.GetField("m_uiCulture", [Reflection.BindingFlags]::NonPublic -bor [Reflection.BindingFlags]::Static)
$field.SetValue($null, $culture)