Skip to content

Commit a16f0ed

Browse files
committed
Extract languages provider into Gradle plugin
1 parent 26cd4e6 commit a16f0ed

File tree

2 files changed

+74
-3
lines changed

2 files changed

+74
-3
lines changed

app/build.gradle.kts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ plugins {
88
id("kotlin-parcelize")
99
id("com.squareup.wire")
1010
id("molly")
11+
id("signal-locales")
1112
}
1213

1314
val canonicalVersionCode = 1641
@@ -18,9 +19,7 @@ val mollyRevision = 1
1819

1920
// MOLLY: Use default debug keystore generated by Android Studio at $HOME/.android/debug.keystore
2021

21-
val languagesProvider = providers.of(LanguageListValueSource::class.java) {
22-
parameters.resDir.set(layout.projectDirectory.dir("src/main/res"))
23-
}
22+
val languagesProvider = supportedLocales.fromResDir(layout.projectDirectory.dir("src/main/res"))
2423

2524
val languagesForBuildConfigProvider = languagesProvider.map { languages ->
2625
languages.joinToString(separator = ", ") { language -> "\"$language\"" }
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import org.gradle.api.Project
2+
import org.gradle.api.file.DirectoryProperty
3+
import org.gradle.api.file.RegularFileProperty
4+
import org.gradle.api.provider.Provider
5+
import org.gradle.api.provider.ValueSource
6+
import org.gradle.api.provider.ValueSourceParameters
7+
import org.gradle.api.tasks.InputDirectory
8+
import org.gradle.api.tasks.InputFile
9+
import org.gradle.api.tasks.Optional
10+
import org.gradle.api.tasks.PathSensitive
11+
import org.gradle.api.tasks.PathSensitivity
12+
import java.io.File
13+
import java.util.Properties
14+
import kotlin.apply
15+
16+
abstract class SupportedLocalesExtension(private val project: Project) {
17+
fun fromResDir(resDir: Directory): Provider<List<String>> {
18+
return project.providers.of(LanguageListValueSource::class.java) {
19+
parameters.resDir.set(resDir)
20+
}
21+
}
22+
}
23+
24+
project.extensions.create("supportedLocales", SupportedLocalesExtension::class.java, project)
25+
26+
abstract class LanguageListValueSource : ValueSource<List<String>, LanguageListValueSource.Params> {
27+
interface Params : ValueSourceParameters {
28+
@get:InputDirectory
29+
@get:PathSensitive(PathSensitivity.RELATIVE)
30+
val resDir: DirectoryProperty
31+
}
32+
33+
override fun obtain(): List<String> {
34+
// In API 35, language codes for Hebrew and Indonesian now use the ISO 639-1 code ("he" and "id").
35+
// However, the value resources still only support the outdated code ("iw" and "in") so we have
36+
// to manually indicate that we support these languages.
37+
val updatedLanguageCodes = listOf("he", "id")
38+
39+
val resRoot = parameters.resDir.asFile.get()
40+
41+
val languages = resRoot
42+
.walkTopDown()
43+
.filter { it.isFile && it.name == "strings.xml" }
44+
.mapNotNull { stringFile -> stringFile.parentFile?.name }
45+
.map { valuesFolderName -> valuesFolderName.removePrefix("values-") }
46+
.filter { valuesFolderName -> valuesFolderName != "values" }
47+
.map { languageCode -> languageCode.replace("-r", "_") }
48+
.toList()
49+
.distinct()
50+
.sorted()
51+
52+
return languages + updatedLanguageCodes + "en"
53+
}
54+
}
55+
56+
abstract class PropertiesFileValueSource : ValueSource<Properties?, PropertiesFileValueSource.Params> {
57+
interface Params : ValueSourceParameters {
58+
@get:InputFile
59+
@get:Optional
60+
@get:PathSensitive(PathSensitivity.RELATIVE)
61+
val file: RegularFileProperty
62+
}
63+
64+
override fun obtain(): Properties? {
65+
val f: File = parameters.file.asFile.get()
66+
if (!f.exists()) return null
67+
68+
return Properties().apply {
69+
f.inputStream().use { load(it) }
70+
}
71+
}
72+
}

0 commit comments

Comments
 (0)