Keywords: Android Localization | BCP47 Standard | Resource Folder Naming | Multilingual Support | LocaleList API
Abstract: This article provides an in-depth exploration of Android's multilingual support mechanisms, detailing the application of BCP 47 and ISO 639-1 language code standards in Android app localization. It systematically presents the list of languages and locale settings supported in Android 5.0 and later versions, with practical code examples demonstrating proper resource folder naming. The analysis extends to the improved resource resolution strategy introduced in Android 7.0, including the use of LocaleList API and optimization of multilingual fallback mechanisms, offering developers a complete internationalization solution.
Overview of Android Multilingual Support
The Android platform has been committed to providing localized experiences for global users since its inception. With the release of Android 5.0, the system began comprehensive support for the BCP 47 standard, marking a significant step forward in multilingual support. BCP 47 is an international standard for identifying languages, regions, and variants, offering greater flexibility and comprehensiveness compared to the traditional ISO 639-1 standard.
Language Code Standards and Resource Folder Naming
In Android application development, correctly naming resource folders is crucial for implementing localization. The naming rules vary depending on the language code standard used.
When using ISO 639-1 two-letter codes, resource folders follow the format values-xx, where xx represents the two-letter language code. For example, English resource folders should be named values-en, while French resource folders should be named values-fr.
For the BCP 47 standard, resource folders follow the format values-b+xxx, where xxx is the three-letter language code. This naming convention supports finer-grained language variants and better meets the linguistic needs of different regions.
Language List Supported in Android 5.1
Android 5.1 version supports an extensive range of languages and locale settings, covering major global languages. Here are some representative examples:
English supports multiple regional variants including: en_US (US English), en_GB (UK English), en_AU (Australian English), etc. Chinese supports both Simplified Chinese and Traditional Chinese, corresponding to zh_CN and zh_TW respectively. Spanish supports regional variants such as es_ES (European Spanish) and es_419 (Latin American Spanish).
It's important to note that certain languages have special code mapping relationships. For example, the standard code for Hebrew is "he", but it's rewritten as "iw" in Android; the standard code for Indonesian is "id", but it's rewritten as "in" in Android. This rewriting mechanism maintains compatibility with earlier versions.
Evolution of Resource Resolution Mechanisms
Android 7.0 introduced significant improvements in resource resolution, substantially enhancing the accuracy of multilingual support and user experience.
Resource Resolution Before Android 7.0
Prior to Android 7.0, the system's resource resolution mechanism was relatively simple. When the system couldn't find an exact language resource match, it would sequentially attempt the following strategies: first remove the country code, and if still no match found, fall back to the default language.
Consider this scenario: an app supports English (default), German, Spanish, French, and Italian, while the user's device is set to Swiss French (fr_CH). In versions before Android 7.0, the system would attempt:
Try fr_CH => Fail
Try fr => Fail
Use default language (English)
This mechanism could result in users seeing interfaces in unfamiliar languages, affecting the user experience.
Improved Resolution Strategy in Android 7.0
Android 7.0 introduced a more intelligent resource resolution mechanism. In the same scenario, the new resolution process is:
Try fr_CH => Fail
Try fr => Fail
Try children of fr => Find fr_FR
Use fr_FR
This improved resolution strategy finds the closest parent dialect, ensuring users receive interfaces that match their language preferences as closely as possible.
Using the LocaleList API
Android 7.0 introduced the LocaleList.getDefault() API, allowing applications to directly query the list of languages set by the user. This API provides developers with more powerful multilingual processing capabilities.
Here's an example of using the LocaleList API:
import android.os.LocaleList;
public class LanguageHelper {
public static void displayUserLanguages() {
LocaleList localeList = LocaleList.getDefault();
for (int i = 0; i < localeList.size(); i++) {
Locale locale = localeList.get(i);
Log.d("Language", "User language: " + locale.getDisplayName());
}
}
}
This API is particularly useful for applications that need to dynamically adjust content based on user language preferences, such as search engines, browsers, and input methods.
Best Practices and Configuration Recommendations
To ensure effective multilingual support and optimal performance, developers are advised to follow these best practices:
Explicitly Specify Supported Languages
Use the resConfigs property in the module-level build.gradle file to explicitly specify the languages supported by the application:
android {
defaultConfig {
// ...
resConfigs "en", "es", "zh", "fr"
}
}
This approach optimizes the application's package size by including only resource files for the specified languages.
Use Formatters Instead of Hardcoding
As Android supports an increasing number of languages, hardcoding numbers, dates, and other formats may cause display issues. It's recommended to use system-provided formatters:
String formattedMessage = String.format(locale, "Choose a %d-digit PIN", 4);
Resource Folder Organization Strategy
For Android 7.0 and later versions, it's recommended to store resources in the most common parent dialect. For example:
- Change
values-es-rUStovalues-b+es+419(Latin American Spanish) - Change
values-en-rGBtovalues-b+en+001(International English)
This organization method improves the performance and reliability of resource resolution.
Practical Implementation Example
Let's demonstrate how to implement multilingual support in an Android application through a complete example.
First, create corresponding resource folders in the project's res directory:
res/
values/ # Default resources (English)
strings.xml
values-es/ # Spanish resources
strings.xml
values-zh/ # Chinese resources
strings.xml
values-fr/ # French resources
strings.xml
Define corresponding string resources in each strings.xml file:
<!-- values/strings.xml -->
<resources>
<string name="app_name">My Application</string>
<string name="welcome_message">Welcome to our app!</string>
</resources>
<!-- values-es/strings.xml -->
<resources>
<string name="app_name">Mi Aplicación</string>
<string name="welcome_message">íBienvenido a nuestra aplicación!</string>
</resources>
Dynamically retrieve current language settings in code:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get current language settings
Locale currentLocale = getResources().getConfiguration().getLocales().get(0);
String languageName = currentLocale.getDisplayName();
TextView languageInfo = findViewById(R.id.language_info);
languageInfo.setText("Current language: " + languageName);
}
}
Conclusion
The Android platform's multilingual support has undergone significant evolution, progressing from limited language support in early versions to comprehensive BCP 47 standard compatibility today. By correctly using language code standards, rationally organizing resource folder structures, and leveraging the improved resolution mechanisms introduced in Android 7.0, developers can provide excellent localized experiences for global users.
As the globalization trend of mobile applications continues to strengthen, deeply understanding and properly implementing Android's multilingual support mechanisms becomes increasingly important. By following the best practices outlined in this article, developers can build high-quality applications that truly serve global users.