INTRODUCTION TO ANDROID LOCALIZATION

Lecture Two: Format Strings and Plurals



In the last article we ran through the basics of internationalization and localization for Android apps and localized one into French. In this lecture, we will share some common localization issues and their solutions.



String Types

Let’s first go through the three types of string resources, all of which you will likely encounter during your l10n process:

a) Strings: Most basic string

b) String arrays: List of items organized into a string array

c) Quantity strings: Strings that handle pluralization (more on this later)



Format strings

Let’s say you have a dynamic string:

This can be achieved by the following:

values/strings.xml (default)

MainActivity.java

Note: If you are unfamiliar with the above format, we recommend reading about Android string formatting.



The Problem

The first issue you would run into is string positioning. For example, if this string was in Chinese, here is what it would look like:

As you can see, the order of the arguments is different from the English version (even if you can’t read Chinese, you’ll notice 3 precedes 1).

So how do we achieve the above result with format strings?



The Answer: Positional Specifiers

One of the handy features of format strings is positional specifiers. You can tell the formatter the order of the arguments by inserting n$ into the format specifier, e.g. %1$s, %2$d, %3$d.

Therefore the format string for Traditional Chinese would look like this:

values-zh-Hant/strings.xml



Plurals

If you are a detail-minded person you might have noticed another problem in the above example: what if there is only one language in total?

Luckily, Android Studio has nifty string arrays that can handle your second l10n issue: pluralization. Enable pluralization support through these steps:

1) In the default values/strings.xml, insert the following quantity string:

2) Test the plural form with the following code:

3) The first numLang parameter in res.getQuantityString() will return the correct string form while the second numLang parameter replaces the %d placeholder, if present. (Otherwise we only need to call it once.)

4) The string numLanguages should have the value 1 language.



Gender Rules

While Android supports both plural and gender rules, the XML resources file format cannot handle gender rules as of Android Studio v2.2.3. We’ll keep you updated if this gets resolved in a future release.



Download sample file for Android Localization Lectures!

Android Localization Guide-sample file



<  Go to Lecture One: A Beginner’s Guide

    Go to Lecture Three: Best Practices  >



Learn localization and reach global markets like these do.

  • Tango
  • Viber
  • Day One
  • Hootsuite
  • 500px
  • Truecaller
  • Glide
  • Wish
  • Secret