Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save SimonWahlin/a9131e10d2eababf5b825007f63189b0 to your computer and use it in GitHub Desktop.
Save SimonWahlin/a9131e10d2eababf5b825007f63189b0 to your computer and use it in GitHub Desktop.
Set-PowerPointLanguage
<#
.SYNOPSIS
Change language settings for a PowerPoint presentation
.EXAMPLE
PS> Set-PowerPointLanguage -Path MyPres.pptx
Writes a copy of MyPres.pptx to MyPres.en-us.pptx with the language set to 'en-US' - English (the default)
.EXAMPLE
PS> Set-PowerPointLanguage -Path MyPres.pptx -Language de-DE
Sets the language to 'de-DE' (German)
.EXAMPLE
PS> gci -rec -inc *.pptx | Set-PowerPointLanguage -OutputPath {[io.path]::ChangeExtension($_.Fullname, ".en-us.pptx")} -whatif
Shows what will happen if you try to set the language to all pptx files in the current directory
#>
function Set-PowerPointLanguage {
[Alias("sppl")]
[CmdletBinding(SupportsShouldProcess)]
param(
[ValidatePattern("\.pptx$", ErrorMessage = "Eeh - this only work on PowerPoint files")]
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
[Alias("PSPath")]
[string] $Path
,
[Parameter(ValueFromPipelineByPropertyName)]
[string] $OutputPath
,
[ValidateSet("en-US", "de-DE", IgnoreCase = $false)] # since this are the official languages of PSConfEU
[string] $Language = 'en-US'
,
[switch] $Force
)
process {
$p = $null
$paths = $pscmdlet.GetResolvedProviderPathFromPSPath($Path, [ref] $p)
foreach ($resolvedPath in $Paths) {
if (!$PSBoundParameters.OutputPath) {
$OutputPath = [io.path]::ChangeExtension($resolvedPath, ".$Language.pptx")
}
$OutputPath = $pscmdlet.GetUnresolvedProviderPathFromPSPath($OutputPath)
$relSource = Resolve-Path -Relative $resolvedPath
$relOutput = $OutputPath
if ($OutputPath -like "$pwd*") {
$pwdLen = $pwd.ProviderPath.Length
$relOutput = "." + $OutputPath.SubString($pwdlen, $outputPath.length - $pwdlen)
}
if ($Force -or $pscmdlet.ShouldProcess( "$relSource -> $relOutput", "Change Language to '$Language'")) {
$resolvedItem = Get-Item $resolvedPath
if ($resolvedItem.Fullname -eq $OutputPath) {
Write-Error "Careful there - don't overwrite your original" -targetObject $resolvedPath
continue
}
if ((Test-Path $OutputPath) -and !$Force) {
if (!$PSCmdlet.ShouldContinue($relOutput, "File already exists. Overwrite?")) {
continue
}
Remove-Item $OutputPath -Confirm:$false
}
$tmpDir = "$env:Temp\$($resolvedItem.BaseName)"
if (Test-Path $tmpDir) {
Remove-Item -Recurse -Force:$Force $tmpDir -Confirm:$false # make sure we start with a clean slate
}
Expand-Archive $resolvedPath -DestinationPath $tmpDir -Force:$Force
Get-ChildItem $tmpDir -Recurse -Include *.xml | Foreach-Object {
$fullname = $_.fullname
(Get-Content -Raw -Encoding utf8 -LiteralPath $fullname) -replace 'lang="de-DE"', 'lang="en-US"' |
Set-Content -Literalpath $fullname -Encoding utf8 -NoNewline -Confirm:$false
}
Compress-Archive -Path $tmpDir\* -DestinationPath $OutputPath -Force:$Force
Remove-Item -Recurse -Force:$Force $tmpDir -Confirm:$false
Get-Item $OutputPath
}
}
}
}
Register-ArgumentCompleter -Command Set-PowerPointLanguage -Parameter Path -ScriptBlock {
param($c, $p, $w, $a, $fb)
Get-ChildItem -Recurse -Include *.pptx -Depth 2 | Where-Object Name -like $w* | Where-object Name -notmatch '.\w\w-\w\w\.pptx$' | foreach-Object {
[string]$rel = Resolve-path -Relative $_.fullname
$c = if ($rel -match '\s') { "`"$rel`""} else {$rel}
[System.Management.Automation.CompletionResult]::new($c, $rel, "ProviderItem", $_.fullname)
}
}
Register-ArgumentCompleter -Command Set-PowerPointLanguage -Parameter OutputPath -ScriptBlock {
param($c, $p, $w, $a, $fb)
. {
if ($fb.Path) {
[io.path]::ChangeExtension($fb.Path, ".en-us.pptx")
[io.path]::ChangeExtension($fb.Path, ".de-de.pptx")
}
else {
"{[io.path]::ChangeExtension(`$_.Fullname, '.en-us.pptx')}"
"{[io.path]::ChangeExtension(`$_.Fullname, '.de-de.pptx')}"
}
} | foreach-Object {
$cmp = if ($_ -match '\s') { "`"$_`""} else {$_}
[System.Management.Automation.CompletionResult]::new($cmp, $_, "ProviderItem", $_)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment