4 Tips to Prepare Your Android App for Localization 1


Supporting localization in your Android app does not have to be difficult. You do not need to maintain and upkeep multiple versions of the app. In fact, you can support multiple languages in one app.

 

My goal in this post is to provide you with tips to support localization in a single app. These tips are specifically for Android Studio, but if you are using a different IDE the information should still be useful to you.


 

Decide to Localize Early

It is important to decide whether you want to localize your app or not as early in development as possible. The direction of the development of the app will depend on it. For example, you might have a mixture of in-code variables and resource value files. In-code variables will be more difficult to accommodate for different languages later on if you decide to support other languages.

 

In addition, Android Studio and the Android framework has built-in support to help you with localization. If you decide early in development to support multiple languages then you can structure your app to take advantage of the built-in support.

 

Use Resource Files Over In-Code Declarations

When developing or adding support for localization in your app, it is best to use resource files instead of in-code values. The Android framework has support for localization if you are using resource files. For example, if you choose to support English, Spanish, and Japanese you can create resource files for each language. In each resource file, you can have translated text, different layouts, different menus, or themes for a specific language. Best of all, the Android framework will automatically load and apply the resources for you.

 

Familiarize Yourself With How the Android System Works With Languages

When your app starts, the Android framework looks for the languages your app supports through the available resource files. The resource files that match the system selected language will be the ones that are used. If there is no match, then the Android framework will use the default, which is usually English.

 

Afterwards, when you refer to a resource value in code, the one that matches the system language is the result. That means the same code can support all the languages that you provide resource files for.

 

Structuring Your Project for Localization

To take advantage of the Android framework’s localization support, your project must be structured in a specific way. The Android framework uses the ISO 639.2 code for languages as the identifier for your resource files. All of your resource files for a specific language must be in a directory that follows the framework format under the “res” directory. The framework format for the directory is “<resource type>-<iso 639.2 code of language>”. For example, the values directory for Japanese is “values-ja” and in the directory, you can have your resource files for strings, dimensions, styles, color, and etc. Similarly, for art assets, they will go in a directory called “drawable-ja”.

 

To support another language you can add the resource directory for that language then place the resource files in that directory. The framework will automatically pick the resources that match the system’s current language. This also applies to in-code variable resolution.

The root directory is res and within the directory is three directories. One for English (default), Spanish, and Japanese. Within each language directory, there are different resource files.

 

Tying It All Together

I believe the best way to put all the information together in this post is an example. So for this example, you have three resource file for strings. One for English, Spanish, and Japanese. You want to display “Hello World” in a Text View based on the system’s language. If the system’s language is not English, Spanish, or Japanese then the default will become English.

<resource>
	<string name="hello_world">Hello World</string>
</resource>
<resource>
	<string name="hello_world">Hola Mundo</string>
</resource>
<resource>
	<string name="hello_world">こんにちは世界</string>
</resource>
TextView textView = (TextView) findViewById(R.id.text_view);

// automatically resolve to corresponding system language
// if none of the resource string files match system language then English is default
String helloWorld = getApplicationContext().getString(R.string.hello_world);

textView.set(helloWorld);

 

I hope you found this post helpful. If you found this post helpful, share it with others so they can benefit too.

 

What are your experiences with localizing your app? Are there any tips you wished you knew when you were localizing your app?

 

To stay in touch, follow me on Twitter, leave a comment, or send me an email at steven@brightdevelopers.com.