diff --git a/CHANGELOG.md b/CHANGELOG.md index 0bdbc82..32f4cf9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/source/Public/Get-LocalizedData.ps1 b/source/Public/Get-LocalizedData.ps1 index 39fb820..eb62278 100644 --- a/source/Public/Get-LocalizedData.ps1 +++ b/source/Public/Get-LocalizedData.ps1 @@ -212,6 +212,21 @@ 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 = @( @@ -219,11 +234,11 @@ function Get-LocalizedData $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) { @@ -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') } diff --git a/tests/Unit/Public/Get-LocalizedData.Tests.ps1 b/tests/Unit/Public/Get-LocalizedData.Tests.ps1 index 0701954..08fb0a4 100644 --- a/tests/Unit/Public/Get-LocalizedData.Tests.ps1 +++ b/tests/Unit/Public/Get-LocalizedData.Tests.ps1 @@ -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 @@ -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 + } + } } } }