Localizing Android Applications

Business as Mission requires localization.

Business as Mission

When I spend time with other business owners the topic of “Vision, Mission and Values” often comes up.

If an organization does not have a clear purpose and stated values, it can be easily tossed and turned by opportunities and obstacles that come along over time.

For an example, consider this vision held by the seemingly ubiquitous McDonald’s corporation:

"McDonald's vision is to be the world's best quick service restaurant experience.  Being the best means providing outstanding quality, service, cleanliness, and value, so that we make every customer in every restaurant smile."

Personally, I prefer Mexican food, and you may or may not enjoy McDonald’s. Whatever our personal preferences, McDonald’s can judge their performance by not only their sales figures, but also by scanning the restaurant and looking for smiles. Having something as simple as a “smile meter” helps them operate their business day to day. Not a terrible idea.

But what about “Business as Mission”?

This is a term I learned from a friend not too long ago in speaking about work that his organization is doing on a global basis.

Business as Mission is where an organization looks to not only make a profit for shareholders and provide an income for its employees, but is intentionally working in a specific field to improve the lives of its clients.

This might look like bringing medical care to a dangerous and under-served market where no one else dares venture.

Or it might look like a micro-finance operation where relatively small monetary loans are made to farmers and merchants to help invigorate their businesses in an otherwise entirely stagnant economy.

Whatever its purpose or activity, Business as Mission is often global in nature. And global means multi-lingual, as I was soon to learn.

This same friend asked me if the software he owns and I help maintain could be easily translated into other languages beyond English. My first question was, are we talking about ‘EFIGS’, you know: English, French, Italian, German and Spanish? These are the languages that happily fit into an 8-bit character set. He said, “Sure, those are great, but can we start with Turkish and Russian?”

My response is a topic for another day and a tall glass of Dr. Pepper, but the thought occurred to me that it would be of value to discuss how to modify an Android application to support multiple languages — and that is the topic of this article.

We’re going to demonstrate translating a super simple application into Spanish. Why Spanish? Because I am not sure I know how to spell Turkish or Russian!

Hola Android

The world’s most popular software program is entitled: “Hello World”. So, let’s start with that familiar application and use it as a basis for examining the topic of localization.

First, here is the application in action in its default language of English.

Running the application
Running the application

The only thing we are concerned with here are two strings: the title of the application and the value of “hello”.

The application code itself is nothing beyond the stock Android Activity code which loads the appropriate resources at runtime with a call to setContentView():

package com.msi.linuxmag.hola;

import android.app.Activity;
import android.os.Bundle;

public class HolaAndroid extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

If you are at all familiar with Android programming, you will note that there is absolutely nothing special about this code. In fact, I didn’t even modify it from the boiler-plate code generated by the “new project” wizard.

Let’s have a look at the project as shown in the image below:

The project in Eclipse
The project in Eclipse

Note that under the /res folder there are two folders containing string values. The first is named simply “values”. In the absence of any further guidance on which language to use, an Android application uses the resources found in the values folder.

The second values folder is named “values-es”. The “es” stands for Espanol, or as I will refer to it from here on: “Spanish”.

In this simple application, the values folder contains a single file named “strings.xml”. As you can probably deduce, the strings.xml file contains string literals to be employed in the application.

Let’s have a look at the two strings.xml files in use in this application. First the default version found in /res/values/strings.xml.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello</string>
    <string name="app_name">Frozen Yogurt</string>
</resources>

And now the Spanish file, /res/values-es/strings.xml.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hola</string>
    <string name="app_name">Yogurt congelado</string>
</resources>

Clearly this is a simple example, but it is sufficient to convey the mechanics of how to proceed in translating an application from one language to another.

We have two strings. The first named “hello” has a value of either “Hello” or “Hola”.

The second string named “app_name” has a value of “Frozen Yogurt” in English and “Yogurt congelado” in Spanish. At least that is what Google’s translation widget suggested.

By the way, Google translates “Froyo” to “Froyo”, so I had to come up with something a little less hip and cool, like “Frozen Yogurt”.

Let’s have a look at the application in action.

Demo time

We have already seen what the application looks like in English. Here is a screen shot of the application’s launch icon in the Android home screen shown in the locale of “English-United States”, or “enUS”.

Home screen in English
Home screen in English

Now, let’s change the locale on the device to Spanish.

Switching the locale
Switching the locale

Once the locale is switched to Spanish, we can see that the home application screen has changed. Note the names of applications have changed, including our own. Also note the change in the time formatting in the upper right hand corner of the screen.

Home application screen in Spanish
Home application screen in Spanish

Now let’s see the application running under the Spanish locale.

In Spanish!
In Spanish!

Time for some Yogurt congelado!

So, what if we change the locale to something entirely different, like say German?

German applications screen
German applications screen

What do we get in the application?

Running under German locale
Running under German locale

English? Was Los ist?

When Android does not find a string value which maps to the currently selected locale, it drops back to the default strings which are found in the /res/values folder.

Default resources

The “default” values are found in the base resource folders — and they don’t need to be in English. They can be in whatever language makes sense for you and your application. To have explicitly English values, the strings.xml folder would be located within the /res/values-en folder.

For distinguishing between English in the United States versus English in Australia, you would provider further refinement to the folder names. So, to offer both “Don’t worry about it” in the US and “No worries, mate.” for your Australian clients, place those strings in separate resource folders as follows:

  • /res/values-en-rUS/strings.xml
  • /res/values-en-rAU/strings.xml

We are not limited to just string values. We can have different images (drawables), screen layouts, or even raw resources based on locale. We can also have different resources based on things like screen size and mobile carrier codes but that is beyond our scope at present. To learn more, you can read about Android’s resource selection criteria in detail.

It is important that all of the strings (or any resources) used by an application be found in the default resources files. It is OK to “override” them in a particular language and only provide a subset of the entire list. You might want to do this for example for the title of your application if you want your name to be unique and cross-language, to build brand identity. However, your instructions on how to use your application should be localized.

Localizing an application goes beyond resources — you also need to consider handling dates and numbers in different locales, but we’ll tackle that another time.

Just for fun

I would like to conclude with a fun story shared by a friend of mine who went to Cornell decades before I spent my time far above Cayuga’s waters. He told me that when he matriculated, the university had recently instituted a language requirement for graduation. So, which language did he choose? Fortran! Happy computing.

Fatal error: Call to undefined function aa_author_bios() in /opt/apache/dms/b2b/linux-mag.com/site/www/htdocs/wp-content/themes/linuxmag/single.php on line 62