Created
March 29, 2023 06:03
-
-
Save drlsdee/318bcf8263cdb71be85bcea7937c5619 to your computer and use it in GitHub Desktop.
This gist includes a class and functions for checking whether a file (for example, a script or an ADMX template) has an additional file with localized data that matches the specified culture.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Requires -Version 5.1 | |
class DotNetLocalizedStringFile { | |
[System.IO.FileInfo] | |
$SourceFile | |
[cultureinfo] | |
$CultureInfo | |
[System.IO.FileInfo] | |
$LocalizedFile | |
[bool] | |
$Exists | |
# Method: returns the localized file | |
[System.IO.FileInfo] | |
_getLocalizedFile( | |
[System.IO.FileInfo]$fileInfo, | |
[cultureinfo]$culture, | |
[string]$extension, | |
[bool]$byLCID | |
) { | |
if ($byLCID) { | |
[string]$cultureFolder = $culture.LCID | |
} | |
else { | |
[string]$cultureFolder = $culture.Name | |
} | |
[string]$outFilePath = [System.IO.Path]::ChangeExtension( | |
[System.IO.Path]::Combine( | |
$fileInfo.DirectoryName,$cultureFolder,$fileInfo.BaseName | |
),$extension | |
) | |
[System.IO.FileInfo]$outFileInfo = [System.IO.FileInfo]::new($outFilePath) | |
return $outFileInfo | |
} | |
# Constructor: default (no arguments) | |
DotNetLocalizedStringFile(){} | |
# Constructor: from System.IO.FileInfo, cultureinfo and extension | |
DotNetLocalizedStringFile( | |
[System.IO.FileInfo]$fileInfo, | |
[cultureinfo]$culture, | |
[string]$extension | |
) { | |
$this.SourceFile = $fileInfo | |
$this.CultureInfo = $culture | |
$this.LocalizedFile = $this._getLocalizedFile($fileInfo,$culture,$extension,$false) | |
$this.Exists = $this.LocalizedFile.Exists | |
} | |
# Constructor: from System.IO.FileInfo, cultureinfo and extension | |
DotNetLocalizedStringFile( | |
[System.IO.FileInfo]$fileInfo, | |
[cultureinfo]$culture, | |
[string]$extension, | |
[bool]$byLCID | |
) { | |
$this.SourceFile = $fileInfo | |
$this.CultureInfo = $culture | |
$this.LocalizedFile = $this._getLocalizedFile($fileInfo,$culture,$extension,$byLCID) | |
$this.Exists = $this.LocalizedFile.Exists | |
} | |
# Constructor: from file path, culture name and extension | |
DotNetLocalizedStringFile( | |
[string]$filePath, | |
[string]$cultureName, | |
[string]$extension | |
) { | |
$this.SourceFile = [System.IO.FileInfo]::new($filePath) | |
$this.CultureInfo = [cultureinfo]::GetCultureInfo($cultureName) | |
$this.LocalizedFile = $this._getLocalizedFile($this.SourceFile,$this.CultureInfo,$extension,$false) | |
$this.Exists = $this.LocalizedFile.Exists | |
} | |
# Constructor: from file path, culture name and extension | |
DotNetLocalizedStringFile( | |
[string]$filePath, | |
[string]$cultureName, | |
[string]$extension, | |
[bool]$byLCID | |
) { | |
$this.SourceFile = [System.IO.FileInfo]::new($filePath) | |
$this.CultureInfo = [cultureinfo]::GetCultureInfo($cultureName) | |
$this.LocalizedFile = $this._getLocalizedFile($this.SourceFile,$this.CultureInfo,$extension,$byLCID) | |
$this.Exists = $this.LocalizedFile.Exists | |
} | |
} | |
enum FileOutputType { | |
Missing = 0 | |
Present = 1 | |
} | |
function Get-DotNetLocalizedStringFile { | |
[CmdletBinding()] | |
param ( | |
[Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)] | |
[Alias('FullName')] | |
[System.IO.FileInfo] | |
$Path, | |
[Parameter()] | |
[string] | |
$Extension, | |
[Parameter()] | |
[string[]] | |
$UICulture, | |
[Parameter()] | |
[FileOutputType] | |
$OutputType, | |
[Parameter()] | |
[switch] | |
$ByLCID | |
) | |
begin { | |
if ($PSBoundParameters.ContainsKey('UICulture')) { | |
[cultureinfo[]]$cultureInfoAll = $UICulture.ForEach({[cultureinfo]::new($_)}) | |
} | |
else { | |
[cultureinfo[]]$cultureInfoAll = [cultureinfo]::CurrentUICulture | |
} | |
} | |
process { | |
foreach ($cultureInfo in $cultureInfoAll) { | |
[DotNetLocalizedStringFile]$outputObject = [DotNetLocalizedStringFile]::new($Path,$cultureInfo,$Extension,$ByLCID) | |
if (-not $PSBoundParameters.ContainsKey('OutputType')) { | |
$outputObject | |
continue | |
} | |
[bool]$shouldPresent = [int]$OutputType | |
[bool]$shouldOutput = $outputObject.Exists -eq $shouldPresent | |
if (-not $shouldOutput) { continue } | |
$outputObject | |
} | |
} | |
end {} | |
} | |
function Get-PolicyLocalizationFile { | |
[CmdletBinding()] | |
param ( | |
[Parameter(Mandatory)] | |
[string] | |
$Path, | |
[Parameter()] | |
[string[]] | |
$UICulture, | |
[Parameter()] | |
[FileOutputType] | |
$OutputType | |
) | |
begin { | |
if (-not $PSBoundParameters.ContainsKey('UICulture')) { | |
$UICulture = [System.Windows.Forms.InputLanguage]::InstalledInputLanguages.Culture.Name | |
} | |
$splatGetOutput = @{ | |
UICulture = $UICulture | |
Extension = '.adml' | |
} | |
if ($PSBoundParameters.ContainsKey('OutputType')) { | |
$splatGetOutput.Add('OutputType',$OutputType) | |
} | |
} | |
process { | |
Get-ChildItem -Path $Path -Filter '*.admx' -File | Get-DotNetLocalizedStringFile @splatGetOutput | |
} | |
end {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment