【问题标题】:powershell : changing the culture of current sessionpowershell:改变当前会话的文化
【发布时间】:2011-08-13 19:29:27
【问题描述】:

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

有机会吗?

【问题讨论】:

标签: windows powershell windows-vista powershell-2.0


【解决方案1】:

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

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

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

其他信息

查找哪些值可用于$culture

  • 这将为您提供文化类型列表:

    [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')
    

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

【讨论】:

  • 呃……不。这行不通。切换文化后,读出这些值。 $host 或 [System.Threading.Thread]::CurrentThread.CurrentUICulture 不会更新。在 win 10 和 server 2012 上使用 PowerShell 5 进行测试。
  • +1 表示“这不起作用”……至少不会改变宿主文化。但是,如果您将 (Get-Date).ToString() 弹出到函数末尾,那么您应该会看到该函数返回指定区域性的日期。
【解决方案2】:

由于@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)

【讨论】:

  • 这似乎会影响 Get-UICulture,但对当前会话的错误消息没有影响。它是否做出了持久的改变?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多