Skip to content

Commit

Permalink
Merge pull request #7078 from Bnyro/master
Browse files Browse the repository at this point in the history
fix: app language doesn't change on main activity
  • Loading branch information
Bnyro authored Feb 5, 2025
2 parents 7d0540f + e3c6f4f commit e73fb55
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
13 changes: 0 additions & 13 deletions app/src/main/java/com/github/libretube/helpers/LocaleHelper.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand Down
23 changes: 20 additions & 3 deletions app/src/main/java/com/github/libretube/ui/base/BaseActivity.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
*/
Expand Down

0 comments on commit e73fb55

Please sign in to comment.