Merge pull request #7078 from Bnyro/master

fix: app language doesn't change on main activity
This commit is contained in:
Bnyro 2025-02-05 20:56:52 +01:00 committed by GitHub
commit e73fb55cc9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 20 additions and 16 deletions

View File

@ -1,7 +1,6 @@
package com.github.libretube.helpers package com.github.libretube.helpers
import android.content.Context import android.content.Context
import android.content.res.Configuration
import android.telephony.TelephonyManager import android.telephony.TelephonyManager
import androidx.core.content.getSystemService import androidx.core.content.getSystemService
import androidx.core.os.ConfigurationCompat 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 { private fun getDetectedCountry(context: Context): String {
return detectSIMCountry(context) return detectSIMCountry(context)
?: detectNetworkCountry(context) ?: detectNetworkCountry(context)

View File

@ -1,5 +1,6 @@
package com.github.libretube.ui.base package com.github.libretube.ui.base
import android.content.Context
import android.content.pm.ActivityInfo import android.content.pm.ActivityInfo
import android.os.Bundle import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity 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.PreferenceHelper
import com.github.libretube.helpers.ThemeHelper import com.github.libretube.helpers.ThemeHelper
import com.github.libretube.helpers.WindowHelper import com.github.libretube.helpers.WindowHelper
import java.util.Locale
/** /**
* Activity that applies the LibreTube theme and the in-app language * Activity that applies the LibreTube theme and the in-app language
@ -39,9 +41,6 @@ open class BaseActivity : AppCompatActivity() {
ThemeHelper.updateTheme(this) ThemeHelper.updateTheme(this)
if (isDialogActivity) ThemeHelper.applyDialogActivityTheme(this) if (isDialogActivity) ThemeHelper.applyDialogActivityTheme(this)
// set the apps language
LocaleHelper.updateLanguage(this)
requestOrientationChange() requestOrientationChange()
// wait for the window decor view to be drawn before detecting display cutouts // wait for the window decor view to be drawn before detecting display cutouts
@ -53,6 +52,24 @@ open class BaseActivity : AppCompatActivity() {
super.onCreate(savedInstanceState) 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 * Rotate the screen according to the app orientation preference
*/ */