Skip to content

Commit

Permalink
Get-LocalizedData: Improve on non-English operating systems (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
johlju authored Jul 22, 2020
1 parent 7ed9c78 commit baec570
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 8 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
design pattern to evaluate properties in both _Test_ and _Set_ - fixes
[issue #47](https://github.com/dsccommunity/DscResource.Common/issues/47).

## Fixed

- `Get-LocalizedData`
- Now correctly evaluates the default UI culture
on non-English operating systems ([issue #50](https://github.com/dsccommunity/DscResource.Common/issues/50).
- If the LCID 127 is found it will be skipped and instead use the default
UI culture (which is `'en-US'` unless specified) ([issue #11](https://github.com/dsccommunity/DscResource.Common/issues/11).

## [0.9.1] - 2020-07-08

## Added
Expand Down
63 changes: 56 additions & 7 deletions source/Public/Get-LocalizedData.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -212,18 +212,33 @@ function Get-LocalizedData
#>
$currentCulture = Get-UICulture

$evaluateDefaultCulture = $true

<#
If the LCID is 127 then use default UI culture instead.
See more information in issue https://github.com/dsccommunity/DscResource.Common/issues/11.
#>
if ($currentCulture.LCID -eq 127)
{
$currentCulture = New-Object -TypeName 'System.Globalization.CultureInfo' -ArgumentList @($DefaultUICulture)
$PSBoundParameters['UICulture'] = $DefaultUICulture

$evaluateDefaultCulture = $false
}

$languageFile = $null

$localizedFileNames = @(
$FileName + '.psd1'
$FileName + '.strings.psd1'
)

while ($null -ne $currentCulture -and $currentCulture.Name -and !$languageFile)
while ($null -ne $currentCulture -and $currentCulture.Name -and -not $languageFile)
{
foreach ($fullFileName in $localizedFileNames)
{
$filePath = [io.Path]::Combine($callingScriptRoot, $CurrentCulture.Name, $fullFileName)
$filePath = [System.IO.Path]::Combine($callingScriptRoot, $CurrentCulture.Name, $fullFileName)

if (Test-Path -Path $filePath)
{
Expand All @@ -243,14 +258,48 @@ function Get-LocalizedData
}
}

$currentCulture = $currentCulture.Parent
}
if (-not $languageFile)
{
<#
Evaluate the parent culture if there is one.
if (!$languageFile)
{
$PSBoundParameters.Add('UICulture', $DefaultUICulture)
If the parent culture is LCID 127 then move to the default culture.
See more information in issue https://github.com/dsccommunity/DscResource.Common/issues/11.
#>
if ($currentCulture.Parent -and $currentCulture.Parent.LCID -ne 127)
{
$currentCulture = $currentCulture.Parent
}
else
{
if ($evaluateDefaultCulture)
{
$evaluateDefaultCulture = $false

<#
Could not find localized strings file for the the operating
system UI culture. Evaluating the default UI culture (which
defaults to 'en-US' if not specifically set).
#>
$currentCulture = New-Object -TypeName 'System.Globalization.CultureInfo' -ArgumentList @($DefaultUICulture)
$PSBoundParameters['UICulture'] = $DefaultUICulture
}
else
{
<#
Already evaluated everything we could, exit and let
Import-LocalizedData throw an exception.
#>
break
}
}
}
}

<#
Removes the parameter DefaultUICulture so that isn't used when
calling Import-LocalizedData.
#>
$null = $PSBoundParameters.Remove('DefaultUICulture')
}

Expand Down
52 changes: 51 additions & 1 deletion tests/Unit/Public/Get-LocalizedData.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ $ProjectName = ((Get-ChildItem -Path $ProjectPath\*\*.psd1).Where{
Import-Module $ProjectName

InModuleScope $ProjectName {
Describe 'Get-LocalizedData' {
Describe 'Get-LocalizedData' -Tag 'GetLocalizedData' {
Context 'When specifying a specific filename' {
BeforeAll {
New-Item -Force -Path 'TestDrive:\ar-SA' -ItemType Directory
Expand Down Expand Up @@ -151,6 +151,56 @@ StringKey = Sträng värde
{ Import-Module -Name 'TestDrive:\DSC_Resource2.psm1' -ErrorAction 'Stop' } | Should -Not -Throw
}
}

<#
Regression test for issues loading the *.strings.psd1 from default
culture when OS has different culture for which no localized strings
exist.
#>
Context "When the operating system UI culture does not exist and the default culture has a '.strings.psd1' file" {
BeforeAll {
New-Item -Force -Path 'TestDrive:\en-US' -ItemType Directory

$null = "
ConvertFrom-StringData @`'
# sv-SE strings
StringKey = String value
'@
" | Out-File -Force -FilePath 'TestDrive:\en-US\Get-LocalizedData.Tests.strings.psd1'

Mock -CommandName Get-UICulture -MockWith {
return @{
Parent = @{
Parent = $null
LCID = '7'
KeyboardLayoutId = '7'
Name = 'de'
IetfLanguageTag = 'de'
DisplayName = 'German'
NativeName = 'Deutsch'
EnglishName = 'German'
TwoLetterISOLanguageName = 'de'
ThreeLetterISOLanguageName = 'deu'
ThreeLetterWindowsLanguageName = 'DEU'
}
LCID = '1031'
KeyboardLayoutId = '1031'
Name = 'de-DE'
IetfLanguageTag = 'de-DE'
DisplayName = 'Deutsch (Deutschland)'
NativeName = 'Deutsch (Deutschland)'
EnglishName = 'German (Germany)'
TwoLetterISOLanguageName = 'de'
ThreeLetterISOLanguageName = 'deu'
ThreeLetterWindowsLanguageName = 'DEU'
}
}
}

It 'Should retrieve the data' {
{ Get-LocalizedData -DefaultUICulture 'en-US' -BaseDirectory 'TestDrive:\' -ErrorAction 'Stop' } | Should -Not -Throw
}
}
}
}
}

0 comments on commit baec570

Please sign in to comment.