| Members: 25234 |
| Language: English |
|
Group categories:
|
| More group info » |
|
Android will run on many different devices in many different regions, and used by speakers of many different languages. To reach the most users in the most effective way, developers will want to localize their applications to the native languages spoken by the users they wish to reach. This document explains how to localize an Android application. What You Should Know AlreadyThis is not an introduction to how to build Android applications; for that information, you can begin here: http://code.google.com/android/intro/index.html This document assumes that you are generally familiar with Android resource loading, declarative UI building via Android's XML system, and general Android development considerations such as Activity lifecycle.Introduction to the L10nDemo AppTo illustrate the concepts discussed in this document, we've created the L10nDemo application. This application is very simple, and specifically constructed to illustrate the key moving parts of Android's localization framework.The application consists of a simple UI that displays 3 TextViews, and a button with a background image. The 3 TextViews display text that is localized appropriately for the system's current locale, and the button shows an image of the national flag for the system's current locale. When clicked, the button displays a Dialog containing additional text. The UI is implemented with two variants: one used for portrait orientations, and one for landscape. The contents of each variant are the same, but are visually arranged in a slightly different way. Here is a screenshot of the L10nDemo application, running in English in the 1.0_r2 SDK emulator. To download the source code, you can click here. Android Localization: Theory of OperationBefore we dive in to the details of of the L10nDemo, let's begin with an overview of how Android implements localization.Android provides a framework that allows developers to separate their UI from their application logic. Generally, Android breaks down an application's UI into 3 general concepts:
Developers are not required to make use of these resources; for instance, developers could simply put all their images into the 'raw/' directory in a project, and build up user interfaces programmatically directly from Java source code. However, this design approach is generally considered to be poorly "internationalized" (meaning that it is difficult to localize to multiple languages.) The framework used by Android is specifically designed to be easy to localize, and so we strongly encourage developers to make use of this framework. As you know, Android loads resources such as declarative layout XML files and media assets from the project's 'res/' directory. However, there's a bit more to it than just that: Android can actually selectively load resources from different directories, based on the current device configuration and locale. For instance, your application code might wish to refer to a string value named 'R.string.title'. Android will choose the correct value for that string at runtime by loading the appropriate strings.xml file from a matching 'res/values' directory. For instance, if the current locale is 'en-US', then Android will look for a value for R.string.title by looking in the following files, in this order: 'res/values-en-rUS/strings.xml', 'res/values-en/strings.xml', 'res/values/strings.xml'. Android chooses the first value it finds -- that is, the value most specific to the current locale, eventually falling back on the default. (Note that you use whatever language you like for the default file in 'res/values/strings.xml'; many developers choose English, but it doesn't need to be.) Besides locale, Android will also perform similar resource resolution for device configurations or states, such as whether the screen is currently portrait or landscape. For instance, on a T-Mobile G1 with the screen open and keyboard exposed, Android will look for a layout file matching 'R.layout.main' in this order: 'res/layout-land/main.xml', 'res/layout/main.xml'. You can also mix-and-match these permutations. For instance, 'res/values-fr-rCA-land/strings.xml' would be used to supply values for strings whenever a French-Canadian device is in landscape mode. This particular example is somewhat contrived since it's unlikely you'd have special needs for that specific combination, but if you have a particularly busy UI it might be useful for providing field labels of varying lengths for different screen modes. For the full details on how Android resolves resources, see: http://code.google.com/android/devel/resources-i18n.html#AlternateResources As you'll see, the resource-resolution framework is fairly sophisticaed and can be used for more than just localization. The remainder of this document, however, focuses on showing how to use this framework to localize an app. L10nDemo ResourcesBelow is a list of all the resource directories present in the L10nDemo project, and an explanation of when they are used.
Finally, here is a table specifying exactly which resources are loaded for while locales:
Localization Techniques in Java CodeIn some cases, you cannot avoid needing to access resources from code. The most common example is filling in a template string with values computed at runtime. Fortunately, the Android localization framework is also used when resolving resources from code. For example, the snippet of code below from the L10nDemo uses the standard Java MessageFormat functionality in conjunction with Android's localization framework to transparently select the correct template string for the current locale and fill it in with values derived from code:String msg = getResources().getString(R.string.text_c); // contains {0}, {1}, and {2} in the XML file msg = java.text.MessageFormat.format(msg, "foo", "bar", myStringVariable); In some cases, developers may wish to look up specific resources manually. For example, a project being ported from a different platform that doesn't have a localization framework might have lots of resources simply placed into a shared directory, such as 'raw/'. In such cases we recommend that developers convert over to use Android's framework, but those who choose not to may wish to look up the current locale to select resources manually. That can be accomplished via the standard Java API, or by using the Android Context. For instance, the two lines below are equivalent: String locale = context.getResources().getConfiguration().locale.getDisplayName(); String locale = java.util.Locale.getDefault().getDisplayName(); Testing Localized ApplicationsCurrently it is difficult to test localized applications on devices, since all shipping production devices are themselves not localized to conserve space. This means that on actual devices, there is currently no easy way to change the system locale, and thus no easy way to test the localization of applications in anything other than the device's native locale. We expect that this will become easier to do in the future.Fortunately, it is easy to test localized applications in the emulator; you can simply change the default system locale and restart the emulator runtime.
For instance, to test in Canadian English, type: "setprop persist.sys.language en;setprop persist.sys.country CA;stop;sleep 5;start" This will cause the emulator runtime to restart (which will appear similar to a reboot, but is not actually a full reboot.) Once the Home screen appears again, simply re-launch your application (such as by clicking the Run icon in Eclipse) and the application will launch with the new desired locale. Note that the default localization for the system itself is English, so if you choose a non-English locale for which no localization is present in the emulator, the system itself will still display in English. However even in this case, the locale will have been changed and your own application will localize properly. Note: This only works with the 1.1_r1 SDK, and will not work on devices. For instructions on how to do this on 1.0_r1 and 1.0_r2 SDKs, please see version 5 of this document which can be accessed from the "Version" select menu at the end of this document. Also, this process may change in a future SDK version. Publishing Localized ApplicationsMost developers will want to publish their applications in the Android Market, which is the main application distribution system for Android devices. The process for publishing localized apps is very similar to the process for publishing single-country applications, but there are a few extra considerations. This section describes them; generally, they revolve around managing your .apk files.If your application has just a few localizations, then you probably will simply roll up a single .apk that includes them all. However, on the other extreme, applications that are aggressively localized (such as an application localized to the 40 languages that Google supports for its top-tier products) will end up with 40 different values for the same string, layout, and other resource keys. No individual user will need the localized assets for all 40 languages, so delivering those assets to every user is a waste of space. On a mobile device with limited bandwidth and storage, this waste is even more problematic. In view of this, developers should consider at what point to break their .apk files apart into different builds for different groups of languages. There is no single right answer for every developer; some may only have a handful of languages and choose to place them all in the same .apk, while others may have many assets and choose to have a separate .apk for each supported locale. Still other developers may choose to build apk files for regions -- for instance, one for European locales, one for North American, another for Asian locales, etc. Once you've determined how you want to split up your application, you'll need to construct the .apk files. Unfortunately, due to some bugs in the current SDK tools, the 'aapt -c' command (which would normally be used for this purpose) does not function correctly. This will be fixed in a future version, but in the meantime the only known workaround is to run the aapt command multiple times. The easiest way to do this is to create a temporary folder in Eclipse, move out the resource directories which you do not wish to appear, rebuild the project, and then export an unsigned build as you normally would. Then move the resource directories back in. A future build of the SDK and ADT will fix this issue. Future Changes to LocalizationLike all parts of Android, the localization features are under active development. Here are a few changes that will be coming soon to a development environment near you:
Localization ChecklistNow that you have an understanding of how the Android localization framework is used, here is a short checklist to summarize the process of localizing an Android application.
|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy |
| ©2009 Google |