PoshCode Archive  Artifact Content

Artifact 0064cb4ef451357677226087652eade6bcc75db84abfc9fcd3b2de38556f0149:

  • File Using-Culture.ps1 — part of check-in [ff9a46990e] at 2018-06-10 13:36:57 on branch trunk — A cmdlet to run a script/scriptblock under a different culture (language). Useful for testing localization. (user: unknown size: 2273)

# encoding: ascii
# api: powershell
# title: 
# description: A cmdlet to run a script/scriptblock under a different culture (language). Useful for testing localization.
# version: 0.1
# type: function
# license: CC0
# function: Using-Culture
# x-poshcode-id: 4120
# x-archived: 2013-04-24T09:15:15
#
#
function Using-Culture
{
    <#
        .SYNOPSIS
            Runs a PowerShell script under a different locale to test localization features.
        .DESCRIPTION
            Runs a PowerShell script under a different locale to test localization features.
            Copied from http://rkeithhill.wordpress.com/2009/10/21/windows-powershell-2-0-string-localization/
            and converted to a Cmdlet including help comments.
        .LINK
            Import-LocalizedData
            Convert-FromStringData
            http://rkeithhill.wordpress.com/2009/10/21/windows-powershell-2-0-string-localization/
        .PARAMETER Culture
            The culture (language) to run the script in.
        .PARAMETER Script
            The scriptblock or script (wrapped in a scriptblock) to run.
        .EXAMPLE
            PS C:\> Using-Culture fr-FR { .\test.ps1 }

            Runs the script test.ps1 under French language settings.
    #>
        
    [CmdletBinding()]

    Param([Parameter(Mandatory = $true, HelpMessage = 'The culture (language) to run the script in.')]
          [ValidateNotNull()]
          [System.Globalization.CultureInfo]
          $Culture,
          [Parameter(Mandatory = $true, HelpMessage = 'The scriptblock or wrapped script to run.')]
          [ValidateNotNull()]
          [ScriptBlock]
          $Script
    )

    $OldCulture = [System.Threading.Thread]::CurrentThread.CurrentCulture
    $OldUICulture = [System.Threading.Thread]::CurrentThread.CurrentUICulture
    
    try
    {
        [System.Threading.Thread]::CurrentThread.CurrentCulture = $culture
        [System.Threading.Thread]::CurrentThread.CurrentUICulture = $culture
        
        Invoke-Command $script
    }
    finally
    {
        [System.Threading.Thread]::CurrentThread.CurrentCulture = $OldCulture
        [System.Threading.Thread]::CurrentThread.CurrentUICulture = $OldUICulture
    }
}