Skip to content

Commit

Permalink
Fix language switching code (#12)
Browse files Browse the repository at this point in the history
Authored-by: Selene Feigl <[email protected]>
  • Loading branch information
sfeigl authored Oct 3, 2021
1 parent 827a54f commit c70417c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@ import de.sudoq.R
import de.sudoq.controller.SudoqCompatActivity
import de.sudoq.controller.menus.preferences.LanguageSetting
import de.sudoq.controller.menus.preferences.LanguageUtility.getConfLocale
import de.sudoq.controller.menus.preferences.LanguageUtility.loadLanguageFromLocale
import de.sudoq.controller.menus.preferences.LanguageUtility.loadLanguageFromSharedPreferences2
import de.sudoq.controller.menus.preferences.LanguageUtility.loadLanguageFromSharedPreferences
import de.sudoq.controller.menus.preferences.LanguageUtility.setConfLocale
import de.sudoq.controller.menus.preferences.LanguageUtility.storeLanguageToMemory2
import de.sudoq.controller.menus.preferences.LanguageUtility.storeLanguageToSharedPreferences
import de.sudoq.controller.menus.preferences.PlayerPreferencesActivity
import de.sudoq.controller.sudoku.SudokuActivity
import de.sudoq.model.profile.ProfileSingleton
import de.sudoq.model.profile.ProfileManager
import de.sudoq.persistence.profile.ProfileRepo
import de.sudoq.persistence.profile.ProfilesListRepo
Expand Down Expand Up @@ -62,22 +60,14 @@ class MainActivity : SudoqCompatActivity() {
/* load language from profile */

//retrieve language from profile at beginning of activity lifecycle
currentLanguageCode = loadLanguageFromSharedPreferences2(this)
val a = Locale.getDefault().language
val b = resources.configuration.locale.language
val c = currentLanguageCode!!.toStorableString()
if (currentLanguageCode!!.isSystemLanguage) {
//nothing to do
//locale is adopted automatically from device
} else {
val currentConf = getConfLocale(this)
if (currentConf != currentLanguageCode!!.language) {
//if conf != loaded language, set conf and update
setConfLocale(currentLanguageCode!!.language.name, this)
val refresh = Intent(this, this.javaClass)
finish()
this.startActivity(refresh)
}
currentLanguageCode = loadLanguageFromSharedPreferences(this)
val currentConf = getConfLocale(this)
if (currentConf != currentLanguageCode!!.language.name) {
// if conf != loaded language, set conf and update
setConfLocale(currentLanguageCode!!.language.name, this)
val refresh = Intent(this, this.javaClass)
finish()
this.startActivity(refresh)
}
Log.d("lang", "MainActivity.onCreate() set with $currentLanguageCode")
}
Expand All @@ -100,7 +90,7 @@ class MainActivity : SudoqCompatActivity() {

//load language from memory
val fromConf = getConfLocale(this)
if (fromConf != currentLanguageCode!!.language) {
if (fromConf != currentLanguageCode!!.language.name) {
//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")
Expand Down Expand Up @@ -150,9 +140,9 @@ class MainActivity : SudoqCompatActivity() {
//only adopt external change if language is set to "system language"
if (currentLanguageCode!!.isSystemLanguage) {
//adopt change
currentLanguageCode!!.language = loadLanguageFromLocale()
currentLanguageCode = loadLanguageFromSharedPreferences(this)
//store changes
storeLanguageToMemory2(this, currentLanguageCode!!)
storeLanguageToSharedPreferences(this, currentLanguageCode!!)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import android.widget.AdapterView.OnItemSelectedListener
import androidx.appcompat.widget.Toolbar
import de.sudoq.R
import de.sudoq.controller.menus.NewSudokuActivity
import de.sudoq.controller.menus.preferences.AdvancedPreferencesActivity
import de.sudoq.controller.menus.preferences.LanguageSetting.LanguageCode
import de.sudoq.model.game.GameSettings
import de.sudoq.model.profile.ProfileManager
Expand Down Expand Up @@ -108,7 +107,7 @@ class AdvancedPreferencesActivity : PreferencesActivity() {
val thishere: Activity = this

//set language
currentLanguageCode = LanguageUtility.loadLanguageFromSharedPreferences2(this)
currentLanguageCode = LanguageUtility.loadLanguageFromSharedPreferences(this)
Log.d(
"lang",
"set language to AdvancedPreferencesActivity.onCreate() after setLocaleFromMemory."
Expand All @@ -135,7 +134,7 @@ class AdvancedPreferencesActivity : PreferencesActivity() {
//enum to string(resolving system language) and set
val newCode = LanguageUtility.getLanguageFromItem(enumCode)
LanguageUtility.setConfLocale(newCode.language.name, thishere)
LanguageUtility.storeLanguageToMemory2(this@AdvancedPreferencesActivity, newCode)
LanguageUtility.storeLanguageToSharedPreferences(this@AdvancedPreferencesActivity, newCode)
//int previous = LanguageUtility.loadLanguageFromConf(AdvancedPreferencesActivity.this).name();
if (currentLanguageCode!!.language != newCode.language) {
//if we change e.g. from system(english) to english we need to store a different value but we don't need to refresh.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,63 +10,38 @@ object LanguageUtility {

val SUDOQ_SHARED_PREFS_FILE : String = "SudoqSharedPrefs";

/* save languagesetting to two values*/
fun loadLanguageFromSharedPreferences(a: Activity): LanguageSetting {
val sp = a.getSharedPreferences(SUDOQ_SHARED_PREFS_FILE, MODE_PRIVATE)
val code = sp.getString("language", "system_en")
return LanguageSetting.fromStorableString(code)
}

/* save language to enum */
@JvmStatic
fun loadLanguageFromSharedPreferences2(a: Activity): LanguageSetting {
fun loadLanguageFromSharedPreferences(a: Activity): LanguageSetting {
val sp = a.getSharedPreferences(SUDOQ_SHARED_PREFS_FILE, MODE_PRIVATE)
val code = sp.getString("language", "system")
val langEnum = LanguageCode.valueOf(code!!)
return if (langEnum == LanguageCode.system) {
LanguageSetting(loadLanguageFromLocale(), true)
if (langEnum == LanguageCode.system) {
return resolveSystemLocale()
} else {
LanguageSetting(langEnum, false)
return LanguageSetting(langEnum, false)
}
}

/**
* @param langSetting
*/
@JvmStatic
fun storeLanguageToMemory2(a: Activity, langSetting: LanguageSetting) {
fun storeLanguageToSharedPreferences(a: Activity, langSetting: LanguageSetting) {
val langEnum = if (langSetting.isSystemLanguage) LanguageCode.system else langSetting.language
val sp = a.getSharedPreferences(SUDOQ_SHARED_PREFS_FILE, MODE_PRIVATE)
sp.edit()
.putString("language", langEnum.name)
.apply()
}

/* save language to enum */
@JvmStatic
fun loadLanguageFromLocale(): LanguageCode {
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 {
fun getConfLocale(a: Activity): String {
val res = a.resources
val conf = res.configuration
val code = conf.locale.language

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)
}
return code;
}

//can be 'system'
Expand All @@ -83,7 +58,12 @@ object LanguageUtility {

fun getLanguageFromItem(i: LanguageCode): LanguageSetting {
val system = i == LanguageCode.system
val language = if (system) LanguageCode.valueOf(Locale.getDefault().language) else i
return LanguageSetting(language, system)
return if (system) resolveSystemLocale() else LanguageSetting(i, false)
}

fun resolveSystemLocale(): LanguageSetting {
val defaultCode = Locale.getDefault().language //TODO won't this always return en?
LanguageCode.values().forEach { if (it.name == defaultCode) return LanguageSetting(it, true)}
return LanguageSetting(LanguageCode.en, true)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class PlayerPreferencesActivity : PreferencesActivity() {
p.registerListener(this)

//store language at beginning of activity lifecycle
currentLanguageCode = LanguageUtility.loadLanguageFromSharedPreferences2(this)
currentLanguageCode = LanguageUtility.loadLanguageFromSharedPreferences(this)
}

override fun onResume() {
Expand All @@ -77,7 +77,7 @@ class PlayerPreferencesActivity : PreferencesActivity() {
//load language from memory
//LanguageSetting fromMemory = LanguageUtility.loadLanguageFromSharedPreferences2(this);
val fromConf = LanguageUtility.getConfLocale(this)
if (fromConf != currentLanguageCode!!.language) {
if (fromConf != currentLanguageCode!!.language.name) {
val refresh = Intent(this, this.javaClass)
finish()
this.startActivity(refresh)
Expand All @@ -95,9 +95,9 @@ class PlayerPreferencesActivity : PreferencesActivity() {
//only adopt external change if language is set to "system language"
if (currentLanguageCode!!.isSystemLanguage) {
//adopt change
currentLanguageCode!!.language = LanguageUtility.loadLanguageFromLocale()
currentLanguageCode = LanguageUtility.loadLanguageFromSharedPreferences(this)
//store changes
LanguageUtility.storeLanguageToMemory2(this, currentLanguageCode!!)
LanguageUtility.storeLanguageToSharedPreferences(this, currentLanguageCode!!)
}
}
}
Expand Down

0 comments on commit c70417c

Please sign in to comment.