Skip to content

Commit

Permalink
fix bug where it won't start when locale is IT_it or PL_pl and onResu…
Browse files Browse the repository at this point in the history
…me is called. (in tests of play console)
  • Loading branch information
timo-a committed Sep 11, 2021
1 parent 265fbe5 commit 4d25388
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ class MainActivity : SudoqCompatActivity() {
//load language from memory
val fromConf = getConfLocale(this)
if (fromConf != currentLanguageCode!!.language) {
//language has been changed so we need to restart this activity
//so that the new language is applied to it
Log.d("lang", "refresh because $currentLanguageCode -> $fromConf")
val refresh = Intent(this, this.javaClass)
finish()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,20 @@ class LanguageSetting {
}

//may never be system!
@JvmField
var language: LanguageCode
get() {
if (language == LanguageCode.system)
throw java.lang.IllegalStateException("LanguageSetting.language is 'system'. This is not allowed")
else
return language
}
set(value) {
if (value == LanguageCode.system)
throw java.lang.IllegalArgumentException("LanguageSetting.language may not be set to 'system'.")

language = value
}

var isSystemLanguage: Boolean
private set

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,31 @@ object LanguageUtility {
/* save language to enum */
@JvmStatic
fun loadLanguageFromLocale(): LanguageCode {
val code = Locale.getDefault().language
val code = Locale.getDefault().language //TODO won't this always return en?
LanguageCode.values().forEach { if (it.name == code) return it}
return LanguageCode.en
}

//can be 'system'
@JvmStatic
fun getConfLocale(a: Activity): LanguageCode {
val res = a.resources
val conf = res.configuration
val code = conf.locale.language
return LanguageCode.valueOf(code)

val unmaintainedLang = !LanguageCode.values()
.map { it.name }
.contains(code)

return if (unmaintainedLang) {
Log.e("lang","Inexplicably, the language loaded from conf is $code, defaulting to 'system'")
LanguageCode.system
} else{
LanguageCode.valueOf(code)
}
}

//can be 'system'
@JvmStatic
fun setConfLocale(lang: String, a: Activity) {
Log.i("lang", "setLocale( " + lang + ", " + a.javaClass.simpleName + ")")
Expand Down

0 comments on commit 4d25388

Please sign in to comment.