How to Localize Your Android App - dummies

By Barry Burd

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:

  1. Open your project.

  2. Open the project’s reusable_layout.xml file and put the Designer tool in Text mode.

    Notice lines such as

    android:text=“Extra cheese”

    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.

  3. In the editor, click inside the string Extra cheese.

  4. Press Alt-Enter.

    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.

  5. In the intention action menu, select Extract String Resource.

    Well, wha’ da’ ya’ know?! An Extract Resource dialog box appears!

  6. 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.

  7. Click OK.

    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:

  8. (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.

  9. Right-click (or on a Mac, Ctrl-click) the strings.xml file in your project’s res/values folder in the Project tool window.

  10. In the context menu that appears, select Open Translations Editor.

    The Translations Editor appears in place of the Designer tool.

    The Translations Editor.
    The Translations Editor.
  11. Near the top of the Translations Editor, click the globe icon.

    A list of language locales appears.

    Select a language.
    Select a language.
  12. 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.

  13. 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.)

  14. 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.

  15. 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.

<?xml version=“1.0” encoding=“utf-8”?>
&lt;string name=“hello”&gt;
Ciao mondo, la mia attività!</string>
&lt;string name=“app_name”&gt;
Il mio secondo progetto Android</string>
<string name=“extra_cheese”>Con più formaggio</string>
<string name=“pepperoni”>Merguez</string>
<string name=“plain”>Semplice</string>
<string name=“show”>Mostra</string>