Skip to content

Instantly share code, notes, and snippets.

@gptshubham595
Last active September 9, 2025 14:06
Show Gist options
  • Select an option

  • Save gptshubham595/4d7617d00cb13e06b529e90153b1f183 to your computer and use it in GitHub Desktop.

Select an option

Save gptshubham595/4d7617d00cb13e06b529e90153b1f183 to your computer and use it in GitHub Desktop.
object LocaleHelper {
fun setLocale(context: Context, languageCode: String) {
val appLocale: LocaleListCompat = LocaleListCompat.forLanguageTags(languageCode)
AppCompatDelegate.setApplicationLocales(appLocale)
val sharedPref = context.getSharedPreferences("Settings", Context.MODE_PRIVATE)
sharedPref.edit(commit = true) {
putString("language", languageCode)
}
}
fun loadLocale(context: Context) {
val sharedPref = context.getSharedPreferences("Settings", Context.MODE_PRIVATE)
val language = sharedPref.getString("language", "") ?: ""
if (language.isNotEmpty()) {
val appLocale = LocaleListCompat.forLanguageTags(language)
AppCompatDelegate.setApplicationLocales(appLocale)
}
}
fun getLanguage(context: Context): String {
val sharedPref = context.getSharedPreferences("Settings", Context.MODE_PRIVATE)
return sharedPref.getString("language", "en") ?: "en"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment