mirror of
https://github.com/element-hq/element-android
synced 2024-11-28 21:48:50 +03:00
Support local script (imported from https://github.com/vector-im/riot-android/pull/3364)
This commit is contained in:
parent
118870bc41
commit
3f447df13c
1 changed files with 69 additions and 11 deletions
|
@ -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("]")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue