Skip to content

Instantly share code, notes, and snippets.

@qamarelsafadi
Created April 3, 2023 20:30
Show Gist options
  • Save qamarelsafadi/84a53921f794755d649871b418eaeb5e to your computer and use it in GitHub Desktop.
Save qamarelsafadi/84a53921f794755d649871b418eaeb5e to your computer and use it in GitHub Desktop.
import android.app.LocaleManager
import android.content.Context
import android.os.Build
import android.os.LocaleList
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.os.LocaleListCompat
import java.util.Locale
/// Change Langauage Extenstion
fun Context.changeLocale(language: String){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
this.getSystemService(LocaleManager::class.java)
.applicationLocales = LocaleList(
Locale.forLanguageTag(
language
)
)
} else {
AppCompatDelegate.setApplicationLocales(
LocaleListCompat.forLanguageTags(
language
)
)
}
}
/// Get current locale
val currentAppLocales: String =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
context.getSystemService(LocaleManager::class.java).applicationLocales.toLanguageTags()
} else {
AppCompatDelegate.getApplicationLocales().toLanguageTags()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment