fix displaying in non default languages
This commit is contained in:
parent
cf69dcf423
commit
75d34955b7
|
@ -16,8 +16,6 @@ import androidx.annotation.RawRes
|
|||
import androidx.annotation.StringRes
|
||||
|
||||
interface ResourceHelper {
|
||||
fun updateContext(ctx: Context?)
|
||||
|
||||
fun gs(@StringRes id: Int): String
|
||||
fun gs(@StringRes id: Int, vararg args: Any?): String
|
||||
fun gq(@PluralsRes id: Int, quantity: Int, vararg args: Any?): String
|
||||
|
|
|
@ -2,19 +2,21 @@ package info.nightscout.core.ui.locale
|
|||
|
||||
import android.content.Context
|
||||
import android.content.ContextWrapper
|
||||
import android.content.res.Configuration
|
||||
import android.os.LocaleList
|
||||
import androidx.preference.PreferenceManager
|
||||
import info.nightscout.core.ui.R
|
||||
import java.util.Locale
|
||||
|
||||
object LocaleHelper {
|
||||
|
||||
private fun selectedLanguage(context: Context): String =
|
||||
PreferenceManager.getDefaultSharedPreferences(context).getString(context.getString(R.string.key_language), "default")
|
||||
?: "default"
|
||||
// injection not possible because of use in attachBaseContext
|
||||
//SP.getString(R.string.key_language, Locale.getDefault().language)
|
||||
|
||||
private fun currentLocale(context: Context): Locale {
|
||||
fun currentLocale(context: Context): Locale {
|
||||
val language = selectedLanguage(context)
|
||||
if (language == "default") return Locale.getDefault()
|
||||
|
||||
|
@ -34,18 +36,13 @@ object LocaleHelper {
|
|||
|
||||
val locale = currentLocale(context)
|
||||
Locale.setDefault(locale)
|
||||
val resources = context.resources
|
||||
val configuration = resources.configuration
|
||||
context.createConfigurationContext(configuration)
|
||||
configuration.setLocale(locale)
|
||||
}
|
||||
|
||||
fun wrap(ctx: Context): Context {
|
||||
// no action for system default language
|
||||
if (selectedLanguage(ctx) == "default") return ctx
|
||||
|
||||
val res = ctx.resources
|
||||
val configuration = res.configuration
|
||||
val configuration = Configuration()
|
||||
val newLocale = currentLocale(ctx)
|
||||
configuration.setLocale(newLocale)
|
||||
val localeList = LocaleList(newLocale)
|
||||
|
|
|
@ -19,6 +19,7 @@ import androidx.appcompat.app.AppCompatDelegate
|
|||
import androidx.appcompat.view.ContextThemeWrapper
|
||||
import androidx.core.content.ContextCompat
|
||||
import info.nightscout.core.ui.getThemeColor
|
||||
import info.nightscout.core.ui.locale.LocaleHelper
|
||||
import info.nightscout.core.utils.fabric.FabricPrivacy
|
||||
import info.nightscout.shared.interfaces.ResourceHelper
|
||||
import java.util.Locale
|
||||
|
@ -29,15 +30,12 @@ import javax.inject.Inject
|
|||
*/
|
||||
class ResourceHelperImpl @Inject constructor(var context: Context, private val fabricPrivacy: FabricPrivacy) : ResourceHelper {
|
||||
|
||||
override fun updateContext(ctx: Context?) {
|
||||
ctx?.let { context = it }
|
||||
}
|
||||
|
||||
override fun gs(@StringRes id: Int): String = context.getString(id)
|
||||
override fun gs(@StringRes id: Int): String =
|
||||
context.createConfigurationContext(Configuration().apply { setLocale(LocaleHelper.currentLocale(context)) }).resources.getString(id)
|
||||
|
||||
override fun gs(@StringRes id: Int, vararg args: Any?): String {
|
||||
return try {
|
||||
context.getString(id, *args)
|
||||
context.createConfigurationContext(Configuration().apply { setLocale(LocaleHelper.currentLocale(context)) }).resources.getString(id, *args)
|
||||
} catch (exception: Exception) {
|
||||
val resourceName = context.resources.getResourceEntryName(id)
|
||||
val resourceValue = context.getString(id)
|
||||
|
|
|
@ -33,7 +33,6 @@ open class DaggerAppCompatActivityWithResult : DaggerAppCompatActivity() {
|
|||
public override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setTheme(info.nightscout.core.ui.R.style.AppTheme_NoActionBar)
|
||||
rh.updateContext(this)
|
||||
|
||||
compositeDisposable.add(rxBus.toObservable(EventThemeSwitch::class.java).subscribe {
|
||||
recreate()
|
||||
|
|
Loading…
Reference in a new issue