This commit is contained in:
Benoit Marty 2019-10-30 16:59:31 +01:00
parent 118870bc41
commit 3f447df13c

View file

@ -21,12 +21,16 @@ import android.content.res.Configuration
import android.os.Build import android.os.Build
import android.preference.PreferenceManager import android.preference.PreferenceManager
import androidx.core.content.edit import androidx.core.content.edit
import im.vector.riotx.BuildConfig
import im.vector.riotx.R import im.vector.riotx.R
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import timber.log.Timber import timber.log.Timber
import java.util.Locale import java.util.Locale
import kotlin.Comparator
import kotlin.collections.ArrayList
import kotlin.collections.HashSet
/** /**
* Object to manage the Locale choice of the user * Object to manage the Locale choice of the user
@ -35,6 +39,7 @@ object VectorLocale {
private const val APPLICATION_LOCALE_COUNTRY_KEY = "APPLICATION_LOCALE_COUNTRY_KEY" private const val APPLICATION_LOCALE_COUNTRY_KEY = "APPLICATION_LOCALE_COUNTRY_KEY"
private const val APPLICATION_LOCALE_VARIANT_KEY = "APPLICATION_LOCALE_VARIANT_KEY" private const val APPLICATION_LOCALE_VARIANT_KEY = "APPLICATION_LOCALE_VARIANT_KEY"
private const val APPLICATION_LOCALE_LANGUAGE_KEY = "APPLICATION_LOCALE_LANGUAGE_KEY" private const val APPLICATION_LOCALE_LANGUAGE_KEY = "APPLICATION_LOCALE_LANGUAGE_KEY"
private const val APPLICATION_LOCALE_SCRIPT_KEY = "APPLICATION_LOCALE_SCRIPT_KEY"
private val defaultLocale = Locale("en", "US") private val defaultLocale = Locale("en", "US")
@ -106,6 +111,15 @@ object VectorLocale {
} else { } else {
putString(APPLICATION_LOCALE_VARIANT_KEY, variant) putString(APPLICATION_LOCALE_VARIANT_KEY, variant)
} }
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
val script = locale.script
if (script.isEmpty()) {
remove(APPLICATION_LOCALE_SCRIPT_KEY)
} else {
putString(APPLICATION_LOCALE_SCRIPT_KEY, script)
}
}
} }
} }
@ -159,25 +173,44 @@ object VectorLocale {
* @param context the context * @param context the context
*/ */
private fun initApplicationLocales(context: Context) { private fun initApplicationLocales(context: Context) {
val knownLocalesSet = HashSet<Pair<String, String>>() val knownLocalesSet = HashSet<Triple<String, String, String>>()
try { try {
val availableLocales = Locale.getAvailableLocales() val availableLocales = Locale.getAvailableLocales()
for (locale in availableLocales) { for (locale in availableLocales) {
knownLocalesSet.add(Pair(getString(context, locale, R.string.resources_language), knownLocalesSet.add(
getString(context, locale, R.string.resources_country_code))) Triple(
getString(context, locale, R.string.resources_language),
getString(context, locale, R.string.resources_country_code),
getString(context, locale, R.string.resources_script)
)
)
} }
} catch (e: Exception) { } catch (e: Exception) {
Timber.e(e, "## getApplicationLocales() : failed") Timber.e(e, "## getApplicationLocales() : failed")
knownLocalesSet.add(Pair(context.getString(R.string.resources_language), context.getString(R.string.resources_country_code))) knownLocalesSet.add(
Triple(
context.getString(R.string.resources_language),
context.getString(R.string.resources_country_code),
context.getString(R.string.resources_script)
)
)
} }
supportedLocales.clear() supportedLocales.clear()
knownLocalesSet.mapTo(supportedLocales) { (language, country) -> knownLocalesSet.mapTo(supportedLocales) { (language, country, script) ->
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Locale.Builder()
.setLanguage(language)
.setRegion(country)
.setScript(script)
.build()
} else {
Locale(language, country) Locale(language, country)
} }
}
// sort by human display names // sort by human display names
supportedLocales.sortWith(Comparator { lhs, rhs -> localeToLocalisedString(lhs).compareTo(localeToLocalisedString(rhs)) }) supportedLocales.sortWith(Comparator { lhs, rhs -> localeToLocalisedString(lhs).compareTo(localeToLocalisedString(rhs)) })
@ -190,12 +223,37 @@ object VectorLocale {
* @return the string * @return the string
*/ */
fun localeToLocalisedString(locale: Locale): String { fun localeToLocalisedString(locale: Locale): String {
var res = locale.getDisplayLanguage(locale) return buildString {
append(locale.getDisplayLanguage(locale))
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP
&& locale.script != "Latn"
&& locale.getDisplayScript(locale).isNotEmpty()) {
append(" - ")
append(locale.getDisplayScript(locale))
}
if (locale.getDisplayCountry(locale).isNotEmpty()) { if (locale.getDisplayCountry(locale).isNotEmpty()) {
res += " (" + locale.getDisplayCountry(locale) + ")" append(" (")
append(locale.getDisplayCountry(locale))
append(")")
} }
return res // In debug mode, also display information about the locale in the current locale.
if (BuildConfig.DEBUG) {
append("\n[")
append(locale.displayLanguage)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && locale.script != "Latn") {
append(" - ")
append(locale.displayScript)
}
if (locale.displayCountry.isNotEmpty()) {
append(" (")
append(locale.displayCountry)
append(")")
}
append("]")
}
}
} }
} }