[]
Android 13
API API
LocaleManagersetApplicationLocalesgetApplicationLocalesAndroid AndroidX API Android 12API 32 API AppCompatActivity Appcompat 1.6.0 API
|
|
|
XML
android:localeConfig res/xml/locale_config.xml
Android Studio Giraffe AGP 8.1 AGP LocaleConfig AGP res LocaleConfig LocaleConfig
Android 13API 33 compileSdkVersion 33 Android API
-
build.gradle.ktsGroovybuild.gradleandroidResources {}generateLocaleConfig
Kotlin
android {
androidResources {
generateLocaleConfig = true
}
}
Groovy
android {
androidResources {
generateLocaleConfig true
}
}
LocaleConfig LocaleConfig
unqualifiedResLocale=en-US
android:localeConfig
Android 13 locale_config XML android:localeConfig android:localeConfig
res/xml/locale_config.xmlres/values/strings.xmllocale_config.xml<?xml version="1.0" encoding="utf-8"?> <locale-config xmlns:android="http://schemas.android.com/apk/res/android"> <locale android:name="en-US"/> <locale android:name="en-GB"/> <locale android:name="fr"/> <locale android:name="ja"/> <locale android:name="zh-Hans-MO"/> <locale android:name="zh-Hant-MO"/> </locale-config>-
<manifest> ... <application ... android:localeConfig="@xml/locale_config"> </application> </manifest>
LocaleManager.setOverrideLocaleConfig localeConfig Android A/B
//For setOverrideLocaleConfig
val localeManager = applicationContext
.getSystemService(LocaleManager::class.java)
localeManager.overrideLocaleConfig = LocaleConfig(
LocaleList.forLanguageTags("en-US,ja-JP,zh-Hans-SG")
)
//For getOverrideLocaleConfig
// The app calls the API to get the override LocaleConfig
val overrideLocaleConfig = localeManager.overrideLocaleConfig
// If the returned overrideLocaleConfig isn't equal to NULL, then the app calls the API to get the supported Locales
val supportedLocales = overrideLocaleConfig.supportedLocales()
IME LocaleManager.getApplicationLocales
val currentAppLocales: LocaleList = applicationContext.getSystemService(LocaleManager::class.java).getApplicationLocales(appPackageName)
Gradle
build.gradle resourceConfigurations
android {
...
defaultConfig {
resourceConfigurations += ["en", "en-rGB", "fr", "ja", "b+zh+Hans+MO", "b+zh+Hant+MO"]
}
}
> > > >
> >>
API API
Jetpack Compose
Jetpack Compose API Compose stringResource
Android 12API 32 AndroidX (AppCompatDelegate) Compose activity AppCompatActivity API
import androidx.appcompat.app.AppCompatDelegate
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.core.os.LocaleListCompat
@Composable
fun LanguageSelector() {
// Retrieve the currently configured app locale.
// If no app-specific locale is set, LocaleListCompat.get(0) returns null,
// so we safely fall back to a default (e.g., "en").
val appLocales = AppCompatDelegate.getApplicationLocales()
val currentLocaleTag = appLocales.get(0)?.toLanguageTag() ?: "en"
// Example UI: A button to toggle between English and Spanish
Button(
onClick = {
val newLanguageTag = if (currentLocaleTag == "en") "es" else "en"
val localeList = LocaleListCompat.forLanguageTags(newLanguageTag)
// Setting the locale re-creates the Activity by default,
// which automatically applies the new configuration to Compose.
AppCompatDelegate.setApplicationLocales(localeList)
}
) {
Text(
text = if (currentLocaleTag == "en") "Switch to Spanish" else "Switch to English"
)
}
}
Compose setApplicationLocales AppCompatActivity activity
val appLocale: LocaleListCompat = LocaleListCompat.forLanguageTags("xx-YY")
// Call this on the main thread as it may require Activity.restart()
AppCompatDelegate.setApplicationLocales(appLocale)
setApplicationLocales Activity
AppCompatDelegate.getApplicationLocales
Android 12
Android 12API 32 AppLocalesMetadataHolderService autoStoreLocales true android:enabled false AndroidX
<application
...
<service
android:name="androidx.appcompat.app.AppLocalesMetadataHolderService"
android:enabled="false"
android:exported="false">
<meta-data
android:name="autoStoreLocales"
android:value="true" />
</service>
...
</application>
autoStoreLocales true StrictMode diskRead diskWrite AppCompatDelegate.setApplicationLocales
autoStoreLocales false Android 12API 32 activity onCreate AppCompatDelegate.setApplicationLocales
autoStoreLocales Android 13 AppCompatDelegate.setApplicationLocales
Android API
AndroidX Android 13 Android setApplicationLocales getApplicationLocales
LocaleManager
import android.app.LocaleManager
import android.content.Context
import android.os.Build
import android.os.LocaleList
import androidx.annotation.RequiresApi
import java.util.Locale
@RequiresApi(Build.VERSION_CODES.TIRAMISU)
fun setAppLanguage(context: Context, languageTag: String) {
// 1. Retrieve the system service
val localeManager = context.getSystemService(LocaleManager::class.java)
// 2. Create a LocaleList from the language tag (e.g., "es-ES" or "ja")
val localeList = LocaleList(Locale.forLanguageTag(languageTag))
// 3. Set the locale. The system automatically updates the locale and
// restarts the app, including any necessary configuration updates.
localeManager.applicationLocales = localeList
}
@RequiresApi(Build.VERSION_CODES.TIRAMISU)
fun getAppLanguage(context: Context): String {
val localeManager = context.getSystemService(LocaleManager::class.java)
val currentLocales = localeManager.applicationLocales
// Return the primary app locale, or fall back to the system default
return if (!currentLocales.isEmpty) {
currentLocales.get(0).toLanguageTag()
} else {
Locale.getDefault().toLanguageTag()
}
}
intent
intent Speech Recognizer API EXTRA_LANGUAGE
Chrome Accept-Language
Browser.EXTRA_HEADERS Accept-Language Chrome
AndroidManifest.xml android:localeConfig
android:localeConfig LocaleListCompat.getEmptyLocaleList LocaleList.getEmptyLocaleList
// Use the AndroidX APIs to reset to the system locale for backward and forward compatibility
AppCompatDelegate.setApplicationLocales(
LocaleListCompat.getEmptyLocaleList()
)
// Or use the Framework APIs for Android 13 and above to reset to the system locale
val context = LocalContext.current
context.getSystemService(LocaleManager::class.java)
.applicationLocales = LocaleList.getEmptyLocaleList()
locale_config.xml
Android Android (AOSP) locale_config.xml locale_config.xml
<?xml version="1.0" encoding="utf-8"?>
<locale-config xmlns:android="http://schemas.android.com/apk/res/android">
<locale android:name="af"/> <!-- Afrikaans -->
<locale android:name="am"/> <!-- Amharic -->
<locale android:name="ar"/> <!-- Arabic -->
<locale android:name="as"/> <!-- Assamese -->
<locale android:name="az"/> <!-- Azerbaijani -->
<locale android:name="be"/> <!-- Belarusian -->
<locale android:name="bg"/> <!-- Bulgarian -->
<locale android:name="bn"/> <!-- Bengali -->
<locale android:name="bs"/> <!-- Bosnian -->
<locale android:name="ca"/> <!-- Catalan -->
<locale android:name="cs"/> <!-- Czech -->
<locale android:name="da"/> <!-- Danish -->
<locale android:name="de"/> <!-- German -->
<locale android:name="el"/> <!-- Greek -->
<locale android:name="en-AU"/> <!-- English (Australia) -->
<locale android:name="en-CA"/> <!-- English (Canada) -->
<locale android:name="en-GB"/> <!-- English (United Kingdom) -->
<locale android:name="en-IN"/> <!-- English (India) -->
<locale android:name="en-US"/> <!-- English (United States) -->
<locale android:name="es"/> <!-- Spanish (Spain) -->
<locale android:name="es-US"/> <!-- Spanish (United States) -->
<locale android:name="et"/> <!-- Estonian -->
<locale android:name="eu"/> <!-- Basque -->
<locale android:name="fa"/> <!-- Farsi -->
<locale android:name="fi"/> <!-- Finnish -->
<locale android:name="fil"/> <!-- Filipino -->
<locale android:name="fr"/> <!-- French (France) -->
<locale android:name="fr-CA"/> <!-- French (Canada) -->
<locale android:name="gl"/> <!-- Galician -->
<locale android:name="gu"/> <!-- Gujarati -->
<locale android:name="hi"/> <!-- Hindi -->
<locale android:name="hr"/> <!-- Croatian -->
<locale android:name="hu"/> <!-- Hungarian -->
<locale android:name="hy"/> <!-- Armenian -->
<locale android:name="in"/> <!-- Indonesian -->
<locale android:name="is"/> <!-- Icelandic -->
<locale android:name="it"/> <!-- Italian -->
<locale android:name="iw"/> <!-- Hebrew -->
<locale android:name="ja"/> <!-- Japanese -->
<locale android:name="ka"/> <!-- Georgian -->
<locale android:name="kk"/> <!-- Kazakh -->
<locale android:name="km"/> <!-- Khmer -->
<locale android:name="kn"/> <!-- Kannada -->
<locale android:name="ko"/> <!-- Korean -->
<locale android:name="ky"/> <!-- Kyrgyz -->
<locale android:name="lo"/> <!-- Lao -->
<locale android:name="lt"/> <!-- Lithuanian -->
<locale android:name="lv"/> <!-- Latvian -->
<locale android:name="mk"/> <!-- Macedonian -->
<locale android:name="ml"/> <!-- Malayalam -->
<locale android:name="mn"/> <!-- Mongolian -->
<locale android:name="mr"/> <!-- Marathi -->
<locale android:name="ms"/> <!-- Malay -->
<locale android:name="my"/> <!-- Burmese -->
<locale android:name="nb"/> <!-- Norwegian -->
<locale android:name="ne"/> <!-- Nepali -->
<locale android:name="nl"/> <!-- Dutch -->
<locale android:name="or"/> <!-- Odia -->
<locale android:name="pa"/> <!-- Punjabi -->
<locale android:name="pl"/> <!-- Polish -->
<locale android:name="pt-BR"/> <!-- Portuguese (Brazil) -->
<locale android:name="pt-PT"/> <!-- Portuguese (Portugal) -->
<locale android:name="ro"/> <!-- Romanian -->
<locale android:name="ru"/> <!-- Russian -->
<locale android:name="si"/> <!-- Sinhala -->
<locale android:name="sk"/> <!-- Slovak -->
<locale android:name="sl"/> <!-- Slovenian -->
<locale android:name="sq"/> <!-- Albanian -->
<locale android:name="sr"/> <!-- Serbian (Cyrillic) -->
<locale android:name="sr-Latn"/> <!-- Serbian (Latin) -->
<locale android:name="sv"/> <!-- Swedish -->
<locale android:name="sw"/> <!-- Swahili -->
<locale android:name="ta"/> <!-- Tamil -->
<locale android:name="te"/> <!-- Telugu -->
<locale android:name="th"/> <!-- Thai -->
<locale android:name="tr"/> <!-- Turkish -->
<locale android:name="uk"/> <!-- Ukrainian -->
<locale android:name="ur"/> <!-- Urdu -->
<locale android:name="uz"/> <!-- Uzbek -->
<locale android:name="vi"/> <!-- Vietnamese -->
<locale android:name="zh-Hans"/> <!-- Chinese (Simplified) -->
<locale android:name="zh-Hant"/> <!-- Chinese (Traditional) -->
<locale android:name="zu"/> <!-- Zulu -->
</locale-config>