Part 5 - Application Localization and String Resources
- PDF for offline use:
Application localization is the act of providing alternate resources to target a specific region or locale. For example, you might provide localized language strings for various countries, or you might change colors or layout to match particular cultures. Android will load and use the resources appropriate for the device’s locale at runtime time without any changes to the source code.
For example, the image below shows the same application running in three different device locales, but the text displayed in each button is specific to the locale that each device is set to:
In this example, the contents of a layout file, Main.axml looks something like this:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/myButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> </LinearLayout>
In the example above, the string for the button was loaded from the resources by providing the resource ID for the string.
Tips for Localization
Localization can be a time consuming process. As such, most applications are not localized. In order to minimize the impact that localization issues can cause, following these tips:
Make as few layouts as possible. A single, flexible layout supporting all locales will be easier to maintain.
Make sure to test the default locale. Your application will crash if the default resources cannot be loaded for some reason (i.e. they are missing).
Only create the localized resources you need. For example, although there are many French speaking nations in the world, it may not be necessary to support each individual French region. Supporting these regions may create extra work and unnecessary effort in the application’s development and maintenance.