How to Localize Your Android App
Words, words, words. Most Android apps have so many words. It’s a wonder a developer can keep this stuff straight. It’s too easy to type a word one way in one part of the code and misspell the word in a different part.
You can reduce the problem by creating string constants. For example, you can write
public final String pep = “Pepperoni”;
at the top of your Java program and use the variable pep in place of the string “Pepperoni” throughout the code. But then, to change from the English word pepperoni to the Italian word merguez, you have to mess with your Java code. In a world where only six percent of all mobile phones are in the United States, you don’t want to edit Java code for dozens of countries.
The elegant solution to this problem is to use Android’s string externalization feature. Here’s what you do:
Open your project.
Open the project’s reusable_layout.xml file and put the Designer tool in Text mode.
Notice lines such as
in the reusable_layout.xml file? What if the user isn’t an English language speaker? A layout file describes the look of the app’s screen. The look of the screen shouldn’t depend on the user’s being able to understand what Extra cheese means.
In the editor, click inside the string “Extra cheese”.
A pop-up menu appears. This menu is called an intention action menu. The menu contains a list of actions from which you can choose.
In the intention action menu, select Extract String Resource.
Well, wha’ da’ ya’ know?! An Extract Resource dialog box appears!
In the Resource Name field, type extra_cheese.
A resource name must not contain blank spaces. In this step, extra_cheese is okay and extracheese is also okay. But extra cheese isn’t okay.
Android Studio adds the following element to your res/values/strings.xml file:
<string name=“extra_cheese”>Extra cheese</string>
Android Studio also replaces “Extra cheese” in your layout’s CheckBox element with an @string expression:
(Optional, but very nice if you do it.) Repeat Steps 3 through 7 for the strings “Pepperoni”, “Show”, and (in the activity_main.xml file) “Plain”.
With your app’s strings externalized, you’re ready to go international.
Right-click (or on a Mac, Ctrl-click) the strings.xml file in your project’s res/values folder in the Project tool window.
In the context menu that appears, select Open Translations Editor.
The Translations Editor appears in place of the Designer tool.The Translations Editor.
Near the top of the Translations Editor, click the globe icon.
A list of language locales appears.Select a language.
Select a language locale from the list.
For this exercise, Italian (it) is selected. Check here for the full scoop on language locales.
As a result, the strings.xml branch in the Project tool window now has two sub-branches. Both sub-branches sport the label strings.xml, but the new sub-branch’s icon is a tiny picture of the flag of Italy.strings.xml files.” width=”280″/>Look! You have two strings.xml files.
Temporarily change the Project tool window from Android view to Project view. Your project’s res folder now has a values subfolder and a values-it subfolder. The values-it subfolder contains its own strings.xml file. (Okay, you can go back to the Android view now!)
In the Translations Editor, the term extra_cheese is in red because you haven’t yet translated extra_cheese into Italian. The same is true for other terms that you haven’t yet translated.
Double-click the Italian (it) column in the extra_cheese row. In that column, type Con più formaggio, and then press Enter.
(Sorry. The Translations Editor doesn’t do any translating for you. The Translations Editor only adds code to your project when you type in the translations of words and phrases.)
Repeat Step 13 for the pepperoni, show, and plain rows.
If your Italian is a bit rusty, copy the text from the res/values-it/strings.xml file below.
Test your app.
As with most devices, the emulator has a setting for Language & Input. Change this setting to Italiano (Italia), and suddenly, your app looks like the display below.