From 582e0f19b331f1359539fc2c95cccf17b647d414 Mon Sep 17 00:00:00 2001 From: Ahmad Ansori Palembani Date: Mon, 24 Jun 2024 14:20:24 +0700 Subject: [PATCH] enhance(i18n/android): Dynamically generate `locales_config.xml` --- .../src/main/kotlin/LocalesConfigPlugin.kt | 30 +++++----- i18n/build.gradle.kts | 9 ++- .../androidMain/res/xml/locales_config.xml | 56 ------------------- 3 files changed, 17 insertions(+), 78 deletions(-) delete mode 100644 i18n/src/androidMain/res/xml/locales_config.xml diff --git a/buildSrc/src/main/kotlin/LocalesConfigPlugin.kt b/buildSrc/src/main/kotlin/LocalesConfigPlugin.kt index 9e5976779a..a2aacc1d6a 100644 --- a/buildSrc/src/main/kotlin/LocalesConfigPlugin.kt +++ b/buildSrc/src/main/kotlin/LocalesConfigPlugin.kt @@ -3,28 +3,24 @@ import org.gradle.api.Task import org.gradle.api.tasks.TaskProvider import org.gradle.kotlin.dsl.TaskContainerScope +private val emptyResourcesElement = "\\s*|".toRegex() + fun TaskContainerScope.registerLocalesConfigTask(project: Project): TaskProvider { return with(project) { register("generateLocalesConfig") { - val emptyResourcesElement = "\\s*|".toRegex() - val valuesPrefix = "values-?".toRegex() - - println(projectDir) - val languages = fileTree("$projectDir/src/main/res/") - .matching { - include("**/strings.xml") + val languages = fileTree("$projectDir/src/commonMain/moko-resources/") + .matching { include("**/strings.xml") } + .filterNot { it.readText().contains(emptyResourcesElement) } + .map { + it.parentFile.name + .replace("base", "en") + .replace("-r", "-") + .replace("+", "-") + .takeIf(String::isNotBlank) ?: "en" } - .filterNot { - it.readText().contains(emptyResourcesElement) - } - .map { it.parentFile.name } .sorted() .joinToString(separator = "\n") { - val language = it - .replace(valuesPrefix, "") - .replace("-r", "-") - .takeIf(String::isNotBlank) ?: "en" - " " + " " } val content = """ @@ -34,7 +30,7 @@ $languages """.trimIndent() - val localeFile = file("$projectDir/src/main/res/xml/locales_config.xml") + val localeFile = file("$projectDir/src/androidMain/res/xml/locales_config.xml") localeFile.parentFile.mkdirs() localeFile.writeText(content) } diff --git a/i18n/build.gradle.kts b/i18n/build.gradle.kts index 9188586bba..ef5251e777 100644 --- a/i18n/build.gradle.kts +++ b/i18n/build.gradle.kts @@ -32,11 +32,10 @@ multiplatformResources { } tasks { - // FIXME: Migrate fully to MR -// val localesConfigTask = registerLocalesConfigTask(project) -// preBuild { -// dependsOn(localesConfigTask) -// } + val localesConfigTask = registerLocalesConfigTask(project) + preBuild { + dependsOn(localesConfigTask) + } withType { compilerOptions.freeCompilerArgs.addAll( diff --git a/i18n/src/androidMain/res/xml/locales_config.xml b/i18n/src/androidMain/res/xml/locales_config.xml deleted file mode 100644 index 7d6a19914d..0000000000 --- a/i18n/src/androidMain/res/xml/locales_config.xml +++ /dev/null @@ -1,56 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -