fix displaying in non default languages

This commit is contained in:
Milos Kozak 2023-03-21 18:38:49 +01:00
parent cf69dcf423
commit 75d34955b7
4 changed files with 8 additions and 16 deletions

View file

@ -16,8 +16,6 @@ import androidx.annotation.RawRes
import androidx.annotation.StringRes import androidx.annotation.StringRes
interface ResourceHelper { interface ResourceHelper {
fun updateContext(ctx: Context?)
fun gs(@StringRes id: Int): String fun gs(@StringRes id: Int): String
fun gs(@StringRes id: Int, vararg args: Any?): String fun gs(@StringRes id: Int, vararg args: Any?): String
fun gq(@PluralsRes id: Int, quantity: Int, vararg args: Any?): String fun gq(@PluralsRes id: Int, quantity: Int, vararg args: Any?): String

View file

@ -2,19 +2,21 @@ package info.nightscout.core.ui.locale
import android.content.Context import android.content.Context
import android.content.ContextWrapper import android.content.ContextWrapper
import android.content.res.Configuration
import android.os.LocaleList import android.os.LocaleList
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import info.nightscout.core.ui.R import info.nightscout.core.ui.R
import java.util.Locale import java.util.Locale
object LocaleHelper { object LocaleHelper {
private fun selectedLanguage(context: Context): String = private fun selectedLanguage(context: Context): String =
PreferenceManager.getDefaultSharedPreferences(context).getString(context.getString(R.string.key_language), "default") PreferenceManager.getDefaultSharedPreferences(context).getString(context.getString(R.string.key_language), "default")
?: "default" ?: "default"
// injection not possible because of use in attachBaseContext // injection not possible because of use in attachBaseContext
//SP.getString(R.string.key_language, Locale.getDefault().language) //SP.getString(R.string.key_language, Locale.getDefault().language)
private fun currentLocale(context: Context): Locale { fun currentLocale(context: Context): Locale {
val language = selectedLanguage(context) val language = selectedLanguage(context)
if (language == "default") return Locale.getDefault() if (language == "default") return Locale.getDefault()
@ -34,18 +36,13 @@ object LocaleHelper {
val locale = currentLocale(context) val locale = currentLocale(context)
Locale.setDefault(locale) Locale.setDefault(locale)
val resources = context.resources
val configuration = resources.configuration
context.createConfigurationContext(configuration)
configuration.setLocale(locale)
} }
fun wrap(ctx: Context): Context { fun wrap(ctx: Context): Context {
// no action for system default language // no action for system default language
if (selectedLanguage(ctx) == "default") return ctx if (selectedLanguage(ctx) == "default") return ctx
val res = ctx.resources val configuration = Configuration()
val configuration = res.configuration
val newLocale = currentLocale(ctx) val newLocale = currentLocale(ctx)
configuration.setLocale(newLocale) configuration.setLocale(newLocale)
val localeList = LocaleList(newLocale) val localeList = LocaleList(newLocale)

View file

@ -19,6 +19,7 @@ import androidx.appcompat.app.AppCompatDelegate
import androidx.appcompat.view.ContextThemeWrapper import androidx.appcompat.view.ContextThemeWrapper
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import info.nightscout.core.ui.getThemeColor import info.nightscout.core.ui.getThemeColor
import info.nightscout.core.ui.locale.LocaleHelper
import info.nightscout.core.utils.fabric.FabricPrivacy import info.nightscout.core.utils.fabric.FabricPrivacy
import info.nightscout.shared.interfaces.ResourceHelper import info.nightscout.shared.interfaces.ResourceHelper
import java.util.Locale import java.util.Locale
@ -29,15 +30,12 @@ import javax.inject.Inject
*/ */
class ResourceHelperImpl @Inject constructor(var context: Context, private val fabricPrivacy: FabricPrivacy) : ResourceHelper { class ResourceHelperImpl @Inject constructor(var context: Context, private val fabricPrivacy: FabricPrivacy) : ResourceHelper {
override fun updateContext(ctx: Context?) { override fun gs(@StringRes id: Int): String =
ctx?.let { context = it } context.createConfigurationContext(Configuration().apply { setLocale(LocaleHelper.currentLocale(context)) }).resources.getString(id)
}
override fun gs(@StringRes id: Int): String = context.getString(id)
override fun gs(@StringRes id: Int, vararg args: Any?): String { override fun gs(@StringRes id: Int, vararg args: Any?): String {
return try { return try {
context.getString(id, *args) context.createConfigurationContext(Configuration().apply { setLocale(LocaleHelper.currentLocale(context)) }).resources.getString(id, *args)
} catch (exception: Exception) { } catch (exception: Exception) {
val resourceName = context.resources.getResourceEntryName(id) val resourceName = context.resources.getResourceEntryName(id)
val resourceValue = context.getString(id) val resourceValue = context.getString(id)

View file

@ -33,7 +33,6 @@ open class DaggerAppCompatActivityWithResult : DaggerAppCompatActivity() {
public override fun onCreate(savedInstanceState: Bundle?) { public override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setTheme(info.nightscout.core.ui.R.style.AppTheme_NoActionBar) setTheme(info.nightscout.core.ui.R.style.AppTheme_NoActionBar)
rh.updateContext(this)
compositeDisposable.add(rxBus.toObservable(EventThemeSwitch::class.java).subscribe { compositeDisposable.add(rxBus.toObservable(EventThemeSwitch::class.java).subscribe {
recreate() recreate()