Skip to content

Commit e73fb55

Browse files
authored
Merge pull request libre-tube#7078 from Bnyro/master
fix: app language doesn't change on main activity
2 parents 7d0540f + e3c6f4f commit e73fb55

2 files changed

Lines changed: 20 additions & 16 deletions

File tree

app/src/main/java/com/github/libretube/helpers/LocaleHelper.kt

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.github.libretube.helpers
22

33
import android.content.Context
4-
import android.content.res.Configuration
54
import android.telephony.TelephonyManager
65
import androidx.core.content.getSystemService
76
import androidx.core.os.ConfigurationCompat
@@ -26,18 +25,6 @@ object LocaleHelper {
2625
}
2726
}
2827

29-
fun updateLanguage(context: Context) {
30-
val locale = getAppLocale()
31-
updateResources(context, locale)
32-
}
33-
34-
private fun updateResources(context: Context, locale: Locale) {
35-
Locale.setDefault(locale)
36-
val configuration: Configuration = context.resources.configuration
37-
configuration.setLocale(locale)
38-
context.createConfigurationContext(configuration)
39-
}
40-
4128
private fun getDetectedCountry(context: Context): String {
4229
return detectSIMCountry(context)
4330
?: detectNetworkCountry(context)

app/src/main/java/com/github/libretube/ui/base/BaseActivity.kt

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.libretube.ui.base
22

3+
import android.content.Context
34
import android.content.pm.ActivityInfo
45
import android.os.Bundle
56
import androidx.appcompat.app.AppCompatActivity
@@ -9,6 +10,7 @@ import com.github.libretube.helpers.LocaleHelper
910
import com.github.libretube.helpers.PreferenceHelper
1011
import com.github.libretube.helpers.ThemeHelper
1112
import com.github.libretube.helpers.WindowHelper
13+
import java.util.Locale
1214

1315
/**
1416
* Activity that applies the LibreTube theme and the in-app language
@@ -39,9 +41,6 @@ open class BaseActivity : AppCompatActivity() {
3941
ThemeHelper.updateTheme(this)
4042
if (isDialogActivity) ThemeHelper.applyDialogActivityTheme(this)
4143

42-
// set the apps language
43-
LocaleHelper.updateLanguage(this)
44-
4544
requestOrientationChange()
4645

4746
// wait for the window decor view to be drawn before detecting display cutouts
@@ -53,6 +52,24 @@ open class BaseActivity : AppCompatActivity() {
5352
super.onCreate(savedInstanceState)
5453
}
5554

55+
override fun attachBaseContext(newBase: Context?) {
56+
if (newBase == null) {
57+
super.attachBaseContext(null)
58+
return
59+
}
60+
61+
// change the locale according to the user's preference (or system language as fallback)
62+
val locale = LocaleHelper.getAppLocale()
63+
Locale.setDefault(locale)
64+
65+
val configuration = newBase.resources.configuration.apply {
66+
setLocale(locale)
67+
}
68+
val newContext = newBase.createConfigurationContext(configuration)
69+
70+
super.attachBaseContext(newContext)
71+
}
72+
5673
/**
5774
* Rotate the screen according to the app orientation preference
5875
*/

0 commit comments

Comments
 (0)