|
| 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