aboutsummaryrefslogtreecommitdiff
path: root/buildSrc/src/main
diff options
context:
space:
mode:
Diffstat (limited to 'buildSrc/src/main')
-rw-r--r--buildSrc/src/main/java/CrowdinDownloadPlugin.kt39
1 files changed, 39 insertions, 0 deletions
diff --git a/buildSrc/src/main/java/CrowdinDownloadPlugin.kt b/buildSrc/src/main/java/CrowdinDownloadPlugin.kt
index 6ec6e33a..7a0f9d95 100644
--- a/buildSrc/src/main/java/CrowdinDownloadPlugin.kt
+++ b/buildSrc/src/main/java/CrowdinDownloadPlugin.kt
@@ -5,12 +5,14 @@
import de.undercouch.gradle.tasks.download.Download
import java.io.File
+import javax.xml.parsers.DocumentBuilderFactory
import org.gradle.api.GradleException
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.tasks.Copy
import org.gradle.kotlin.dsl.create
import org.gradle.kotlin.dsl.register
+import org.w3c.dom.Document
private const val EXCEPTION_MESSAGE =
"""Applying `crowdin-plugin` requires a projectName to be configured via the "crowdin" extension."""
@@ -40,6 +42,30 @@ class CrowdinDownloadPlugin : Plugin<Project> {
setDependsOn(setOf("extractCrowdin"))
from("$buildDir/translations/")
into("${projectDir}/src/")
+ setFinalizedBy(setOf("removeIncompleteStrings"))
+ }
+ tasks.register("removeIncompleteStrings") {
+ doLast {
+ val sourceSets = arrayOf("main", "nonFree")
+ for (sourceSet in sourceSets) {
+ val stringFiles = File("${projectDir}/src/$sourceSet").walkTopDown().filter { it.name == "strings.xml" }
+ val sourceFile =
+ stringFiles.firstOrNull { it.path.endsWith("values/strings.xml") }
+ ?: throw GradleException("No root strings.xml found in '$sourceSet' sourceSet")
+ val sourceDoc = parseDocument(sourceFile)
+ val baselineStringCount = countStrings(sourceDoc)
+ val threshold = 0.80 * baselineStringCount
+ stringFiles.forEach { file ->
+ if (file != sourceFile) {
+ val doc = parseDocument(file)
+ val stringCount = countStrings(doc)
+ if (stringCount < threshold) {
+ file.delete()
+ }
+ }
+ }
+ }
+ }
}
tasks.register("crowdin") {
setDependsOn(setOf("extractStrings"))
@@ -54,4 +80,17 @@ class CrowdinDownloadPlugin : Plugin<Project> {
}
}
}
+
+ private fun parseDocument(file: File): Document {
+ val dbFactory = DocumentBuilderFactory.newInstance()
+ val documentBuilder = dbFactory.newDocumentBuilder()
+ return documentBuilder.parse(file)
+ }
+
+ private fun countStrings(document: Document): Int {
+ // Normalization is beneficial for us
+ // https://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
+ document.documentElement.normalize()
+ return document.getElementsByTagName("string").length
+ }
}