ar, es, fr, it, ja, ko, pt-BR, pt-PT, zh-CN, zh-TW language added
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import java.io.File
|
||||
import java.util.Properties
|
||||
|
||||
plugins {
|
||||
@@ -30,6 +31,129 @@ val hasReleaseSigning = listOf(
|
||||
releaseKeyPassword
|
||||
).all { !it.isNullOrBlank() }
|
||||
|
||||
data class SupportedLocaleRow(
|
||||
val tag: String,
|
||||
val resourceQualifier: String,
|
||||
val status: String
|
||||
)
|
||||
|
||||
val activeLocaleStatuses = setOf("complete", "partial")
|
||||
val supportedLocalesCatalogFile = layout.projectDirectory.file("src/main/res/raw/supported_locales.tsv")
|
||||
val defaultStringsFile = layout.projectDirectory.file("src/main/res/values/strings.xml")
|
||||
val generatedLocaleResDir = layout.buildDirectory.dir("generated/res/locales")
|
||||
|
||||
fun parseSupportedLocaleRows(file: File): List<SupportedLocaleRow> {
|
||||
return file.readLines(Charsets.UTF_8)
|
||||
.asSequence()
|
||||
.map { it.trim() }
|
||||
.filter { it.isNotEmpty() && !it.startsWith("#") }
|
||||
.mapNotNull { line ->
|
||||
val columns = line.split('\t')
|
||||
if (columns.size < 3 || columns[0] == "tag") {
|
||||
null
|
||||
} else {
|
||||
SupportedLocaleRow(
|
||||
tag = columns[0].trim(),
|
||||
resourceQualifier = columns[1].trim(),
|
||||
status = columns[2].trim().lowercase()
|
||||
)
|
||||
}
|
||||
}
|
||||
.toList()
|
||||
}
|
||||
|
||||
fun parseStringResourceNames(file: File): Set<String> {
|
||||
val stringNamePattern = Regex("""<string\s+name="([^"]+)"""")
|
||||
return stringNamePattern.findAll(file.readText(Charsets.UTF_8))
|
||||
.map { it.groupValues[1] }
|
||||
.toSet()
|
||||
}
|
||||
|
||||
fun rawResourceQualifierFor(row: SupportedLocaleRow): String {
|
||||
return if (row.resourceQualifier == "values") {
|
||||
"raw"
|
||||
} else {
|
||||
row.resourceQualifier.replaceFirst("values", "raw")
|
||||
}
|
||||
}
|
||||
|
||||
val verifyLocalization = tasks.register("verifyLocalization") {
|
||||
inputs.file(supportedLocalesCatalogFile)
|
||||
inputs.file(defaultStringsFile)
|
||||
inputs.dir(layout.projectDirectory.dir("src/main/res"))
|
||||
|
||||
doLast {
|
||||
val defaultStrings = parseStringResourceNames(defaultStringsFile.asFile)
|
||||
val activeRows = parseSupportedLocaleRows(supportedLocalesCatalogFile.asFile)
|
||||
.filter { it.status in activeLocaleStatuses && it.tag != "system" }
|
||||
|
||||
if (activeRows.none { it.tag == "en" }) {
|
||||
throw GradleException("supported_locales.tsv must include active English fallback locale 'en'.")
|
||||
}
|
||||
|
||||
activeRows
|
||||
.filterNot { it.resourceQualifier == "values" }
|
||||
.forEach { row ->
|
||||
val localizedStringsFile = layout.projectDirectory
|
||||
.file("src/main/res/${row.resourceQualifier}/strings.xml")
|
||||
.asFile
|
||||
if (!localizedStringsFile.exists()) {
|
||||
throw GradleException(
|
||||
"${row.tag} is marked '${row.status}' in supported_locales.tsv, " +
|
||||
"but ${row.resourceQualifier}/strings.xml does not exist."
|
||||
)
|
||||
}
|
||||
|
||||
val localizedStrings = parseStringResourceNames(localizedStringsFile)
|
||||
val missing = defaultStrings - localizedStrings
|
||||
if (missing.isNotEmpty()) {
|
||||
throw GradleException(
|
||||
"${row.resourceQualifier}/strings.xml is missing localized strings: " +
|
||||
missing.sorted().joinToString()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
activeRows
|
||||
.filter { it.status == "complete" }
|
||||
.forEach { row ->
|
||||
val privacyPolicyFile = layout.projectDirectory
|
||||
.file("src/main/res/${rawResourceQualifierFor(row)}/privacy_policy.txt")
|
||||
.asFile
|
||||
if (!privacyPolicyFile.exists()) {
|
||||
throw GradleException(
|
||||
"${row.tag} is marked 'complete' in supported_locales.tsv, " +
|
||||
"but ${rawResourceQualifierFor(row)}/privacy_policy.txt does not exist."
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val generateLocaleConfig = tasks.register("generateLocaleConfig") {
|
||||
inputs.file(supportedLocalesCatalogFile)
|
||||
outputs.file(generatedLocaleResDir.map { it.file("xml/locales_config.xml") })
|
||||
|
||||
doLast {
|
||||
val activeRows = parseSupportedLocaleRows(supportedLocalesCatalogFile.asFile)
|
||||
.filter { it.status in activeLocaleStatuses && it.tag != "system" }
|
||||
|
||||
val outputFile = generatedLocaleResDir.get().file("xml/locales_config.xml").asFile
|
||||
outputFile.parentFile.mkdirs()
|
||||
outputFile.writeText(
|
||||
buildString {
|
||||
appendLine("""<?xml version="1.0" encoding="utf-8"?>""")
|
||||
appendLine("""<locale-config xmlns:android="http://schemas.android.com/apk/res/android">""")
|
||||
activeRows.forEach { row ->
|
||||
appendLine(""" <locale android:name="${row.tag}" />""")
|
||||
}
|
||||
appendLine("</locale-config>")
|
||||
},
|
||||
Charsets.UTF_8
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
gradle.taskGraph.whenReady {
|
||||
val releaseBundleRequested = allTasks.any {
|
||||
it.path == ":app:bundleRelease" || it.name == "bundleRelease"
|
||||
@@ -112,6 +236,12 @@ android {
|
||||
}
|
||||
}
|
||||
|
||||
androidComponents {
|
||||
onVariants { variant ->
|
||||
variant.sources.res?.addStaticSourceDirectory("build/generated/res/locales")
|
||||
}
|
||||
}
|
||||
|
||||
kotlin {
|
||||
compilerOptions {
|
||||
jvmTarget.set(org.jetbrains.kotlin.gradle.dsl.JvmTarget.JVM_17)
|
||||
@@ -163,3 +293,11 @@ dependencies {
|
||||
debugImplementation("androidx.compose.ui:ui-tooling")
|
||||
debugImplementation("androidx.compose.ui:ui-test-manifest")
|
||||
}
|
||||
|
||||
tasks.named("preBuild") {
|
||||
dependsOn(generateLocaleConfig, verifyLocalization)
|
||||
}
|
||||
|
||||
tasks.named("check") {
|
||||
dependsOn(verifyLocalization)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user