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 # Hebrew assets are copied on build
app/src/main/res/values-iw/ app/src/main/res/values-iw/
# Generated
locales_config.xml

View File

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

View File

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

View File

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