Skip to content

Commit

Permalink
Add task to generate locales_config.xml (#7754)
Browse files Browse the repository at this point in the history
  • Loading branch information
ghostbear authored Aug 14, 2022
1 parent 9dbc1aa commit 4291cc8
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ app/**/output.json

# Hebrew assets are copied on build
app/src/main/res/values-iw/

# Generated
locales_config.xml
4 changes: 3 additions & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,8 @@ dependencies {
}

tasks {
val localesConfigTask = registerLocalesConfigTask(project)

withType<Test> {
useJUnitPlatform()
testLogging {
Expand Down Expand Up @@ -313,7 +315,7 @@ tasks {
}

preBuild {
dependsOn(formatKotlin, copyHebrewStrings)
dependsOn(formatKotlin, copyHebrewStrings, localesConfigTask)
}
}

Expand Down
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
8 changes: 8 additions & 0 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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()
}
7 changes: 7 additions & 0 deletions buildSrc/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
dependencyResolutionManagement {
versionCatalogs {
create("kotlinLibs") {
from(files("../gradle/kotlinx.versions.toml"))
}
}
}
40 changes: 40 additions & 0 deletions buildSrc/src/main/kotlin/LocalesConfigPlugin.kt
Original file line number Diff line number Diff line change
@@ -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)
}
}
}

0 comments on commit 4291cc8

Please sign in to comment.