Generate locales with Locator (bitfireAT/davx5#218)

* Added locator plugin

Signed-off-by: Arnau Mora <arnyminer.z@gmail.com>

* Removed old generator

Signed-off-by: Arnau Mora <arnyminer.z@gmail.com>

* Updated source sets

Signed-off-by: Arnau Mora <arnyminer.z@gmail.com>

* Added locator submodule

Signed-off-by: Arnau Mora <arnyminer.z@gmail.com>

* Removed locator submodule

Signed-off-by: Arnau Mora <arnyminer.z@gmail.com>

* Moving scripts

Signed-off-by: Arnau Mora <arnyminer.z@gmail.com>

* Update dependencies

* Added `LocatorPlugin`

Signed-off-by: Arnau Mora <arnyminer.z@gmail.com>

* Fixed locales generation

Signed-off-by: Arnau Mora <arnyminer.z@gmail.com>

* Removed buildSrc

Signed-off-by: Arnau Mora <arnyminer.z@gmail.com>

* Using plugin from Jitpack

Signed-off-by: Arnau Mora <arnyminer.z@gmail.com>

* Using plugin from Gradle

Signed-off-by: Arnau Mora <arnyminer.z@gmail.com>

* Removed comment

Signed-off-by: Arnau Mora <arnyminer.z@gmail.com>

* Remove jitpack

---------

Signed-off-by: Arnau Mora <arnyminer.z@gmail.com>
Co-authored-by: Ricki Hirner <hirner@bitfire.at>
This commit is contained in:
Arnau Mora 2023-03-02 17:39:44 +01:00 committed by Ricki Hirner
parent 89a698e7b2
commit 17fb1168cc
No known key found for this signature in database
GPG key ID: 79A019FCAAEDD3AA
4 changed files with 7 additions and 134 deletions

View file

@ -7,8 +7,7 @@ apply plugin: 'com.mikepenz.aboutlibraries.plugin'
apply plugin: 'dagger.hilt.android.plugin'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply from: 'locales.gradle'
apply plugin: 'com.arnyminerz.locator'
android {
compileSdkVersion 33
@ -36,16 +35,6 @@ android {
arg("room.schemaLocation", "$projectDir/schemas")
}
}
applicationVariants.all { variant ->
def locales = getLocales(variant.flavorName)
variant.buildConfigField "String[]", "TRANSLATION_ARRAY", "new String[]{\"" + locales.join("\",\"") + "\"}"
variant.mergedFlavor.resourceConfigurations.clear()
variant.mergedFlavor.resourceConfigurations.addAll(locales)
generateLocalesConfig(variant.flavorName, locales)
}
}
compileOptions {
@ -75,8 +64,6 @@ android {
}
sourceSets {
ose.res.srcDirs += "build/generated/res/locale-ose"
androidTest.java.srcDirs = [ "src/androidTest/java" ]
androidTest.assets.srcDirs += files("$projectDir/schemas".toString())
}
@ -187,4 +174,4 @@ dependencies {
testImplementation "com.squareup.okhttp3:mockwebserver:${versions.okhttp}"
testImplementation 'junit:junit:4.13.2'
}
}

View file

@ -1,111 +0,0 @@
import groovy.xml.MarkupBuilder
import static groovy.io.FileType.DIRECTORIES
/**
* Obtains a list of all the available locales for an specific flavor
* @since 20221123
* @param flavorDir The base directory of the flavor inside `/app/src`
* @return A list with the language codes of the locales available.
*/
Set<String> getLocalesForFlavor(File flavorDir) {
def dir = new File(flavorDir, "res")
if (!flavorDir.exists()) {
logger.warn("Tried to get locales for non-existing flavor. Directory: $flavorDir")
return new LinkedHashSet()
}
if (!dir.exists()) {
logger.warn("Tried to get locales for a flavor without strings. Directory: $dir")
return new LinkedHashSet()
}
// Initialize the list English, since it's available by default
Set<String> locales = new LinkedHashSet(['en'])
// Get all directories inside resources
logger.trace("Getting locales values directories from $dir")
dir.traverse(type: DIRECTORIES, maxDepth: 0) { file ->
// Get only values directories
def fileName = file.name
if (!fileName.startsWith("values-")) return
// Take only the values directories that contain strings
def stringsFile = new File(file, "strings.xml")
if (!stringsFile.exists()) return
// Add to the list the locale of the strings file
def langCode = fileName.substring(fileName.indexOf('-') + 1)
locales.add(langCode)
}
// Log the available locales
logger.info('Supported locales: ' + locales.join(", "))
// Return the built list
return locales
}
/**
* Obtains a list of all the available locales
* @since 20220928
* @return A list with the language codes of the locales available.
*/
Set<String> getLocales(String flavor) {
// Get all the flavor directories
def dir = new File(projectDir, "src")
// Get a list of locales for the base flavor
def mainDir = new File(dir, 'main')
logger.trace("Getting main locales ($mainDir)...")
def mainLocales = getLocalesForFlavor(mainDir)
// Get a list of locales for the davdroid directory
def davdroidDir = new File(dir, 'davdroid')
logger.trace("Getting davdroid locales ($davdroidDir)...")
def davdroidLocales = getLocalesForFlavor(davdroidDir)
// Get the current flavor
def flavorDir = new File(dir, flavor)
logger.trace("Getting locales for flavor $flavor ($flavorDir)...")
def flavorLocales = getLocalesForFlavor(flavorDir)
// Build the locales list
// We use Set for avoiding duplicates
Set<String> locales = new LinkedHashSet()
locales.addAll(mainLocales)
locales.addAll(davdroidLocales)
locales.addAll(flavorLocales)
// Log the available locales
logger.trace("Supported locales for flavor $flavor: " + locales.join(', '))
return locales
}
def generateLocalesConfig(String flavor, Set<String> locales) {
def outputDir = new File(projectDir, "build/generated/res/locale-$flavor/xml")
mkdir outputDir
logger.trace("Generating locales_config.xml...")
new File(outputDir, "locales_config.xml").withWriter { writer ->
def destXml = new MarkupBuilder(new IndentPrinter(writer, " ", true, true))
destXml.setDoubleQuotes(true)
def destXmlMkp = destXml.getMkp()
destXmlMkp.xmlDeclaration(version: "1.0", encoding: "utf-8")
destXmlMkp.comment("Generated at ${new Date()}")
destXmlMkp.yield "\r\n"
destXml."locale-config"(['xmlns:android': "http://schemas.android.com/apk/res/android"]) {
locales.forEach { locale ->
destXml."locale"("android:name": locale)
}
}
}
}
// Export getLocales and generateLocalesConfig
ext {
getLocales = this.&getLocales
generateLocalesConfig = this.&generateLocalesConfig
}

View file

@ -20,6 +20,7 @@ import androidx.preference.*
import at.bitfire.cert4android.CustomCertManager
import at.bitfire.davdroid.BuildConfig
import at.bitfire.davdroid.ForegroundService
import at.bitfire.davdroid.Locator
import at.bitfire.davdroid.R
import at.bitfire.davdroid.resource.TaskUtils
import at.bitfire.davdroid.settings.Settings
@ -255,14 +256,9 @@ class AppSettingsActivity: AppCompatActivity() {
// Start with the "System default" option on top
Settings.LANGUAGE_SYSTEM to context.getString(R.string.app_settings_language_system_default)
)
// Create another map with the languages available from TRANSLATION_ARRAY
val availableLanguages = BuildConfig.TRANSLATION_ARRAY
.map { lang ->
// Fix the language code in case there are 3-character languages
val fixedLang = resourceQualifierToLanguageTag(lang)
val locale = Locale.forLanguageTag(fixedLang)
locale.language to locale.displayName
}
// Create another map with the languages available from Locales
val availableLanguages = Locator.Locales
.map { locale -> locale.language to locale.displayName }
// Sort alphabetically by the name displayed
.sortedBy { it.second }
// Add all the available languages to the original list

View file

@ -29,6 +29,7 @@ buildscript {
classpath "com.google.dagger:hilt-android-gradle-plugin:${versions.hilt}"
classpath "com.mikepenz.aboutlibraries.plugin:aboutlibraries-plugin:${versions.aboutLibraries}"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${versions.kotlin}"
classpath 'com.arnyminerz.locator:Locator:1.0.2'
}
}