mirror of
https://github.com/meshtastic/Meshtastic-Android.git
synced 2026-04-03 21:53:55 -04:00
#13 add an option to force language X
This commit is contained in:
40
app/src/main/java/com/geeksville/mesh/BaseActivity.kt
Normal file
40
app/src/main/java/com/geeksville/mesh/BaseActivity.kt
Normal file
@@ -0,0 +1,40 @@
|
||||
package com.geeksville.mesh
|
||||
|
||||
import android.content.Context
|
||||
import android.os.Build
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import com.geeksville.android.Logging
|
||||
import com.geeksville.mesh.model.UIViewModel
|
||||
import java.util.*
|
||||
|
||||
open class BaseActivity: AppCompatActivity(), Logging {
|
||||
|
||||
override fun attachBaseContext(newBase: Context) {
|
||||
val res = newBase.resources
|
||||
val config = res.configuration
|
||||
|
||||
// get chosen language from preference
|
||||
val prefs = UIViewModel.getPreferences(newBase)
|
||||
val langCode = prefs.getString("lang","zz")
|
||||
debug("langCode is $langCode")
|
||||
|
||||
if (Build.VERSION.SDK_INT >= 17) {
|
||||
val locale = if (langCode == "zz")
|
||||
Locale.getDefault()
|
||||
else
|
||||
Locale(langCode)
|
||||
config.setLocale(locale)
|
||||
|
||||
if(Build.VERSION.SDK_INT > 24) {
|
||||
//Using createNewConfigurationContext will cause CompanionDeviceManager to crash
|
||||
applyOverrideConfiguration(config)
|
||||
super.attachBaseContext(newBase)
|
||||
}else {
|
||||
super.attachBaseContext(newBase.createConfigurationContext(config))
|
||||
}
|
||||
} else {
|
||||
super.attachBaseContext(newBase)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -118,7 +118,7 @@ eventually:
|
||||
val utf8 = Charset.forName("UTF-8")
|
||||
|
||||
@AndroidEntryPoint
|
||||
class MainActivity : AppCompatActivity(), Logging,
|
||||
class MainActivity : BaseActivity(), Logging,
|
||||
ActivityCompat.OnRequestPermissionsResultCallback {
|
||||
|
||||
companion object {
|
||||
@@ -1079,6 +1079,10 @@ class MainActivity : AppCompatActivity(), Logging,
|
||||
chooseThemeDialog()
|
||||
return true
|
||||
}
|
||||
R.id.preferences_language -> {
|
||||
chooseLangDialog()
|
||||
return true
|
||||
}
|
||||
else -> super.onOptionsItemSelected(item)
|
||||
}
|
||||
}
|
||||
@@ -1167,4 +1171,30 @@ class MainActivity : AppCompatActivity(), Logging,
|
||||
}
|
||||
}
|
||||
|
||||
private fun chooseLangDialog() {
|
||||
|
||||
/// Prepare dialog and its items
|
||||
val builder = MaterialAlertDialogBuilder(this)
|
||||
builder.setTitle(getString(R.string.preferences_language))
|
||||
|
||||
val languageLabels by lazy { resources.getStringArray(R.array.language_entries) }
|
||||
val languageValues by lazy { resources.getStringArray(R.array.language_values) }
|
||||
|
||||
/// Load preferences and its value
|
||||
val prefs = UIViewModel.getPreferences(this)
|
||||
val editor: SharedPreferences.Editor = prefs.edit()
|
||||
val lang = prefs.getString("lang", "zz")
|
||||
debug("Lang from prefs: $lang")
|
||||
|
||||
builder.setSingleChoiceItems(languageLabels, languageValues.indexOf(lang)) { dialog, which ->
|
||||
var lang = languageValues[which];
|
||||
debug("Set lang pref to $lang")
|
||||
editor.putString("lang", lang)
|
||||
editor.apply()
|
||||
dialog.dismiss()
|
||||
}
|
||||
val dialog = builder.create()
|
||||
dialog.show()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user