Keywords: Android | Language Retrieval | Locale Class | Multi-language Support | Internationalization
Abstract: This article provides an in-depth exploration of various methods for retrieving the current language in Android systems, focusing on the core API usage of the Locale class, and combines it with the per-app language preferences feature introduced in Android 13 to offer a comprehensive solution for multi-language application development. The article details the usage scenarios and differences of key methods such as getDisplayLanguage() and getLanguage(), as well as how to implement application-level language management through system settings and APIs, helping developers build better internationalized application experiences.
Basic Methods for Android Language Retrieval
In Android application development, retrieving the device's current language is a fundamental requirement for internationalization support. The Locale.getDefault() method provides access to the system's default locale object, which offers various methods for obtaining language information.
The Locale.getDefault().getDisplayLanguage() method returns the localized display name of the current language, such as "English" in English environments or "中文" in Chinese environments. This method is suitable for directly displaying language names in user interfaces.
The Locale.getDefault().getLanguage() method returns the standard language code, such as "en" for English or "zh" for Chinese. This concise code format is ideal for program logic decisions and resource file matching.
Detailed Analysis of Locale Class Methods
The Locale class provides rich methods for obtaining language and region information, allowing developers to choose appropriate methods based on specific requirements:
// Get language codes and display names
String languageCode = Locale.getDefault().getLanguage(); // Returns: en
String displayLanguage = Locale.getDefault().getDisplayLanguage(); // Returns: English
// Get ISO standard codes
String iso3Language = Locale.getDefault().getISO3Language(); // Returns: eng
// Get region information
String countryCode = Locale.getDefault().getCountry(); // Returns: US
String displayCountry = Locale.getDefault().getDisplayCountry(); // Returns: United States
// Get complete locale information
String displayName = Locale.getDefault().getDisplayName(); // Returns: English (United States)
String localeString = Locale.getDefault().toString(); // Returns: en_US
String languageTag = Locale.getDefault().toLanguageTag(); // Returns: en-USThese methods provide developers with language and region information at different granularities, allowing flexible selection based on application scenarios.
Android 13 Per-App Language Preferences
Android 13 introduced the per-app language preferences feature, allowing users to set languages individually for each application, independent of the system language. This functionality is implemented through new APIs in the LocaleManager class.
Applications can retrieve current app language settings using the getApplicationLocales() method:
// Kotlin implementation
val localeManager = applicationContext.getSystemService(LocaleManager::class.java)
val currentAppLocales = localeManager.applicationLocales
// Java implementation
LocaleManager localeManager = context.getSystemService(LocaleManager.class);
LocaleList currentAppLocales = localeManager.getApplicationLocales();Setting application language uses the setApplicationLocales() method:
// Set application language to Simplified Chinese
localeManager.setApplicationLocales(LocaleList.forLanguageTags("zh-CN"))System Settings Integration
To make application language settings appear in system settings, configure language support in AndroidManifest.xml:
<application
android:localeConfig="@xml/locales_config"
...>
</application>Simultaneously create the res/xml/locales_config.xml file to define supported languages:
<?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="zh-CN"/>
<locale android:name="ja-JP"/>
<locale android:name="es-ES"/>
</locale-config>Backward Compatibility Implementation
For Android 12 and lower versions, use the AndroidX support library to achieve the same functionality:
// Use AndroidX to set application language
val appLocale = LocaleListCompat.forLanguageTags("zh-CN")
AppCompatDelegate.setApplicationLocales(appLocale)
// Get application language settings
val currentLocales = AppCompatDelegate.getApplicationLocales()Configure the service in AndroidManifest.xml to support automatic language setting storage:
<service
android:name="androidx.appcompat.app.AppLocalesMetadataHolderService"
android:enabled="false"
android:exported="false">
<meta-data
android:name="autoStoreLocales"
android:value="true" />
</service>Best Practice Recommendations
When implementing multi-language support, it's recommended to follow these best practices:
Choose appropriate language retrieval methods: use getDisplayLanguage() when displaying to users, and use getLanguage() for program logic.
Handle language switching promptly: ensure the interface correctly refreshes and displays corresponding language resources when application language changes.
Consider regional variants: the same language may have different expression habits in different regions, such as differences between American and British English.
Test multi-language scenarios: ensure the application displays and functions correctly in various language environments.
By properly applying these language retrieval and management methods, developers can build truly internationalized Android applications, providing users with better localized experiences.