From e3c6f4fa795fb8b54cfc9642cd3a201c3e7f2389 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Wed, 5 Feb 2025 20:55:52 +0100 Subject: [PATCH] fix: app language doesn't change on main activity --- .../github/libretube/helpers/LocaleHelper.kt | 13 ----------- .../github/libretube/ui/base/BaseActivity.kt | 23 ++++++++++++++++--- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/com/github/libretube/helpers/LocaleHelper.kt b/app/src/main/java/com/github/libretube/helpers/LocaleHelper.kt index e5a9afbe2..d1724a958 100644 --- a/app/src/main/java/com/github/libretube/helpers/LocaleHelper.kt +++ b/app/src/main/java/com/github/libretube/helpers/LocaleHelper.kt @@ -1,7 +1,6 @@ package com.github.libretube.helpers import android.content.Context -import android.content.res.Configuration import android.telephony.TelephonyManager import androidx.core.content.getSystemService import androidx.core.os.ConfigurationCompat @@ -26,18 +25,6 @@ object LocaleHelper { } } - fun updateLanguage(context: Context) { - val locale = getAppLocale() - updateResources(context, locale) - } - - private fun updateResources(context: Context, locale: Locale) { - Locale.setDefault(locale) - val configuration: Configuration = context.resources.configuration - configuration.setLocale(locale) - context.createConfigurationContext(configuration) - } - private fun getDetectedCountry(context: Context): String { return detectSIMCountry(context) ?: detectNetworkCountry(context) diff --git a/app/src/main/java/com/github/libretube/ui/base/BaseActivity.kt b/app/src/main/java/com/github/libretube/ui/base/BaseActivity.kt index 96d2e49f4..bccf4ff75 100644 --- a/app/src/main/java/com/github/libretube/ui/base/BaseActivity.kt +++ b/app/src/main/java/com/github/libretube/ui/base/BaseActivity.kt @@ -1,5 +1,6 @@ package com.github.libretube.ui.base +import android.content.Context import android.content.pm.ActivityInfo import android.os.Bundle import androidx.appcompat.app.AppCompatActivity @@ -9,6 +10,7 @@ import com.github.libretube.helpers.LocaleHelper import com.github.libretube.helpers.PreferenceHelper import com.github.libretube.helpers.ThemeHelper import com.github.libretube.helpers.WindowHelper +import java.util.Locale /** * Activity that applies the LibreTube theme and the in-app language @@ -39,9 +41,6 @@ open class BaseActivity : AppCompatActivity() { ThemeHelper.updateTheme(this) if (isDialogActivity) ThemeHelper.applyDialogActivityTheme(this) - // set the apps language - LocaleHelper.updateLanguage(this) - requestOrientationChange() // wait for the window decor view to be drawn before detecting display cutouts @@ -53,6 +52,24 @@ open class BaseActivity : AppCompatActivity() { super.onCreate(savedInstanceState) } + override fun attachBaseContext(newBase: Context?) { + if (newBase == null) { + super.attachBaseContext(null) + return + } + + // change the locale according to the user's preference (or system language as fallback) + val locale = LocaleHelper.getAppLocale() + Locale.setDefault(locale) + + val configuration = newBase.resources.configuration.apply { + setLocale(locale) + } + val newContext = newBase.createConfigurationContext(configuration) + + super.attachBaseContext(newContext) + } + /** * Rotate the screen according to the app orientation preference */