Skip to content

Commit 498bcda

Browse files
committed
fix: Language switcher - save to prefs and apply on app startup
1 parent 52ba201 commit 498bcda

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

app/src/main/java/com/appcontrolx/App.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.appcontrolx
33
import android.app.Application
44
import android.util.Log
55
import androidx.appcompat.app.AppCompatDelegate
6+
import androidx.core.os.LocaleListCompat
67
import androidx.preference.PreferenceManager
78
import com.appcontrolx.utils.Constants
89
import com.topjohnwu.superuser.Shell
@@ -44,6 +45,7 @@ class App : Application() {
4445
override fun onCreate() {
4546
super.onCreate()
4647
applyTheme()
48+
applyLanguage()
4749
Log.d(TAG, "App initialized")
4850
}
4951

@@ -52,4 +54,17 @@ class App : Application() {
5254
val theme = prefs.getInt(Constants.PREFS_THEME, AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
5355
AppCompatDelegate.setDefaultNightMode(theme)
5456
}
57+
58+
private fun applyLanguage() {
59+
val prefs = PreferenceManager.getDefaultSharedPreferences(this)
60+
val langCode = prefs.getString(Constants.PREFS_LANGUAGE, "system") ?: "system"
61+
62+
val localeList = when (langCode) {
63+
"en" -> LocaleListCompat.forLanguageTags("en")
64+
"id" -> LocaleListCompat.forLanguageTags("id")
65+
else -> LocaleListCompat.getEmptyLocaleList()
66+
}
67+
68+
AppCompatDelegate.setApplicationLocales(localeList)
69+
}
5570
}

app/src/main/java/com/appcontrolx/ui/SettingsFragment.kt

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,9 @@ class SettingsFragment : Fragment() {
101101
)
102102
val languageCodes = arrayOf("system", "en", "id")
103103

104-
// Get current from AppCompatDelegate
105-
val currentLocales = AppCompatDelegate.getApplicationLocales()
106-
val currentLang = if (currentLocales.isEmpty) "system" else currentLocales.toLanguageTags().split("-")[0]
107-
val currentIndex = languageCodes.indexOf(currentLang).coerceAtLeast(0)
104+
// Get current from prefs (more reliable)
105+
val savedLang = prefs.getString(Constants.PREFS_LANGUAGE, "system") ?: "system"
106+
val currentIndex = languageCodes.indexOf(savedLang).coerceAtLeast(0)
108107

109108
MaterialAlertDialogBuilder(requireContext())
110109
.setTitle(R.string.settings_language)
@@ -119,8 +118,10 @@ class SettingsFragment : Fragment() {
119118
}
120119

121120
private fun updateLanguageText() {
121+
// Check from prefs first (more reliable), then AppCompatDelegate
122+
val savedLang = prefs.getString(Constants.PREFS_LANGUAGE, null)
122123
val currentLocales = AppCompatDelegate.getApplicationLocales()
123-
val langTag = if (currentLocales.isEmpty) "system" else currentLocales.toLanguageTags()
124+
val langTag = savedLang ?: if (currentLocales.isEmpty) "system" else currentLocales.toLanguageTags()
124125

125126
binding.tvCurrentLanguage.text = when {
126127
langTag.startsWith("en") -> getString(R.string.language_english)
@@ -130,6 +131,9 @@ class SettingsFragment : Fragment() {
130131
}
131132

132133
private fun applyLanguage(langCode: String) {
134+
// Save to prefs for persistence
135+
prefs.edit().putString(Constants.PREFS_LANGUAGE, langCode).apply()
136+
133137
// Use AppCompatDelegate for proper locale handling (Android 13+ compatible)
134138
val localeList = when (langCode) {
135139
"en" -> LocaleListCompat.forLanguageTags("en")
@@ -138,7 +142,11 @@ class SettingsFragment : Fragment() {
138142
}
139143

140144
AppCompatDelegate.setApplicationLocales(localeList)
141-
// Activity will recreate automatically, no need for manual restart
145+
146+
// For older Android versions, we need to restart the app
147+
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.TIRAMISU) {
148+
restartApp()
149+
}
142150
}
143151

144152
private fun setupCurrentMode() {

0 commit comments

Comments
 (0)