fix(locales): use buildconfig instead of generating kt file

This commit is contained in:
Ax333l
2026-01-08 23:27:02 +01:00
parent 2805ac6540
commit 72b1db9a2f
2 changed files with 30 additions and 53 deletions

View File

@@ -143,6 +143,19 @@ android {
(preRelease?.substringAfterLast('.')?.toInt() ?: 99)
}
vectorDrawables.useSupportLibrary = true
val resDir = file("src/main/res")
val locales = resDir.listFiles()
.orEmpty()
//noinspection WrongGradleMethod
.filter { it.isDirectory && it.name.matches(Regex("values-[a-z]{2}(-r[A-Z]{2})?")) }
//noinspection WrongGradleMethod
.map { it.name.removePrefix("values-").replace("-r", "-") }
.sorted()
//noinspection WrongGradleMethod
.joinToString(prefix = "{", separator = ",", postfix = "}") { "\"$it\"" }
buildConfigField("String[]", "SUPPORTED_LOCALES", locales)
}
buildTypes {
@@ -230,10 +243,8 @@ android {
buildConfig = true
}
android {
androidResources {
generateLocaleConfig = true
}
androidResources {
generateLocaleConfig = true
}
externalNativeBuild {
@@ -242,8 +253,6 @@ android {
version = "3.22.1"
}
}
sourceSets["main"].kotlin.srcDir(layout.buildDirectory.dir("generated/source/locales"))
}
kotlin {
@@ -251,46 +260,6 @@ kotlin {
}
tasks {
val generateSupportedLocales by registering {
description = "Generate list of supported locales from resource directories"
val resDir = file("src/main/res")
val outputDir = layout.buildDirectory.dir("generated/source/locales")
inputs.dir(resDir)
outputs.dir(outputDir)
doLast {
val locales = resDir.listFiles()
.orEmpty()
.filter { it.isDirectory && it.name.matches(Regex("values-[a-z]{2}(-r[A-Z]{2})?")) }
.map { it.name.removePrefix("values-").replace("-r", "-") }
.sorted()
.joinToString("\n ") { "Locale.forLanguageTag(\"$it\")," }
val output = outputDir.get().asFile.resolve("app/revanced/manager/util/GeneratedLocales.kt")
output.parentFile.mkdirs()
output.writeText(
"""
|package app.revanced.manager.util
|
|import java.util.Locale
|
|object GeneratedLocales {
| val SUPPORTED_LOCALES = listOf(
| Locale.ENGLISH,$locales
| )
|}
""".trimMargin()
)
}
}
preBuild {
dependsOn(generateSupportedLocales)
}
// Needed by gradle-semantic-release-plugin.
// Tracking: https://github.com/KengoTODA/gradle-semantic-release-plugin/issues/435.
val publish by registering {

View File

@@ -1,21 +1,22 @@
package app.revanced.manager.util
import android.app.LocaleConfig
import android.content.Context
import android.os.Build
import android.os.LocaleList
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.os.LocaleListCompat
import app.revanced.manager.BuildConfig
import java.util.Locale
object SupportedLocales {
fun getSupportedLocales(context: Context): List<Locale> {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
runCatching {
android.app.LocaleConfig(context).supportedLocales?.toList()
}.getOrNull() ?: GeneratedLocales.SUPPORTED_LOCALES
} else {
GeneratedLocales.SUPPORTED_LOCALES
}
var result: List<Locale>? = null
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) result = runCatching {
LocaleConfig(context).supportedLocales?.toList()
}.getOrNull()
return result ?: generated
}
fun getCurrentLocale(): Locale? =
@@ -29,4 +30,11 @@ object SupportedLocales {
locale.getDisplayName(locale).replaceFirstChar { it.uppercase(locale) }
private fun LocaleList.toList() = (0 until size()).map { get(it) }
private val generated by lazy {
listOf(
Locale.ENGLISH,
*BuildConfig.SUPPORTED_LOCALES.map(Locale::forLanguageTag).toTypedArray()
)
}
}