PoshCode Archive  Artifact [a2e929048d]

Artifact a2e929048d9e7b6c32af6b65722e53d372a158001eeb5c18eefe511c02a584a9:

  • File Use-Culture.ps1 — part of check-in [2fdf841aae] at 2018-06-10 14:16:02 on branch trunk — From Windows PowerShell Cookbook (O’Reilly) by Lee Holmes (user: Lee Holmes size: 1854)

# encoding: ascii
# api: powershell
# title: Use-Culture.ps1
# description: From Windows PowerShell Cookbook (O’Reilly) by Lee Holmes
# version: 0.1
# type: script
# author: Lee Holmes
# license: CC0
# function: Set-Culture
# x-poshcode-id: 6449
# x-archived: 2016-07-20T02:36:24
# x-published: 2016-07-16T16:24:00
#
#
#############################################################################
##
## Use-Culture
##
## From Windows PowerShell Cookbook (O'Reilly)
## by Lee Holmes (http://www.leeholmes.com/guide)
##
#############################################################################

<#

.SYNOPSIS

Invoke a scriptblock under the given culture

.EXAMPLE

Use-Culture fr-FR { [DateTime]::Parse("25/12/2007") }
mardi 25 decembre 2007 00:00:00

#>

param(
    ## The culture in which to evaluate the given script block
    [Parameter(Mandatory = $true)]
    [System.Globalization.CultureInfo] $Culture,

    ## The code to invoke in the context of the given culture
    [Parameter(Mandatory = $true)]
    [ScriptBlock] $ScriptBlock
)

Set-StrictMode -Version Latest

## A helper function to set the current culture
function Set-Culture([System.Globalization.CultureInfo] $culture)
{
    [System.Threading.Thread]::CurrentThread.CurrentUICulture = $culture
    [System.Threading.Thread]::CurrentThread.CurrentCulture = $culture
}

## Remember the original culture information
$oldCulture = [System.Threading.Thread]::CurrentThread.CurrentUICulture

## Restore the original culture information if
## the user's script encounters errors.
trap { Set-Culture $oldCulture }

## Set the current culture to the user's provided
## culture.
Set-Culture $culture

## Invoke the user's scriptblock
& $ScriptBlock

## Restore the original culture information.
Set-Culture $oldCulture