Add task to generate locales_config.xml (#7754)

This commit is contained in:
Andreas 2022-08-14 16:43:28 +02:00 committed by GitHub
parent 9dbc1aa7a3
commit 4291cc8eb1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 62 additions and 1 deletions

3
.gitignore vendored
View file

@ -14,3 +14,6 @@ app/**/output.json
# Hebrew assets are copied on build
app/src/main/res/values-iw/
# Generated
locales_config.xml

View file

@ -281,6 +281,8 @@ dependencies {
}
tasks {
val localesConfigTask = registerLocalesConfigTask(project)
withType<Test> {
useJUnitPlatform()
testLogging {
@ -313,7 +315,7 @@ tasks {
}
preBuild {
dependsOn(formatKotlin, copyHebrewStrings)
dependsOn(formatKotlin, copyHebrewStrings, localesConfigTask)
}
}

View file

@ -27,6 +27,7 @@
android:hardwareAccelerated="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:localeConfig="@xml/locales_config"
android:largeHeap="true"
android:requestLegacyExternalStorage="true"
android:roundIcon="@mipmap/ic_launcher_round"

View file

@ -1,6 +1,14 @@
plugins {
`kotlin-dsl`
}
dependencies {
compileOnly("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinLibs.versions.kotlin.version.get()}")
implementation(gradleApi())
}
repositories {
mavenCentral()
google()
}

View file

@ -0,0 +1,7 @@
dependencyResolutionManagement {
versionCatalogs {
create("kotlinLibs") {
from(files("../gradle/kotlinx.versions.toml"))
}
}
}

View file

@ -0,0 +1,40 @@
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.tasks.TaskProvider
import org.gradle.kotlin.dsl.TaskContainerScope
fun TaskContainerScope.registerLocalesConfigTask(project: Project): TaskProvider<Task> {
return with(project) {
register("generateLocalesConfig") {
val emptyResourcesElement = "<resources>\\s*<\\/resources>|<resources\\/>".toRegex()
val valuesPrefix = "values-?".toRegex()
val languages = fileTree("$projectDir/src/main/res/")
.matching {
include("**/strings.xml")
}
.filterNot {
it.readText().contains(emptyResourcesElement)
}
.joinToString(separator = "\n") {
val language = it.parentFile.name
.replace(valuesPrefix, "")
.takeIf(String::isNotBlank) ?: "en"
" <locale android:name=\"$language\"/>"
}
val content = """
<?xml version="1.0" encoding="utf-8"?>
<locale-config xmlns:android="http://schemas.android.com/apk/res/android">
$languages
</locale-config>
""".trimIndent()
val localeFile = file("$projectDir/src/main/res/xml/locales_config.xml")
localeFile.parentFile.mkdirs()
localeFile.writeText(content)
}
}
}