# 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: 2226
# x-archived: 2017-03-27T02:39:11
# x-published: 2011-09-09T21:42: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