|
| 1 | +#!/usr/bin/env pwsh |
| 2 | +$ErrorActionPreference = 'Stop' |
| 3 | + |
| 4 | +# Git configuration for automated commits |
| 5 | +git config user.name "github-actions[bot]" |
| 6 | +git config user.email "github-actions[bot]@users.noreply.github.com" |
| 7 | + |
| 8 | +$rawAddonId = $env:ADDON_ID |
| 9 | +if ([string]::IsNullOrWhiteSpace($rawAddonId)) { |
| 10 | + Write-Error "Failed to get addon ID." |
| 11 | + exit 1 |
| 12 | +} |
| 13 | +$addonId = $rawAddonId.Trim() |
| 14 | + |
| 15 | +# --- STEP 1: PREPARATION AND SOURCE UPDATE --- |
| 16 | + |
| 17 | +$xliffFile = "./$addonId.xliff" |
| 18 | +$mdFile = "./readme.md" |
| 19 | + |
| 20 | +if (Test-Path $mdFile) { |
| 21 | + if (Test-Path $xliffFile) { |
| 22 | + $tempXliff = [System.IO.Path]::GetTempFileName() |
| 23 | + try { |
| 24 | + Copy-Item "$addonId.xliff" $tempXliff -Force |
| 25 | + Write-Host "DEBUG: Updating XLIFF source based on readme.md..." |
| 26 | + uv run .github/scripts/markdownTranslate.py updateXliff -m $mdFile -x $tempXliff -o $xliffFile |
| 27 | + } finally { |
| 28 | + if (Test-Path $tempXliff) { |
| 29 | + Remove-Item $tempXliff -Force |
| 30 | + } |
| 31 | + } |
| 32 | + } else { |
| 33 | + Write-Host "DEBUG: XLIFF template not found. Creating new one from readme.md..." |
| 34 | + uv run .github/scripts/markdownTranslate.py generateXliff -m $mdFile -o $xliffFile |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +# Update POT file (addon interface) |
| 39 | +uv run scons pot |
| 40 | +$potFile = "$addonId.pot" |
| 41 | + |
| 42 | +# --- STEP 2: UPLOAD SOURCES TO CROWDIN --- |
| 43 | + |
| 44 | +if (Test-Path $potFile) { |
| 45 | + Write-Host "DEBUG: Uploading updated POT source to Crowdin..." |
| 46 | + ./l10nUtil.exe uploadSourceFile "$potFile" -c addon |
| 47 | +} |
| 48 | + |
| 49 | +if (Test-Path $xliffFile) { |
| 50 | + Write-Host "DEBUG: Uploading updated XLIFF source to Crowdin..." |
| 51 | + ./l10nUtil.exe uploadSourceFile "$xliffFile" -c addon |
| 52 | + git add "$xliffFile" |
| 53 | + git diff --staged --quiet |
| 54 | + if ($LASTEXITCODE -ne 0) { |
| 55 | + git commit -m "Update $xliffFile for $addonId" |
| 56 | + git push |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +# --- STEP 3: EXPORT AND PROCESS TRANSLATIONS --- |
| 61 | + |
| 62 | +Write-Host "DEBUG: Exporting translations from Crowdin..." |
| 63 | +./l10nUtil.exe exportTranslations -o _addonL10n -c addon |
| 64 | + |
| 65 | +# Ensure base directories exist |
| 66 | +New-Item -ItemType Directory -Force -Path addon/locale | Out-Null |
| 67 | +New-Item -ItemType Directory -Force -Path addon/doc | Out-Null |
| 68 | + |
| 69 | +# Load language mappings for Crowdin API calls |
| 70 | +$languageMappings = Get-Content -Raw ".github/scripts/languageMappings.json" | ConvertFrom-Json |
| 71 | + |
| 72 | +foreach ($dir in Get-ChildItem -Path "_addonL10n/$addonId" -Directory) { |
| 73 | + $langCode = $dir.Name |
| 74 | + |
| 75 | + if ($langCode -eq "en") { continue } |
| 76 | + |
| 77 | + # --- Identify codes |
| 78 | + $crowdinLang = $null |
| 79 | + |
| 80 | + # Use the ."variable" syntax to correctly read the PSCustomObject from JSON |
| 81 | + if ($languageMappings.PSObject.Properties.Name -contains $langCode) { |
| 82 | + $crowdinLang = $languageMappings."$langCode" |
| 83 | + } |
| 84 | + |
| 85 | + # Fallback: If no mapping is found, replace underscores with dashes for Crowdin compatibility |
| 86 | + if (-not $crowdinLang) { |
| 87 | + $crowdinLang = $langCode.Replace('_', '-') |
| 88 | + } |
| 89 | + |
| 90 | + # The $langCode (folder name from Crowdin) represents the local repository language code. |
| 91 | + # It matches the NVDA directory structure, so no extra mapping is needed. |
| 92 | + Write-Host "--- Processing Language: $langCode (Crowdin: $crowdinLang) ---" -ForegroundColor Cyan |
| 93 | + |
| 94 | + # Paths |
| 95 | + $remoteMd = Join-Path $dir.FullName "$addonId.md" |
| 96 | + $remoteXliff = Join-Path $dir.FullName "$addonId.xliff" |
| 97 | + $remotePo = Join-Path $dir.FullName "$addonId.po" |
| 98 | + $localMdDir = "addon/doc/$langCode" |
| 99 | + $localMd = "$localMdDir/readme.md" |
| 100 | + $localPoPath = "addon/locale/$langCode/LC_MESSAGES/nvda.po" |
| 101 | + |
| 102 | + # --- 3.1 PO FILE PROCESSING --- |
| 103 | + $poImported = $false |
| 104 | + if (Test-Path $remotePo) { |
| 105 | + Write-Host "DEBUG: Checking Remote PO progress for $crowdinLang..." |
| 106 | + uv run python .github/scripts/checkTranslation.py "$addonId.po" $crowdinLang |
| 107 | + if ($LASTEXITCODE -eq 0) { |
| 108 | + Write-Host "SUCCESS: Remote PO is valid. Importing to $localPoPath" |
| 109 | + New-Item -ItemType Directory -Force -Path (Split-Path $localPoPath) | Out-Null |
| 110 | + Move-Item $remotePo $localPoPath -Force |
| 111 | + $poImported = $true |
| 112 | + } else { |
| 113 | + Write-Host "WARNING: Remote PO progress is below threshold." |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + if (-not $poImported -and (Test-Path $localPoPath)) { |
| 118 | + Write-Host "ACTION: Uploading local legacy PO to Crowdin ($crowdinLang) as fallback." |
| 119 | + ./l10nUtil.exe uploadTranslationFile $crowdinLang "$addonId.po" $localPoPath -c addon |
| 120 | + } |
| 121 | + |
| 122 | + # --- 3.2 DOCUMENTATION PROCESSING (MD & XLIFF) --- |
| 123 | + $scoreMd = 0.0 |
| 124 | + $scoreXliff = 0.0 |
| 125 | + |
| 126 | + if (Test-Path $remoteMd) { |
| 127 | + Write-Host "DEBUG: Evaluating Remote Markdown score..." |
| 128 | + $res = uv run python .github/scripts/checkTranslation.py "$addonId.md" $crowdinLang |
| 129 | + $scoreMd = [double]($res | Select-String "mdScore=").ToString().Split("=")[1] |
| 130 | + } else { |
| 131 | + Write-Host "DEBUG: No remote Markdown file found for this language." |
| 132 | + } |
| 133 | + |
| 134 | + if (Test-Path $remoteXliff) { |
| 135 | + Write-Host "DEBUG: Evaluating Remote XLIFF score..." |
| 136 | + $res = uv run python .github/scripts/checkTranslation.py "$addonId.xliff" $crowdinLang |
| 137 | + $scoreXliff = [double]($res | Select-String "xliffScore=").ToString().Split("=")[1] |
| 138 | + } else { |
| 139 | + Write-Host "DEBUG: No remote XLIFF file found for this language." |
| 140 | + } |
| 141 | + |
| 142 | + Write-Host "DEBUG: Comparison Scores -> MD: $scoreMd | XLIFF: $scoreXliff" |
| 143 | + |
| 144 | + $threshold = 0.5 |
| 145 | + $docImported = $false |
| 146 | + |
| 147 | + if ($scoreXliff -gt $threshold -or $scoreMd -gt $threshold) { |
| 148 | + if (!(Test-Path $localMdDir)) { New-Item -ItemType Directory -Force -Path $localMdDir | Out-Null } |
| 149 | + |
| 150 | + if ($scoreXliff -ge $scoreMd) { |
| 151 | + Write-Host "SUCCESS: XLIFF is better or equal. Converting XLIFF to local MD ($langCode)..." |
| 152 | + ./l10nUtil.exe xliff2md $remoteXliff $localMd |
| 153 | + $docImported = $true |
| 154 | + } else { |
| 155 | + Write-Host "SUCCESS: Markdown is better. Importing Remote MD to local ($langCode)..." |
| 156 | + Move-Item $remoteMd $localMd -Force |
| 157 | + $docImported = $true |
| 158 | + } |
| 159 | + } else { |
| 160 | + Write-Host "WARNING: Both remote MD and XLIFF scores are below threshold ($threshold)." |
| 161 | + } |
| 162 | + |
| 163 | + if (-not $docImported -and (Test-Path $localMd)) { |
| 164 | + Write-Host "ACTION: Documentation quality too low. Uploading local MD to Crowdin ($crowdinLang) as fallback." |
| 165 | + ./l10nUtil.exe uploadTranslationFile $crowdinLang "$addonId.md" $localMd -c addon |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +# --- STEP 4: COMMIT UPDATED TRANSLATIONS --- |
| 170 | + |
| 171 | +git add addon/locale addon/doc |
| 172 | +git diff --staged --quiet |
| 173 | +if ($LASTEXITCODE -ne 0) { |
| 174 | + git commit -m "Update translations for $addonId from Crowdin (Automatic Sync)" |
| 175 | + $branch = $env:downloadTranslationsBranch |
| 176 | + git push -f origin "HEAD:$branch" |
| 177 | + Write-Host "SUCCESS: Translations committed and pushed." |
| 178 | +} else { |
| 179 | + Write-Host "DEBUG: No changes in translations to commit." |
| 180 | +} |
0 commit comments