feat(translations): add a report of missing and extra language keys (#860)

This commit is contained in:
HetCreep
2026-11-10 23:34:30 +01:00
committed by Jeffrey
parent 6797d6e462
commit 39d1127f91
5 changed files with 291 additions and 0 deletions
+78
View File
@@ -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
}
$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)
}
}
}
+57
View File
@@ -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
}