Repository navigation
feat(i18n): add a report of missing and extra language keys #860
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
fe0aaaf
feat(i18n): add a report of missing and extra language keys
HetCreep fd6ba42
Merge remote-tracking branch 'upstream/master' into feat/language-key…
HetCreep a0011c9
refactor: move the language key report scripts to Tools
HetCreep c018977
fix: keep the language key report going when one language fails to load
HetCreep File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| BeforeAll { | ||
| . (Join-Path $PSScriptRoot '..\Scripts\FileIO\Import-JsonFile.ps1') | ||
| . (Join-Path $PSScriptRoot '..\Scripts\FileIO\Import-LanguageFile.ps1') | ||
| . (Join-Path $PSScriptRoot '..\Tools\Get-LanguageKeyReport.ps1') | ||
|
|
||
| <# | ||
| .SYNOPSIS | ||
| Writes a language folder from plain hashtables, so each test states exactly which keys a language ships. | ||
| .PARAMETER Root | ||
| The directory that receives the language folder. | ||
| .PARAMETER Code | ||
| The language folder name, for example de-DE. | ||
| .PARAMETER Chrome | ||
| The flat Chrome catalog (key to text). | ||
| .PARAMETER Features | ||
| The Features table of Features.json (feature id to fields). | ||
| #> | ||
| function New-LanguageFolder { | ||
| param([string]$Root, [string]$Code, [hashtable]$Chrome, [hashtable]$Features = @{ TweakA = @{ Label = 'A' } }) | ||
|
|
||
| $folder = Join-Path $Root $Code | ||
| New-Item -ItemType Directory -Path $folder | Out-Null | ||
| $files = @{ | ||
| Chrome = $Chrome | ||
| Features = @{ Features = $Features; UiGroups = @{ GroupA = @{ Label = 'G'; Values = @{ ValueA = 'V' } } } } | ||
| Categories = @{ CategoryA = @{ Label = 'C' } } | ||
| Apps = @{ 'App.A' = @{ FriendlyName = 'App' } } | ||
| } | ||
| foreach ($name in $files.Keys) { | ||
| $files[$name] | ConvertTo-Json -Depth 6 | Set-Content -LiteralPath (Join-Path $folder "$name.json") -Encoding UTF8 | ||
| } | ||
| } | ||
|
|
||
| $script:EnglishChrome = @{ Title = 'Title'; Selected_one = '{0} item'; Selected_other = '{0} items' } | ||
| } | ||
|
|
||
| Describe 'Get-LanguageKeyReport' { | ||
| BeforeEach { | ||
| $root = Join-Path $TestDrive ([guid]::NewGuid().ToString('N')) | ||
| New-Item -ItemType Directory -Path $root | Out-Null | ||
| New-LanguageFolder -Root $root -Code 'en-US' -Chrome $script:EnglishChrome | ||
| } | ||
|
|
||
| It 'reports nothing for a language that has every key' { | ||
| New-LanguageFolder -Root $root -Code 'de-DE' -Chrome $script:EnglishChrome | ||
|
|
||
| $report = Get-LanguageKeyReport -LanguagesPath $root | ||
|
|
||
| $report.LanguageCode | Should -Be 'de-DE' | ||
| $report.MissingKeys | Should -BeNullOrEmpty | ||
| $report.ExtraKeys | Should -BeNullOrEmpty | ||
| } | ||
|
|
||
| It 'lists missing and extra keys, including nested catalog fields' { | ||
| New-LanguageFolder -Root $root -Code 'de-DE' -Chrome @{ Selected_one = 'x'; Selected_other = 'x'; Old = 'x' } ` | ||
| -Features @{ TweakB = @{ Label = 'B' } } | ||
|
|
||
| $report = Get-LanguageKeyReport -LanguagesPath $root | ||
|
|
||
| $report.MissingKeys | Should -Be @('Features.TweakA.Label', 'Title') | ||
| $report.ExtraKeys | Should -Be @('Features.TweakB.Label', 'Old') | ||
| } | ||
|
|
||
| It 'expects no _one keys for a language whose rule only returns other' { | ||
| New-LanguageFolder -Root $root -Code 'ja-JP' -Chrome @{ Title = 'x'; Selected_other = 'x' } | ||
|
|
||
| $report = Get-LanguageKeyReport -LanguagesPath $root | ||
|
|
||
| $report.MissingKeys | Should -BeNullOrEmpty | ||
| $report.ExtraKeys | Should -BeNullOrEmpty | ||
| } | ||
|
|
||
| It 'flags a _one key that a language whose rule only returns other never uses' { | ||
| New-LanguageFolder -Root $root -Code 'zh-CN' -Chrome $script:EnglishChrome | ||
|
|
||
| (Get-LanguageKeyReport -LanguagesPath $root).ExtraKeys | Should -Be @('Selected_one') | ||
| } | ||
|
|
||
| It 'expects a _many key where the language rule returns many' { | ||
| New-LanguageFolder -Root $root -Code 'pt-BR' -Chrome $script:EnglishChrome | ||
|
|
||
| (Get-LanguageKeyReport -LanguagesPath $root).MissingKeys | Should -Be @('Selected_many') | ||
| } | ||
|
|
||
| It 'does not report a shipped _other key as extra when the rule never returns other' { | ||
| New-LanguageFolder -Root $root -Code 'ru-RU' -Chrome @{ Title = 'x'; Selected_one = 'x'; Selected_few = 'x'; Selected_many = 'x'; Selected_other = 'x' } | ||
|
|
||
| $report = Get-LanguageKeyReport -LanguagesPath $root | ||
|
|
||
| $report.MissingKeys | Should -BeNullOrEmpty | ||
| $report.ExtraKeys | Should -BeNullOrEmpty | ||
| } | ||
|
|
||
| It 'marks a folder with a missing catalog as not loaded' { | ||
| New-LanguageFolder -Root $root -Code 'de-DE' -Chrome $script:EnglishChrome | ||
| Remove-Item -LiteralPath (Join-Path $root 'de-DE\Apps.json') | ||
| Mock Import-JsonFile -ParameterFilter { $filePath -like '*de-DE*Apps.json' } { $null } | ||
|
|
||
| $report = Get-LanguageKeyReport -LanguagesPath $root | ||
|
|
||
| $report.Loaded | Should -BeFalse | ||
| } | ||
|
|
||
| It 'marks a folder with a missing catalog as not loaded when errors stop the script, and still reports the others' { | ||
| New-LanguageFolder -Root $root -Code 'de-DE' -Chrome $script:EnglishChrome | ||
| New-LanguageFolder -Root $root -Code 'fr-FR' -Chrome $script:EnglishChrome | ||
| Remove-Item -LiteralPath (Join-Path $root 'de-DE\Apps.json') | ||
| Mock Write-Warning {} | ||
| $ErrorActionPreference = 'Stop' | ||
|
|
||
| $report = @(Get-LanguageKeyReport -LanguagesPath $root) | ||
|
|
||
| $report.LanguageCode | Should -Be @('de-DE', 'fr-FR') | ||
| $report[0].Loaded | Should -BeFalse | ||
| $report[1].Loaded | Should -BeTrue | ||
| Should -Invoke Write-Warning -Times 1 -Exactly -ParameterFilter { $Message -like "*de-DE*" } | ||
| } | ||
|
|
||
| It 'limits the report to the requested languages' { | ||
| New-LanguageFolder -Root $root -Code 'de-DE' -Chrome $script:EnglishChrome | ||
| New-LanguageFolder -Root $root -Code 'fr-FR' -Chrome $script:EnglishChrome | ||
|
|
||
| @(Get-LanguageKeyReport -LanguagesPath $root -LanguageCode 'fr-FR').LanguageCode | Should -Be @('fr-FR') | ||
| } | ||
| } | ||
|
|
||
| Describe 'Shipped languages' { | ||
| It 'all load for the report' { | ||
| $reports = @(Get-LanguageKeyReport -LanguagesPath (Join-Path $PSScriptRoot '..\Config\Languages')) | ||
|
|
||
| $reports | Should -Not -BeNullOrEmpty | ||
| foreach ($report in $reports) { | ||
| $report.Loaded | Should -BeTrue -Because "$($report.LanguageCode) must load" | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| <# | ||
| .SYNOPSIS | ||
| Lists the keys each translated language is missing or carries extra, compared to en-US. | ||
| .DESCRIPTION | ||
| Plural-aware: a plural family in en-US (for example AppSelectionStatusSelected_one and _other) is expected in | ||
| each language as one key per category that language's Get-PluralCategory rule can return, so ja-JP is not | ||
| reported missing its _one keys and pt-BR is not reported for having _many. A shipped _other key is always allowed. Requires Import-JsonFile.ps1 and Import-LanguageFile.ps1 from Scripts/FileIO. | ||
| Reports only; it never changes a language folder. A language folder whose catalogs cannot be loaded is returned with Loaded = $false and the remaining languages are still reported; only a missing en-US baseline throws. | ||
| .PARAMETER LanguagesPath | ||
| The directory containing language folders. Defaults to Config/Languages. | ||
| .PARAMETER LanguageCode | ||
| Limits the report to these language folders. Defaults to every folder except en-US. | ||
| .OUTPUTS | ||
| PSCustomObject per language: LanguageCode, Loaded, ExpectedCount, MissingKeys, ExtraKeys. | ||
| #> | ||
| function Get-LanguageKeyReport { | ||
| param( | ||
| [string]$LanguagesPath = (Join-Path $PSScriptRoot '..\Config\Languages'), | ||
| [string[]]$LanguageCode | ||
| ) | ||
|
|
||
| $baseline = Import-LanguageContent -LanguageFolder 'en-US' -LanguagesPath $LanguagesPath | ||
| if (-not $baseline) { | ||
| throw "Unable to load the en-US baseline from '$LanguagesPath'." | ||
| } | ||
|
|
||
| $pluralPattern = '^([^.]+)_(zero|one|two|few|many|other)$' | ||
| $plainKeys = @() | ||
| $pluralBases = @() | ||
| foreach ($key in (Get-LanguageKeyPaths -Lang $baseline)) { | ||
| if ($key -match $pluralPattern) { $pluralBases += $Matches[1] } else { $plainKeys += $key } | ||
| } | ||
| $pluralBases = @($pluralBases | Sort-Object -Unique) | ||
|
|
||
| # Counts that reach every category of the shipped rules, including the whole-million 'many' of Portuguese. | ||
| $sampleCounts = @(0..1000) + @(1000000, 1000001, 2000000) | ||
|
|
||
| if (-not $LanguageCode) { | ||
| $LanguageCode = @(Get-ChildItem -LiteralPath $LanguagesPath -Directory | Where-Object Name -ne 'en-US' | | ||
| Sort-Object Name | Select-Object -ExpandProperty Name) | ||
| } | ||
|
|
||
| foreach ($code in $LanguageCode) { | ||
| # A folder with a missing or unreadable catalog must not stop the report: list it as not loaded and go on. | ||
| try { | ||
| $content = Import-LanguageContent -LanguageFolder $code -LanguagesPath $LanguagesPath -ErrorAction Stop | ||
| } | ||
| catch { | ||
| Write-Warning "Language '$code' could not be loaded: $($_.Exception.Message)" | ||
| $content = $null | ||
| } | ||
| if (-not $content) { | ||
| [PSCustomObject]@{ LanguageCode = $code; Loaded = $false; ExpectedCount = 0; MissingKeys = @(); ExtraKeys = @() } | ||
| continue | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| $categories = @($sampleCounts | ForEach-Object { Get-PluralCategory -LanguageCode $code -Count $_ } | Sort-Object -Unique) | ||
| $expected = [System.Collections.Generic.HashSet[string]]::new([string[]]@( | ||
| $plainKeys + @($pluralBases | ForEach-Object { $base = $_; $categories | ForEach-Object { "${base}_$_" } }) | ||
| )) | ||
| # CLDR gives every language an 'other' category (fractions), so shipping it is allowed even when the rule never returns it. | ||
| $allowed = [System.Collections.Generic.HashSet[string]]::new($expected) | ||
| foreach ($base in $pluralBases) { [void]$allowed.Add("${base}_other") } | ||
| $actual = [System.Collections.Generic.HashSet[string]]::new([string[]](Get-LanguageKeyPaths -Lang $content)) | ||
|
|
||
| [PSCustomObject]@{ | ||
| LanguageCode = $code | ||
| Loaded = $true | ||
| ExpectedCount = $expected.Count | ||
| MissingKeys = @($expected | Where-Object { -not $actual.Contains($_) } | Sort-Object) | ||
| ExtraKeys = @($actual | Where-Object { -not $allowed.Contains($_) } | Sort-Object) | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| <# | ||
| .SYNOPSIS | ||
| Prints, per translated language, which keys are missing or extra compared to en-US. | ||
| .DESCRIPTION | ||
| Informational only: a language that lags behind en-US, or whose catalogs cannot be loaded (it is listed as | ||
| "could not load all catalogs"), never fails the run. It exits 1 only when the script itself breaks, for example | ||
| when the en-US baseline cannot be loaded. | ||
| Writes Markdown to the console and, on GitHub Actions, to the job summary. | ||
| .PARAMETER LanguageCode | ||
| Limits the report to these language folders. Defaults to every folder except en-US. | ||
| #> | ||
| [CmdletBinding()] | ||
| param( | ||
| [string[]]$LanguageCode | ||
| ) | ||
|
|
||
| $ErrorActionPreference = 'Stop' | ||
| $scriptsPath = Join-Path (Split-Path -Parent $PSScriptRoot) 'Scripts' | ||
| . (Join-Path $scriptsPath 'FileIO\Import-JsonFile.ps1') | ||
| . (Join-Path $scriptsPath 'FileIO\Import-LanguageFile.ps1') | ||
| . (Join-Path $PSScriptRoot 'Get-LanguageKeyReport.ps1') | ||
|
|
||
| $reports = @(Get-LanguageKeyReport -LanguageCode $LanguageCode) | ||
|
|
||
| $lines = [System.Collections.Generic.List[string]]::new() | ||
| $lines.Add('## Language key report (compared to en-US)') | ||
| $lines.Add('') | ||
| $lines.Add('| Language | Expected | Missing | Extra |') | ||
| $lines.Add('| --- | ---: | ---: | ---: |') | ||
| foreach ($report in $reports) { | ||
| if ($report.Loaded) { | ||
| $lines.Add("| $($report.LanguageCode) | $($report.ExpectedCount) | $($report.MissingKeys.Count) | $($report.ExtraKeys.Count) |") | ||
| } | ||
| else { | ||
| $lines.Add("| $($report.LanguageCode) | could not load all catalogs | | |") | ||
| } | ||
| } | ||
|
|
||
| foreach ($report in ($reports | Where-Object { $_.MissingKeys.Count -or $_.ExtraKeys.Count })) { | ||
| foreach ($kind in 'Missing', 'Extra') { | ||
| $keys = @($report."${kind}Keys") | ||
| if ($keys.Count -eq 0) { continue } | ||
| $lines.Add('') | ||
| $lines.Add("<details><summary>$($report.LanguageCode): $($keys.Count) $($kind.ToLowerInvariant())</summary>") | ||
| $lines.Add('') | ||
| foreach ($key in $keys) { $lines.Add("- ``$key``") } | ||
| $lines.Add('') | ||
| $lines.Add('</details>') | ||
| } | ||
| } | ||
|
|
||
| $lines | Write-Output | ||
| if ($env:GITHUB_STEP_SUMMARY) { | ||
| Add-Content -LiteralPath $env:GITHUB_STEP_SUMMARY -Value $lines -Encoding utf8 | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.