Android Localization 101

According to IDC, Android has roughly 80% of the worldwide smartphone market share with a strong lead on iOS outside of the US and Western Europe. That means that there is a very large number potential users for your app that are going to be best served by having your app localized in their language. Sadly, many developers don't take the time or care to properly localize apps and some don't even take advantage of Android's built in systems for coding in a language agnostic way, despite how simple and necessary it is to do so. This is particularly embarrassing when you consider that Google has pre-packages a number of common words and phrases into the OS in the most commonly used languages. Let's take a look at a simple example of doing it wrong and then right.

Wrong:

public void wrongWay() {  
            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                context);
            alertDialogBuilder.setTitle("My Awesome Title");
            alertDialogBuilder
                .setMessage("My Awesome Message")
                .setCancelable(false)
                .setNegativeButton("OK",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {
                        dialog.cancel();
                    }
                });
                AlertDialog alertDialog = alertDialogBuilder.create();
                alertDialog.show();
}

That seems simple enough, right? After all, all that is being done here is creating a simple dialog box that shows an "OK" button that when tapped just dismissed the alert. It's true that if you only ever expect throughout the entire theoretical lifetime of your app, then this will probably be fine for you, but, given Android's global footprint, that's not a great strategy. This is doubly true when you consider how easy it is to do the right way.

Android has built in the ability to easily read strings from embedded XML string files. The idea is you have a key string and use that string to pull the correct translated one out of the appropriate file for the user's language preference. It might sounds like a bit much, but take a look at the string file for our sample above:

<string name="alert_title">My Awesome Title</string>  
<string name="alert_message">My Awesome Message</string>  

Here's what the above sample would like implementing our strings file:

public void rightWay() {  
            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                context);
            alertDialogBuilder.setTitle(R.string.alert_title);
            alertDialogBuilder
                .setMessage(R.string.alert_message)
                .setCancelable(false)
                .setNegativeButton(android.R.string.ok ,new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {
                        dialog.cancel();
                    }
                });
                AlertDialog alertDialog = alertDialogBuilder.create();
                alertDialog.show();
}

That's it. From here, if you wanted to say localize your app to Spanish, you'd just have to add a strings file using the same format as the one replicated above but with the text in Spanish. Also, notice the "android.R.string.ok" -- that's using one the built strings that Google provides in Android itself.

Experienced developers will know that this is all the first baby-step in getting your app ready for a world-wide audience. Follow us here and on Twitter for updates on our next Android localization post!

Got a project you need some help with? Contact us by filling out the contact form on this page. We'd love to help!