Basic Guide: Android Internationalization (i18n)

Today we want to have a quick look at how you can get started with your Android Internationalization (i18n) project within Android Studio.

This basic guide will give you a short overview on how to get started with your Android internationalization process and for that we assume, that you have a working Android application.

What is Internationalization in Android?

Before we move on to the code, we must understand what internationalization of an Android application entails.

In an Android OS based mobile device, users have a locale set, based on their location and their preferences. Whenever an application starts, that application can decide to respect the preference set by the user and show all the text in the app based on their set language.

Internationalizing one’s application is a great way to also increase one’s userbase, as product users don’t feel limited due to the presence of a foreign language which they might not even understand.

Knowing the project

For this lesson, we will be making an Android application using the Android Studio IDE. The Android Studio IDE will get the user locale, set on the device and automatically also set the language of the application to be the same. Of course, we will be demonstrating this with only a limited number of languages in this demo, three to be exact: English, Japanese and French.

We will try to keep the project simple and try to build two functionalities which are complementary to each other when it comes to creating an excellent and localized user experience for the end user:

  • When a user opens our app, the app will automatically get the device’s set locale and set the app’s language to be the same locale.
  • Inside the application, we will allow the user to change the locale for the application as well. This will change the application’s language too.

Setting up the project

Let’s make a new project in Android Studio as below:

We named our application “AndroidInternationalization” and it will be supporting three languages.

Managing Dependencies

In Android, dependencies are managed using the Gradle build tool. When we make a project using Android Studio, it automatically adds the dependencies it needs to set up a project.

For the sake of keeping this guide simple – you might also have different versions of dependencies and SDK versions – here is our app level build.gradle file for this project:

We also have a root level dependency build.gradle file and here are the contents of the file:

This what it looks like when using Android Studio Version 3.0.1, which is what we used for this tutorial.

Making multiple String resource files

To give our localization process a kick start for the three supported languages, we will create some new folders and files in the res folder in our project structure. The file structure now looks as follows:

See how there are three, similarly named folders for values:

  • the values directory is for default language, i.e. English
  • the values-fr directory is for the French language
  • the values-ja directory is for the Japanese language

We have also added the same file (strings.xml) in all the three folders.

Trying the application

For now, we will run our application multiple times and will only change the device’s locale so that app can show changes in the locale:

Of course, the default locale shown above reflects the English language being used. Let’s change this now to Japanese:

Finally, let’s try the French locale:

This was the simplest demonstration of an internationalization support of our app. The best thing about this was that we actually didn’t have to do any configuration for this in our app or on our device. The application just picked the locale from the device and used that language in the app as well.

One thing to note here is if the Android cannot find a language locale’s file or just a particular String in the locale-specific file, it will switch either completely or just for that specific String to the default English Strings file. This enables us to show the user something rather than nothing at all!

Translating between text

If you want to perform some dynamic localization in your project and not through the Strings resource file, you have two options. We will explain these options along with the use cases:

  1. Returning language specific data from the server: In this case, you know what data you are returning from the Server. In the request object from the device to the server, you can include meta-data about the user’s language. Now, it is the responsibility of the server to return the response in a correct language. This is very much flexible but only applicable when your server know what data to return,
  2. Using a runtime API for the translation purposes: It is also possible to use Translation APIs which translate the text at the go. This approach can be used in apps like conversational apps where Users can chat with each other. When a user sends a message in, say French and user at the other end understands German, we can use the API to convert the text to German and then pass it to the receiving end.

Conclusion

In this lesson, we integrated Internationalization process in our demo Android based application. We saw how easy it is to integrate Internationalization in Android.

Internationalization is a great way to increase users on a product so there are no limits in terms of how users use your product.

However, writing code to localize your app is one task, but working with translations comes with a whole new set of challenges. Fortunately, PhraseApp can make your life as a developer easier! Feel free to learn more about PhraseApp, referring to the Getting Started guide.

If you’re interested in the topic of i18n in the Backend using Spring Boot, make sure to check out our other article which also takes a closer look at spring boot localization.

Basic Guide: Android Internationalization (i18n)
Rate this post
Related Posts
Comments