Fix crash due to bad script. Tha bad value has already been fixed on Weblate.

This commit is contained in:
Benoit Marty 2020-05-28 11:27:11 +02:00
parent d1c4d4b099
commit ec1422b0f0
2 changed files with 21 additions and 9 deletions

View File

@ -21,10 +21,12 @@ import android.content.res.Configuration
import android.os.Build import android.os.Build
import androidx.core.content.edit import androidx.core.content.edit
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
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.withContext import kotlinx.coroutines.withContext
import timber.log.Timber import timber.log.Timber
import java.util.IllformedLocaleException
import java.util.Locale import java.util.Locale
/** /**
@ -38,6 +40,8 @@ object VectorLocale {
private val defaultLocale = Locale("en", "US") private val defaultLocale = Locale("en", "US")
private const val ISO_15924_LATN = "Latn"
/** /**
* The cache of supported application languages * The cache of supported application languages
*/ */
@ -189,13 +193,21 @@ object VectorLocale {
) )
} }
val list = knownLocalesSet.map { (language, country, script) -> val list = knownLocalesSet.mapNotNull { (language, country, script) ->
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
try {
Locale.Builder() Locale.Builder()
.setLanguage(language) .setLanguage(language)
.setRegion(country) .setRegion(country)
.setScript(script) .setScript(script)
.build() .build()
} catch (exception: IllformedLocaleException) {
if (BuildConfig.DEBUG) {
throw exception
}
// Ignore this locale in production
null
}
} else { } else {
Locale(language, country) Locale(language, country)
} }
@ -218,7 +230,7 @@ object VectorLocale {
append(locale.getDisplayLanguage(locale)) append(locale.getDisplayLanguage(locale))
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP
&& locale.script != "Latn" && locale.script != ISO_15924_LATN
&& locale.getDisplayScript(locale).isNotEmpty()) { && locale.getDisplayScript(locale).isNotEmpty()) {
append(" - ") append(" - ")
append(locale.getDisplayScript(locale)) append(locale.getDisplayScript(locale))
@ -242,7 +254,7 @@ object VectorLocale {
return buildString { return buildString {
append("[") append("[")
append(locale.displayLanguage) append(locale.displayLanguage)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && locale.script != "Latn") { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && locale.script != ISO_15924_LATN) {
append(" - ") append(" - ")
append(locale.displayScript) append(locale.displayScript)
} }

View File

@ -5,7 +5,7 @@
<string name="resources_language">en</string> <string name="resources_language">en</string>
<string name="resources_country_code">US</string> <string name="resources_country_code">US</string>
<!-- NOTE TO TRANSLATORS: ONLY POSSIBLE VALUES: Latn OR Cyrl --> <!-- NOTE TO TRANSLATORS: Value MUST have 4 letters and MUST be in this list: https://www.unicode.org/iso15924/iso15924-codes.html. Example: "Arab", "Cyrl", "Latn", etc. -->
<string name="resources_script">Latn</string> <string name="resources_script">Latn</string>
<!-- theme --> <!-- theme -->