Skip to content

Commit 39d1127

Browse files
HetCreepRaphire
authored andcommitted
feat(translations): add a report of missing and extra language keys (#860)
1 parent 6797d6e commit 39d1127

5 files changed

Lines changed: 291 additions & 0 deletions

File tree

‎.github/CONTRIBUTING.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ Win11Debloat/
144144
├── Assets/ # Static assets (icons, start menu templates)
145145
├── Tools/ # Development scripts (excluded from release packages)
146146
│ ├── Build-Release.ps1 # Runtime release packaging
147+
│ ├── Get-LanguageKeyReport.ps1 # Compares language catalogs with en-US (plural-aware)
148+
│ ├── Report-LanguageKeys.ps1 # Prints the missing/extra language key report
147149
│ └── Run-Tests.ps1 # Pester test runner (optionally bootstraps Pester)
148150
├── Tests/ # Pester tests for script and XAML contracts
149151
├── Backups/ # Registry backups (generated during use)

‎.github/workflows/tests.yml‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,17 @@ jobs:
4242
Install-Module -Name PSScriptAnalyzer -Scope CurrentUser -Force -Confirm:$false
4343
Import-Module PSScriptAnalyzer
4444
Invoke-ScriptAnalyzer -Path . -Recurse -Severity Error -EnableExit
45+
46+
language-keys:
47+
name: Language key report (informational)
48+
runs-on: windows-latest
49+
timeout-minutes: 5
50+
steps:
51+
- name: Check out repository
52+
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
53+
with:
54+
persist-credentials: false
55+
56+
- name: Report missing and extra language keys
57+
shell: powershell
58+
run: ./Tools/Report-LanguageKeys.ps1
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
BeforeAll {
2+
. (Join-Path $PSScriptRoot '..\Scripts\FileIO\Import-JsonFile.ps1')
3+
. (Join-Path $PSScriptRoot '..\Scripts\FileIO\Import-LanguageFile.ps1')
4+
. (Join-Path $PSScriptRoot '..\Tools\Get-LanguageKeyReport.ps1')
5+
6+
<#
7+
.SYNOPSIS
8+
Writes a language folder from plain hashtables, so each test states exactly which keys a language ships.
9+
10+
.PARAMETER Root
11+
The directory that receives the language folder.
12+
13+
.PARAMETER Code
14+
The language folder name, for example de-DE.
15+
16+
.PARAMETER Chrome
17+
The flat Chrome catalog (key to text).
18+
19+
.PARAMETER Features
20+
The Features table of Features.json (feature id to fields).
21+
#>
22+
function New-LanguageFolder {
23+
param([string]$Root, [string]$Code, [hashtable]$Chrome, [hashtable]$Features = @{ TweakA = @{ Label = 'A' } })
24+
25+
$folder = Join-Path $Root $Code
26+
New-Item -ItemType Directory -Path $folder | Out-Null
27+
$files = @{
28+
Chrome = $Chrome
29+
Features = @{ Features = $Features; UiGroups = @{ GroupA = @{ Label = 'G'; Values = @{ ValueA = 'V' } } } }
30+
Categories = @{ CategoryA = @{ Label = 'C' } }
31+
Apps = @{ 'App.A' = @{ FriendlyName = 'App' } }
32+
}
33+
foreach ($name in $files.Keys) {
34+
$files[$name] | ConvertTo-Json -Depth 6 | Set-Content -LiteralPath (Join-Path $folder "$name.json") -Encoding UTF8
35+
}
36+
}
37+
38+
$script:EnglishChrome = @{ Title = 'Title'; Selected_one = '{0} item'; Selected_other = '{0} items' }
39+
}
40+
41+
Describe 'Get-LanguageKeyReport' {
42+
BeforeEach {
43+
$root = Join-Path $TestDrive ([guid]::NewGuid().ToString('N'))
44+
New-Item -ItemType Directory -Path $root | Out-Null
45+
New-LanguageFolder -Root $root -Code 'en-US' -Chrome $script:EnglishChrome
46+
}
47+
48+
It 'reports nothing for a language that has every key' {
49+
New-LanguageFolder -Root $root -Code 'de-DE' -Chrome $script:EnglishChrome
50+
51+
$report = Get-LanguageKeyReport -LanguagesPath $root
52+
53+
$report.LanguageCode | Should -Be 'de-DE'
54+
$report.MissingKeys | Should -BeNullOrEmpty
55+
$report.ExtraKeys | Should -BeNullOrEmpty
56+
}
57+
58+
It 'lists missing and extra keys, including nested catalog fields' {
59+
New-LanguageFolder -Root $root -Code 'de-DE' -Chrome @{ Selected_one = 'x'; Selected_other = 'x'; Old = 'x' } `
60+
-Features @{ TweakB = @{ Label = 'B' } }
61+
62+
$report = Get-LanguageKeyReport -LanguagesPath $root
63+
64+
$report.MissingKeys | Should -Be @('Features.TweakA.Label', 'Title')
65+
$report.ExtraKeys | Should -Be @('Features.TweakB.Label', 'Old')
66+
}
67+
68+
It 'expects no _one keys for a language whose rule only returns other' {
69+
New-LanguageFolder -Root $root -Code 'ja-JP' -Chrome @{ Title = 'x'; Selected_other = 'x' }
70+
71+
$report = Get-LanguageKeyReport -LanguagesPath $root
72+
73+
$report.MissingKeys | Should -BeNullOrEmpty
74+
$report.ExtraKeys | Should -BeNullOrEmpty
75+
}
76+
77+
It 'flags a _one key that a language whose rule only returns other never uses' {
78+
New-LanguageFolder -Root $root -Code 'zh-CN' -Chrome $script:EnglishChrome
79+
80+
(Get-LanguageKeyReport -LanguagesPath $root).ExtraKeys | Should -Be @('Selected_one')
81+
}
82+
83+
It 'expects a _many key where the language rule returns many' {
84+
New-LanguageFolder -Root $root -Code 'pt-BR' -Chrome $script:EnglishChrome
85+
86+
(Get-LanguageKeyReport -LanguagesPath $root).MissingKeys | Should -Be @('Selected_many')
87+
}
88+
89+
It 'does not report a shipped _other key as extra when the rule never returns other' {
90+
New-LanguageFolder -Root $root -Code 'ru-RU' -Chrome @{ Title = 'x'; Selected_one = 'x'; Selected_few = 'x'; Selected_many = 'x'; Selected_other = 'x' }
91+
92+
$report = Get-LanguageKeyReport -LanguagesPath $root
93+
94+
$report.MissingKeys | Should -BeNullOrEmpty
95+
$report.ExtraKeys | Should -BeNullOrEmpty
96+
}
97+
98+
It 'marks a folder with a missing catalog as not loaded' {
99+
New-LanguageFolder -Root $root -Code 'de-DE' -Chrome $script:EnglishChrome
100+
Remove-Item -LiteralPath (Join-Path $root 'de-DE\Apps.json')
101+
Mock Import-JsonFile -ParameterFilter { $filePath -like '*de-DE*Apps.json' } { $null }
102+
103+
$report = Get-LanguageKeyReport -LanguagesPath $root
104+
105+
$report.Loaded | Should -BeFalse
106+
}
107+
108+
It 'marks a folder with a missing catalog as not loaded when errors stop the script, and still reports the others' {
109+
New-LanguageFolder -Root $root -Code 'de-DE' -Chrome $script:EnglishChrome
110+
New-LanguageFolder -Root $root -Code 'fr-FR' -Chrome $script:EnglishChrome
111+
Remove-Item -LiteralPath (Join-Path $root 'de-DE\Apps.json')
112+
Mock Write-Warning {}
113+
$ErrorActionPreference = 'Stop'
114+
115+
$report = @(Get-LanguageKeyReport -LanguagesPath $root)
116+
117+
$report.LanguageCode | Should -Be @('de-DE', 'fr-FR')
118+
$report[0].Loaded | Should -BeFalse
119+
$report[1].Loaded | Should -BeTrue
120+
Should -Invoke Write-Warning -Times 1 -Exactly -ParameterFilter { $Message -like "*de-DE*" }
121+
}
122+
123+
It 'limits the report to the requested languages' {
124+
New-LanguageFolder -Root $root -Code 'de-DE' -Chrome $script:EnglishChrome
125+
New-LanguageFolder -Root $root -Code 'fr-FR' -Chrome $script:EnglishChrome
126+
127+
@(Get-LanguageKeyReport -LanguagesPath $root -LanguageCode 'fr-FR').LanguageCode | Should -Be @('fr-FR')
128+
}
129+
}
130+
131+
Describe 'Shipped languages' {
132+
It 'all load for the report' {
133+
$reports = @(Get-LanguageKeyReport -LanguagesPath (Join-Path $PSScriptRoot '..\Config\Languages'))
134+
135+
$reports | Should -Not -BeNullOrEmpty
136+
foreach ($report in $reports) {
137+
$report.Loaded | Should -BeTrue -Because "$($report.LanguageCode) must load"
138+
}
139+
}
140+
}

‎Tools/Get-LanguageKeyReport.ps1‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<#
2+
.SYNOPSIS
3+
Lists the keys each translated language is missing or carries extra, compared to en-US.
4+
5+
.DESCRIPTION
6+
Plural-aware: a plural family in en-US (for example AppSelectionStatusSelected_one and _other) is expected in
7+
each language as one key per category that language's Get-PluralCategory rule can return, so ja-JP is not
8+
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.
9+
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.
10+
11+
.PARAMETER LanguagesPath
12+
The directory containing language folders. Defaults to Config/Languages.
13+
14+
.PARAMETER LanguageCode
15+
Limits the report to these language folders. Defaults to every folder except en-US.
16+
17+
.OUTPUTS
18+
PSCustomObject per language: LanguageCode, Loaded, ExpectedCount, MissingKeys, ExtraKeys.
19+
#>
20+
function Get-LanguageKeyReport {
21+
param(
22+
[string]$LanguagesPath = (Join-Path $PSScriptRoot '..\Config\Languages'),
23+
[string[]]$LanguageCode
24+
)
25+
26+
$baseline = Import-LanguageContent -LanguageFolder 'en-US' -LanguagesPath $LanguagesPath
27+
if (-not $baseline) {
28+
throw "Unable to load the en-US baseline from '$LanguagesPath'."
29+
}
30+
31+
$pluralPattern = '^([^.]+)_(zero|one|two|few|many|other)$'
32+
$plainKeys = @()
33+
$pluralBases = @()
34+
foreach ($key in (Get-LanguageKeyPaths -Lang $baseline)) {
35+
if ($key -match $pluralPattern) { $pluralBases += $Matches[1] } else { $plainKeys += $key }
36+
}
37+
$pluralBases = @($pluralBases | Sort-Object -Unique)
38+
39+
# Counts that reach every category of the shipped rules, including the whole-million 'many' of Portuguese.
40+
$sampleCounts = @(0..1000) + @(1000000, 1000001, 2000000)
41+
42+
if (-not $LanguageCode) {
43+
$LanguageCode = @(Get-ChildItem -LiteralPath $LanguagesPath -Directory | Where-Object Name -ne 'en-US' |
44+
Sort-Object Name | Select-Object -ExpandProperty Name)
45+
}
46+
47+
foreach ($code in $LanguageCode) {
48+
# A folder with a missing or unreadable catalog must not stop the report: list it as not loaded and go on.
49+
try {
50+
$content = Import-LanguageContent -LanguageFolder $code -LanguagesPath $LanguagesPath -ErrorAction Stop
51+
}
52+
catch {
53+
Write-Warning "Language '$code' could not be loaded: $($_.Exception.Message)"
54+
$content = $null
55+
}
56+
if (-not $content) {
57+
[PSCustomObject]@{ LanguageCode = $code; Loaded = $false; ExpectedCount = 0; MissingKeys = @(); ExtraKeys = @() }
58+
continue
59+
}
60+
61+
$categories = @($sampleCounts | ForEach-Object { Get-PluralCategory -LanguageCode $code -Count $_ } | Sort-Object -Unique)
62+
$expected = [System.Collections.Generic.HashSet[string]]::new([string[]]@(
63+
$plainKeys + @($pluralBases | ForEach-Object { $base = $_; $categories | ForEach-Object { "${base}_$_" } })
64+
))
65+
# CLDR gives every language an 'other' category (fractions), so shipping it is allowed even when the rule never returns it.
66+
$allowed = [System.Collections.Generic.HashSet[string]]::new($expected)
67+
foreach ($base in $pluralBases) { [void]$allowed.Add("${base}_other") }
68+
$actual = [System.Collections.Generic.HashSet[string]]::new([string[]](Get-LanguageKeyPaths -Lang $content))
69+
70+
[PSCustomObject]@{
71+
LanguageCode = $code
72+
Loaded = $true
73+
ExpectedCount = $expected.Count
74+
MissingKeys = @($expected | Where-Object { -not $actual.Contains($_) } | Sort-Object)
75+
ExtraKeys = @($actual | Where-Object { -not $allowed.Contains($_) } | Sort-Object)
76+
}
77+
}
78+
}

‎Tools/Report-LanguageKeys.ps1‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<#
2+
.SYNOPSIS
3+
Prints, per translated language, which keys are missing or extra compared to en-US.
4+
5+
.DESCRIPTION
6+
Informational only: a language that lags behind en-US, or whose catalogs cannot be loaded (it is listed as
7+
"could not load all catalogs"), never fails the run. It exits 1 only when the script itself breaks, for example
8+
when the en-US baseline cannot be loaded.
9+
Writes Markdown to the console and, on GitHub Actions, to the job summary.
10+
11+
.PARAMETER LanguageCode
12+
Limits the report to these language folders. Defaults to every folder except en-US.
13+
#>
14+
[CmdletBinding()]
15+
param(
16+
[string[]]$LanguageCode
17+
)
18+
19+
$ErrorActionPreference = 'Stop'
20+
$scriptsPath = Join-Path (Split-Path -Parent $PSScriptRoot) 'Scripts'
21+
. (Join-Path $scriptsPath 'FileIO\Import-JsonFile.ps1')
22+
. (Join-Path $scriptsPath 'FileIO\Import-LanguageFile.ps1')
23+
. (Join-Path $PSScriptRoot 'Get-LanguageKeyReport.ps1')
24+
25+
$reports = @(Get-LanguageKeyReport -LanguageCode $LanguageCode)
26+
27+
$lines = [System.Collections.Generic.List[string]]::new()
28+
$lines.Add('## Language key report (compared to en-US)')
29+
$lines.Add('')
30+
$lines.Add('| Language | Expected | Missing | Extra |')
31+
$lines.Add('| --- | ---: | ---: | ---: |')
32+
foreach ($report in $reports) {
33+
if ($report.Loaded) {
34+
$lines.Add("| $($report.LanguageCode) | $($report.ExpectedCount) | $($report.MissingKeys.Count) | $($report.ExtraKeys.Count) |")
35+
}
36+
else {
37+
$lines.Add("| $($report.LanguageCode) | could not load all catalogs | | |")
38+
}
39+
}
40+
41+
foreach ($report in ($reports | Where-Object { $_.MissingKeys.Count -or $_.ExtraKeys.Count })) {
42+
foreach ($kind in 'Missing', 'Extra') {
43+
$keys = @($report."${kind}Keys")
44+
if ($keys.Count -eq 0) { continue }
45+
$lines.Add('')
46+
$lines.Add("<details><summary>$($report.LanguageCode): $($keys.Count) $($kind.ToLowerInvariant())</summary>")
47+
$lines.Add('')
48+
foreach ($key in $keys) { $lines.Add("- ``$key``") }
49+
$lines.Add('')
50+
$lines.Add('</details>')
51+
}
52+
}
53+
54+
$lines | Write-Output
55+
if ($env:GITHUB_STEP_SUMMARY) {
56+
Add-Content -LiteralPath $env:GITHUB_STEP_SUMMARY -Value $lines -Encoding utf8
57+
}

0 commit comments

Comments
 (0)