Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nfaycel/af5a5db9531b6eb5392af77948444ad1 to your computer and use it in GitHub Desktop.
Save nfaycel/af5a5db9531b6eb5392af77948444ad1 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