2022-08-14 16:43:28 +02:00
|
|
|
import org.gradle.api.Project
|
|
|
|
import org.gradle.api.Task
|
|
|
|
import org.gradle.api.tasks.TaskProvider
|
|
|
|
import org.gradle.kotlin.dsl.TaskContainerScope
|
|
|
|
|
2023-03-24 22:58:29 -04:00
|
|
|
private val emptyResourcesElement = "<resources>\\s*</resources>|<resources/>".toRegex()
|
|
|
|
|
2022-08-14 16:43:28 +02:00
|
|
|
fun TaskContainerScope.registerLocalesConfigTask(project: Project): TaskProvider<Task> {
|
|
|
|
return with(project) {
|
|
|
|
register("generateLocalesConfig") {
|
2023-11-18 13:54:56 -05:00
|
|
|
val languages = fileTree("$projectDir/src/commonMain/resources/MR/")
|
2023-03-24 22:58:29 -04:00
|
|
|
.matching { include("**/strings.xml") }
|
|
|
|
.filterNot { it.readText().contains(emptyResourcesElement) }
|
2023-11-18 13:54:56 -05:00
|
|
|
.map {
|
|
|
|
it.parentFile.name
|
|
|
|
.replace("base", "en")
|
2022-08-29 13:54:02 -04:00
|
|
|
.replace("-r", "-")
|
2023-03-24 22:58:29 -04:00
|
|
|
.replace("+", "-")
|
2022-08-14 16:43:28 +02:00
|
|
|
.takeIf(String::isNotBlank) ?: "en"
|
2023-11-18 13:54:56 -05:00
|
|
|
}
|
|
|
|
.sorted()
|
|
|
|
.joinToString(separator = "\n") {
|
|
|
|
" <locale android:name=\"$it\"/>"
|
2022-08-14 16:43:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
val content = """
|
|
|
|
<?xml version="1.0" encoding="utf-8"?>
|
|
|
|
<locale-config xmlns:android="http://schemas.android.com/apk/res/android">
|
|
|
|
$languages
|
|
|
|
</locale-config>
|
|
|
|
""".trimIndent()
|
|
|
|
|
2023-11-18 13:54:56 -05:00
|
|
|
val localeFile = file("$projectDir/src/androidMain/res/xml/locales_config.xml")
|
2022-08-14 16:43:28 +02:00
|
|
|
localeFile.parentFile.mkdirs()
|
|
|
|
localeFile.writeText(content)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|