|
| 1 | +#!/usr/bin/env pwsh |
| 2 | +<# |
| 3 | +.SYNOPSIS |
| 4 | + Compare two Hugo i18n YAML files and identify missing translation keys. |
| 5 | +
|
| 6 | +.DESCRIPTION |
| 7 | + This script compares two YAML translation files and identifies keys that exist in the source |
| 8 | + but are missing in the target. It can optionally add the missing keys to the target file. |
| 9 | +
|
| 10 | +.PARAMETER SourceFile |
| 11 | + Path to the source YAML file (contains the keys you want to port) |
| 12 | +
|
| 13 | +.PARAMETER TargetFile |
| 14 | + Path to the target YAML file (file to be updated) |
| 15 | +
|
| 16 | +.PARAMETER AddMissing |
| 17 | + Switch to automatically add missing keys to the target file |
| 18 | +
|
| 19 | +.PARAMETER WhatIf |
| 20 | + Show what would be added without actually modifying the target file |
| 21 | +
|
| 22 | +.EXAMPLE |
| 23 | + .\Compare-YamlTranslations.ps1 -SourceFile "module\i18n\en.yaml" -TargetFile "site\i18n\en.yaml" |
| 24 | + |
| 25 | +.EXAMPLE |
| 26 | + .\Compare-YamlTranslations.ps1 -SourceFile "module\i18n\en.yaml" -TargetFile "site\i18n\en.yaml" -AddMissing |
| 27 | + |
| 28 | +.EXAMPLE |
| 29 | + .\Compare-YamlTranslations.ps1 -SourceFile "module\i18n\en.yaml" -TargetFile "site\i18n\en.yaml" -AddMissing -WhatIf |
| 30 | +#> |
| 31 | + |
| 32 | +[CmdletBinding()] |
| 33 | +param( |
| 34 | + [Parameter(Mandatory = $true)] |
| 35 | + [string]$SourceFile, |
| 36 | + |
| 37 | + [Parameter(Mandatory = $true)] |
| 38 | + [string]$TargetFile, |
| 39 | + |
| 40 | + [Parameter(Mandatory = $false)] |
| 41 | + [switch]$AddMissing, |
| 42 | + |
| 43 | + [Parameter(Mandatory = $false)] |
| 44 | + [switch]$WhatIf |
| 45 | +) |
| 46 | + |
| 47 | +# Function to parse YAML translation file and extract key-value pairs |
| 48 | +function Parse-YamlTranslations { |
| 49 | + param([string]$FilePath) |
| 50 | + |
| 51 | + if (-not (Test-Path $FilePath)) { |
| 52 | + throw "File not found: $FilePath" |
| 53 | + } |
| 54 | + |
| 55 | + $content = Get-Content $FilePath -Raw |
| 56 | + $translations = @{} |
| 57 | + $currentEntry = @{} |
| 58 | + |
| 59 | + # Split content into lines and process |
| 60 | + $lines = $content -split "`n" |
| 61 | + |
| 62 | + foreach ($line in $lines) { |
| 63 | + $line = $line.Trim() |
| 64 | + |
| 65 | + # Skip empty lines and comments |
| 66 | + if ([string]::IsNullOrWhiteSpace($line) -or $line.StartsWith('#')) { |
| 67 | + continue |
| 68 | + } |
| 69 | + |
| 70 | + # Check for new entry (starts with - id:) |
| 71 | + if ($line -match '^-\s+id:\s*(.+)$') { |
| 72 | + # Save previous entry if it exists |
| 73 | + if ($currentEntry.id -and $currentEntry.translation) { |
| 74 | + $translations[$currentEntry.id] = $currentEntry.translation |
| 75 | + } |
| 76 | + |
| 77 | + # Start new entry |
| 78 | + $currentEntry = @{ |
| 79 | + id = $matches[1].Trim('"').Trim("'") |
| 80 | + translation = $null |
| 81 | + } |
| 82 | + } |
| 83 | + # Check for translation line |
| 84 | + elseif ($line -match '^translation:\s*(.+)$') { |
| 85 | + if ($currentEntry.id) { |
| 86 | + $translationValue = $matches[1].Trim() |
| 87 | + # Remove surrounding quotes if present |
| 88 | + if (($translationValue.StartsWith('"') -and $translationValue.EndsWith('"')) -or |
| 89 | + ($translationValue.StartsWith("'") -and $translationValue.EndsWith("'"))) { |
| 90 | + $translationValue = $translationValue.Substring(1, $translationValue.Length - 2) |
| 91 | + } |
| 92 | + $currentEntry.translation = $translationValue |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + # Don't forget the last entry |
| 98 | + if ($currentEntry.id -and $currentEntry.translation) { |
| 99 | + $translations[$currentEntry.id] = $currentEntry.translation |
| 100 | + } |
| 101 | + |
| 102 | + return $translations |
| 103 | +} |
| 104 | + |
| 105 | +# Function to format YAML entry |
| 106 | +function Format-YamlEntry { |
| 107 | + param( |
| 108 | + [string]$Id, |
| 109 | + [string]$Translation |
| 110 | + ) |
| 111 | + |
| 112 | + # Escape translation value if it contains special characters |
| 113 | + $escapedTranslation = $Translation |
| 114 | + if ($Translation -match '[:"''\\]' -or $Translation.Contains('{{') -or $Translation.Contains('}}')) { |
| 115 | + $escapedTranslation = '"' + $Translation.Replace('\', '\\').Replace('"', '\"') + '"' |
| 116 | + } |
| 117 | + else { |
| 118 | + $escapedTranslation = '"' + $Translation + '"' |
| 119 | + } |
| 120 | + |
| 121 | + return @" |
| 122 | +- id: $Id |
| 123 | + translation: $escapedTranslation |
| 124 | +
|
| 125 | +"@ |
| 126 | +} |
| 127 | + |
| 128 | +try { |
| 129 | + Write-Host "🔍 Comparing YAML translation files..." -ForegroundColor Cyan |
| 130 | + Write-Host "Source: $SourceFile" -ForegroundColor Gray |
| 131 | + Write-Host "Target: $TargetFile" -ForegroundColor Gray |
| 132 | + Write-Host "" |
| 133 | + |
| 134 | + # Parse both files |
| 135 | + Write-Host "📖 Parsing source file..." -ForegroundColor Yellow |
| 136 | + $sourceTranslations = Parse-YamlTranslations -FilePath $SourceFile |
| 137 | + Write-Host " Found $($sourceTranslations.Count) translation keys" -ForegroundColor Green |
| 138 | + |
| 139 | + Write-Host "📖 Parsing target file..." -ForegroundColor Yellow |
| 140 | + $targetTranslations = Parse-YamlTranslations -FilePath $TargetFile |
| 141 | + Write-Host " Found $($targetTranslations.Count) translation keys" -ForegroundColor Green |
| 142 | + Write-Host "" |
| 143 | + |
| 144 | + # Find missing keys |
| 145 | + $missingKeys = @() |
| 146 | + $duplicateKeys = @() |
| 147 | + $differentValues = @() |
| 148 | + |
| 149 | + foreach ($sourceKey in $sourceTranslations.Keys) { |
| 150 | + if (-not $targetTranslations.ContainsKey($sourceKey)) { |
| 151 | + $missingKeys += $sourceKey |
| 152 | + } |
| 153 | + elseif ($targetTranslations[$sourceKey] -ne $sourceTranslations[$sourceKey]) { |
| 154 | + $differentValues += $sourceKey |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + # Find keys that exist in target but not in source (potential duplicates or extras) |
| 159 | + foreach ($targetKey in $targetTranslations.Keys) { |
| 160 | + if (-not $sourceTranslations.ContainsKey($targetKey)) { |
| 161 | + $duplicateKeys += $targetKey |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + # Display results |
| 166 | + Write-Host "📊 Comparison Results:" -ForegroundColor Cyan |
| 167 | + Write-Host "=====================" -ForegroundColor Cyan |
| 168 | + |
| 169 | + if ($missingKeys.Count -gt 0) { |
| 170 | + Write-Host "❌ Missing keys in target ($($missingKeys.Count)):" -ForegroundColor Red |
| 171 | + foreach ($key in $missingKeys | Sort-Object) { |
| 172 | + Write-Host " - $key" -ForegroundColor Red |
| 173 | + Write-Host " Value: $($sourceTranslations[$key])" -ForegroundColor Gray |
| 174 | + } |
| 175 | + Write-Host "" |
| 176 | + } |
| 177 | + else { |
| 178 | + Write-Host "✅ No missing keys found!" -ForegroundColor Green |
| 179 | + Write-Host "" |
| 180 | + } |
| 181 | + |
| 182 | + if ($differentValues.Count -gt 0) { |
| 183 | + Write-Host "⚠️ Keys with different values ($($differentValues.Count)):" -ForegroundColor Yellow |
| 184 | + foreach ($key in $differentValues | Sort-Object) { |
| 185 | + Write-Host " - $key" -ForegroundColor Yellow |
| 186 | + Write-Host " Source: $($sourceTranslations[$key])" -ForegroundColor Gray |
| 187 | + Write-Host " Target: $($targetTranslations[$key])" -ForegroundColor Gray |
| 188 | + } |
| 189 | + Write-Host "" |
| 190 | + } |
| 191 | + |
| 192 | + if ($duplicateKeys.Count -gt 0) { |
| 193 | + Write-Host "ℹ️ Keys only in target ($($duplicateKeys.Count)):" -ForegroundColor Blue |
| 194 | + foreach ($key in $duplicateKeys | Sort-Object) { |
| 195 | + Write-Host " - $key" -ForegroundColor Blue |
| 196 | + } |
| 197 | + Write-Host "" |
| 198 | + } |
| 199 | + |
| 200 | + # Add missing keys if requested |
| 201 | + if ($AddMissing -and $missingKeys.Count -gt 0) { |
| 202 | + if ($WhatIf) { |
| 203 | + Write-Host "🔍 What would be added to target file:" -ForegroundColor Cyan |
| 204 | + foreach ($key in $missingKeys | Sort-Object) { |
| 205 | + $entry = Format-YamlEntry -Id $key -Translation $sourceTranslations[$key] |
| 206 | + Write-Host $entry -ForegroundColor Green |
| 207 | + } |
| 208 | + } |
| 209 | + else { |
| 210 | + Write-Host "➕ Adding missing keys to target file..." -ForegroundColor Green |
| 211 | + |
| 212 | + # Read current target content |
| 213 | + $targetContent = Get-Content $TargetFile -Raw |
| 214 | + |
| 215 | + # Add missing entries at the end |
| 216 | + $newEntries = "" |
| 217 | + foreach ($key in $missingKeys | Sort-Object) { |
| 218 | + $newEntries += Format-YamlEntry -Id $key -Translation $sourceTranslations[$key] |
| 219 | + } |
| 220 | + |
| 221 | + # Ensure there's a newline before adding new content |
| 222 | + if (-not $targetContent.EndsWith("`n")) { |
| 223 | + $targetContent += "`n" |
| 224 | + } |
| 225 | + |
| 226 | + # Add the new entries |
| 227 | + $updatedContent = $targetContent + $newEntries |
| 228 | + |
| 229 | + # Write back to file |
| 230 | + Set-Content -Path $TargetFile -Value $updatedContent -NoNewline |
| 231 | + |
| 232 | + Write-Host "✅ Successfully added $($missingKeys.Count) missing keys to $TargetFile" -ForegroundColor Green |
| 233 | + } |
| 234 | + } |
| 235 | + elseif ($missingKeys.Count -gt 0) { |
| 236 | + Write-Host "💡 To add missing keys automatically, use the -AddMissing parameter" -ForegroundColor Cyan |
| 237 | + Write-Host "💡 To see what would be added without making changes, use -AddMissing -WhatIf" -ForegroundColor Cyan |
| 238 | + } |
| 239 | + |
| 240 | +} |
| 241 | +catch { |
| 242 | + Write-Error "An error occurred: $($_.Exception.Message)" |
| 243 | + exit 1 |
| 244 | +} |
0 commit comments