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