Skip to content

Commit 9a196f6

Browse files
refactor(locale): Migrate to AppCompatDelegate for language handling
This commit refactors the app's language and localization handling to use the modern `AppCompatDelegate.setApplicationLocales()` API. This change simplifies the localization logic by removing the custom `LocalizedResourcesProvider` and its caching mechanism. The key changes include: - A new `LauncherLocaleManager` object is introduced to centralize language application logic using `AppCompatDelegate`. - `LocalizedResourcesProvider` has been deleted, as its functionality is now handled by the AndroidX library. - The `getLocalizedString` and `getLocalizedStringArray` functions have been simplified to directly use the application context, removing the dependency on the old provider. - Language changes in settings now call `LauncherLocaleManager.applyAppLanguage()` instead of clearing a custom cache. - The app's language is now applied at startup in `MainActivity`. - The `Language` enum in `Constants.kt` has been streamlined by removing redundant `string()` and `value()` methods.
1 parent 84ff559 commit 9a196f6

File tree

7 files changed

+37
-116
lines changed

7 files changed

+37
-116
lines changed

app/src/main/java/com/github/droidworksstudio/common/ContextExtensions.kt

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import android.content.IntentFilter
1212
import android.content.pm.ApplicationInfo
1313
import android.content.pm.PackageManager
1414
import android.content.res.Configuration
15-
import android.content.res.Resources
1615
import android.net.Uri
1716
import android.os.BatteryManager
1817
import android.os.Bundle
@@ -309,29 +308,18 @@ fun Context.isBiometricEnabled(): Boolean {
309308
}
310309
}
311310

312-
private fun currentAppResources(): Resources {
311+
fun getLocalizedString(@StringRes resId: Int, vararg args: Any): String {
313312
val context = Mlauncher.getContext()
314-
val prefs = Prefs(context)
315-
val locale = Locale.forLanguageTag(prefs.appLanguage.locale().toString())
316-
return LocalizedResourcesProvider.getResources(context, locale)
317-
}
318-
319-
fun getLocalizedString(
320-
@StringRes stringResId: Int,
321-
vararg args: Any
322-
): String {
323-
val res = currentAppResources()
324313
return if (args.isEmpty()) {
325-
res.getString(stringResId)
314+
context.getString(resId)
326315
} else {
327-
res.getString(stringResId, *args)
316+
context.getString(resId, *args)
328317
}
329318
}
330319

331-
fun getLocalizedStringArray(
332-
@ArrayRes arrayResId: Int
333-
): Array<String> {
334-
return currentAppResources().getStringArray(arrayResId)
320+
fun getLocalizedStringArray(@ArrayRes resId: Int): Array<String> {
321+
val context = Mlauncher.getContext()
322+
return context.resources.getStringArray(resId)
335323
}
336324

337325

@@ -380,7 +368,7 @@ fun Context.requestRuntimePermission(
380368
}
381369

382370
fun Context.getCurrentTimestamp(prefs: Prefs): String {
383-
val timezone = prefs.appLanguage.timezone()
371+
val timezone = prefs.appLanguage.locale()
384372
val is24HourFormat = DateFormat.is24HourFormat(this)
385373
val best12 = DateFormat.getBestDateTimePattern(
386374
timezone,
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.github.droidworksstudio.common
2+
3+
import androidx.appcompat.app.AppCompatDelegate
4+
import com.github.droidworksstudio.mlauncher.Mlauncher
5+
import com.github.droidworksstudio.mlauncher.data.Constants
6+
import com.github.droidworksstudio.mlauncher.data.Prefs
7+
8+
object LauncherLocaleManager {
9+
fun applyAppLanguage() {
10+
val prefs = Prefs(Mlauncher.getContext())
11+
12+
val primaryLocale = prefs.appLanguage.locale()
13+
14+
val localeList = if (prefs.appLanguage == Constants.Language.System) {
15+
androidx.core.os.LocaleListCompat.getEmptyLocaleList()
16+
} else {
17+
androidx.core.os.LocaleListCompat.create(primaryLocale)
18+
}
19+
AppCompatDelegate.setApplicationLocales(localeList)
20+
}
21+
}
22+
23+

app/src/main/java/com/github/droidworksstudio/common/LocalizedResourcesProvider.kt

Lines changed: 0 additions & 40 deletions
This file was deleted.

app/src/main/java/com/github/droidworksstudio/mlauncher/MainActivity.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import androidx.lifecycle.ViewModelProvider
2121
import androidx.navigation.NavController
2222
import androidx.navigation.findNavController
2323
import com.github.droidworksstudio.common.CrashHandler
24+
import com.github.droidworksstudio.common.LauncherLocaleManager
2425
import com.github.droidworksstudio.common.showLongToast
2526
import com.github.droidworksstudio.mlauncher.data.Constants
2627
import com.github.droidworksstudio.mlauncher.data.Migration
@@ -119,6 +120,8 @@ class MainActivity : AppCompatActivity() {
119120
override fun onCreate(savedInstanceState: Bundle?) {
120121
super.onCreate(savedInstanceState)
121122

123+
LauncherLocaleManager.applyAppLanguage()
124+
122125
// Enables edge-to-edge mode
123126
enableEdgeToEdge()
124127

app/src/main/java/com/github/droidworksstudio/mlauncher/data/Constants.kt

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -187,62 +187,10 @@ object Constants {
187187
}
188188

189189
@Composable
190-
override fun string(): String {
191-
return when (this) {
192-
System -> getLocalizedString(R.string.system_default)
193-
Arabic -> getLocalizedString(R.string.lang_arabic)
194-
Czech -> getLocalizedString(R.string.lang_czech)
195-
Dutch -> getLocalizedString(R.string.lang_dutch)
196-
English -> getLocalizedString(R.string.lang_english)
197-
French -> getLocalizedString(R.string.lang_french)
198-
German -> getLocalizedString(R.string.lang_german)
199-
Hebrew -> getLocalizedString(R.string.lang_hebrew)
200-
Italian -> getLocalizedString(R.string.lang_italian)
201-
Japanese -> getLocalizedString(R.string.lang_japanese)
202-
Korean -> getLocalizedString(R.string.lang_korean)
203-
Lithuanian -> getLocalizedString(R.string.lang_lithuanian)
204-
Polish -> getLocalizedString(R.string.lang_polish)
205-
Portuguese -> getLocalizedString(R.string.lang_portuguese)
206-
Romanian -> getLocalizedString(R.string.lang_romanian)
207-
Russian -> getLocalizedString(R.string.lang_russian)
208-
Slovak -> getLocalizedString(R.string.lang_slovak)
209-
Spanish -> getLocalizedString(R.string.lang_spanish)
210-
Thai -> getLocalizedString(R.string.lang_thai)
211-
Turkish -> getLocalizedString(R.string.lang_turkish)
212-
}
213-
}
190+
override fun string(): String = getString()
214191

215192

216193
fun locale(): Locale {
217-
return Locale.forLanguageTag(value())
218-
}
219-
220-
private fun value(): String {
221-
return when (this) {
222-
System -> Locale.getDefault().language
223-
Arabic -> "ar"
224-
Czech -> "cs"
225-
Dutch -> "nl"
226-
English -> "en"
227-
French -> "fr"
228-
German -> "de"
229-
Hebrew -> "iw"
230-
Italian -> "it"
231-
Japanese -> "ja"
232-
Korean -> "ko"
233-
Lithuanian -> "lt"
234-
Polish -> "pl"
235-
Portuguese -> "pt"
236-
Romanian -> "ro"
237-
Russian -> "ru"
238-
Slovak -> "sk"
239-
Spanish -> "es"
240-
Thai -> "th"
241-
Turkish -> "tr"
242-
}
243-
}
244-
245-
fun timezone(): Locale {
246194
return Locale.forLanguageTag(zone())
247195
}
248196

app/src/main/java/com/github/droidworksstudio/mlauncher/helper/SystemUtils.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,7 @@ fun getNextAlarm(context: Context, prefs: Prefs): CharSequence {
150150
val nextAlarmClock = alarmManager.nextAlarmClock ?: return "No alarm is set."
151151

152152
val alarmTime = nextAlarmClock.triggerTime
153-
val timezone =
154-
prefs.appLanguage.timezone() // Assuming this returns a string like "America/New_York"
153+
val timezone = prefs.appLanguage.locale() // Assuming this returns a string like "America/New_York"
155154
val formattedDate = DateFormat.getBestDateTimePattern(timezone, "eeeddMMM")
156155
val best12 = DateFormat.getBestDateTimePattern(
157156
timezone,

app/src/main/java/com/github/droidworksstudio/mlauncher/ui/SettingsFragment.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ import androidx.core.os.bundleOf
5252
import androidx.lifecycle.ViewModelProvider
5353
import androidx.navigation.fragment.findNavController
5454
import com.github.droidworksstudio.common.AppLogger
55-
import com.github.droidworksstudio.common.LocalizedResourcesProvider
55+
import com.github.droidworksstudio.common.LauncherLocaleManager
5656
import com.github.droidworksstudio.common.getLocalizedString
5757
import com.github.droidworksstudio.common.isBiometricEnabled
5858
import com.github.droidworksstudio.common.isGestureNavigationEnabled
@@ -588,7 +588,7 @@ class SettingsFragment : BaseFragment() {
588588
val newLanguage = languageEntries[newLanguageIndex]
589589
selectedLanguage = newLanguage // Update state
590590
prefs.appLanguage = newLanguage // Persist in preferences
591-
LocalizedResourcesProvider.clearCache()
591+
LauncherLocaleManager.applyAppLanguage()
592592
requireActivity().recreate() // force reload with new language
593593
}
594594
}

0 commit comments

Comments
 (0)