build: add fixStrings gradle task to repair escaped xliff translations #477
- Commit
6ff60d9f4abbcd3115ff21de4ed846101b8e92b0- Author
- Vayun Mathur <[email protected]>
- When
- 2026-08-10 16:57 UTC (3 months ago)
- Parents
bac62fb
Diff
build.gradle.kts
diff --git a/build.gradle.kts b/build.gradle.ktsindex c1185877a7..7fc2fe771f 100644--- a/build.gradle.kts+++ b/build.gradle.kts@@ -5,4 +5,40 @@ plugins { alias(libs.plugins.android.library) apply false alias(libs.plugins.kotlin.serialization) apply false alias(libs.plugins.ksp) apply false+}++// Repairs corrupted translations contributed via Weblate where the <xliff:g> markup+// was escaped (e.g. "<xliff:g ...>%1$s</xliff:g>"). When escaped, Android+// renders the markup literally in the UI instead of only the placeholder. This task+// unwraps such occurrences back to their inner placeholder (e.g. "%1$s"). See issue #477.+tasks.register("fixStrings") {+ group = "translation"+ description = "Fixes translated strings where <xliff:g> markup was escaped and shows up literally in the UI (#477)."+ val projectRoot = rootDir+ doLast {+ val escapedXliff = Regex(+ "<\\s*xliff:g\\b.*?>(.*?)<\\s*/\\s*xliff:g\\s*>",+ RegexOption.DOT_MATCHES_ALL,+ )+ var filesChanged = 0+ var replacements = 0+ projectRoot.walkTopDown()+ .onEnter { it.name != "build" && it.name != ".git" }+ .filter { it.isFile && it.name == "strings.xml" }+ .forEach { file ->+ val original = file.readText()+ var count = 0+ val fixed = escapedXliff.replace(original) { match ->+ count+++ match.groupValues[1]+ }+ if (count > 0) {+ file.writeText(fixed)+ filesChanged+++ replacements += count+ println("fixStrings: fixed $count occurrence(s) in ${file.relativeTo(projectRoot)}")+ }+ }+ println("fixStrings: done. Fixed $replacements occurrence(s) across $filesChanged file(s).")+ } }\ No newline at end of file