Internationalizing Your iOS App: Getting Started

In this tutorial, learn how to prepare your app to support multiple languages, including regional numeric formats, rules for plurals, and much more. By Richard Critz.

Leave a rating/review
Save for later
Share
You are currently viewing page 3 of 4 of this article. Click here to view the first page.

Pluralization

You may have observed that iLikeIt randomly chooses for you to take either 1, 2 or 5 months to sell 1000 apps. If not, run the app now and tap You like? several times to see this in action. You’ll notice, whether you’re running in English or Spanish, that the message is grammatically incorrect when you take only one month.

Never fear, iOS to the rescue again! iOS supports another localization file type called a .stringsdict. It works just like a .strings file except that it contains a dictionary with multiple replacements for a single key.

Choose File\New\File from the menu. In the resulting dialog, select iOS\Resource\Stringsdict File and click Next. Name the file Localizable and click Create. Open all of the disclosure triangles and you will see the following:

stringsdict format

Here’s what each section of the dictionary does:

  1. The Localized String Key is a dictionary that represents one localization and it’s the key used by NSLocalizedString(_:tableName:bundle:value:comment:). The localization lookup searches the .stringsdict first and then, if it finds nothing there, the equivalent .strings file. You may have as many of these keys as you like in your .stringsdict; iLikeIt will only need one.
  2. The Localized Format Key is the actual localization — the value returned by NSLocalizedString(_:tableName:bundle:value:comment:). It can contain variables for substitution. These variables take the form %#@variable-name@.
  3. You must include a Variable dictionary for each variable contained in the Localized Format Key. It defines the rules and substitutions for the variable.
  4. There are two rule types for a variable: Plural Rule and Size Rule. This tutorial will only cover the former.
  5. The Number Format Specifier is optional and tells iOS the data type of the value being used to make the substitution.
  6. The Variable dictionary contains one or more keys that specify the exact substitutions for the variable. For a Plural Rule, only the other key is required; the others are language-specific.
Note: For complete information on the stringsdict file format, see Appendix C of Apple’s Internationalization and Localization Guide.

Edit the dictionary to match this picture; specific changes are listed below.

English stringsdict file

Here are the specific changes you are making:

  1. Change the name of the Localized String Key to You have sold 1000 apps in %d months
  2. Change the value of the Localized Format Key to You have sold %@ apps in %#@months@
    This defines a variable months for use in the dictionary.
  3. Rename the Variable dictionary to months
  4. Set the Number Format Specifier (NSStringFormatValueTypeKey) to d
  5. Set the one key’s value to %d month
    Use this key when you want the singular form of the phrase.
  6. Set the other key’s value to %d months
    Use this key for all other cases.

You may delete the empty keys but I recommend against it since you may need them later. If the key is empty, iOS just ignores it.

Note: Xcode switches from showing the “friendly” names of some keys and values to showing their raw values when you edit the stringsdict. While it’s ugly, it’s not incorrect. Just ignore it.

You’ve now completed your base Localizable.stringsdict and are ready to add the Spanish version. In the File inspector, click Localize….

As it did with Localizable.strings, Xcode will ask you to confirm the file’s language. The default will be English since that’s your development language. Click Localize.

The File inspector will update to show the available and selected languages. Click the checkbox next to Spanish to add a Spanish version of the file.

Click the disclosure triangle next to Localizable.stringsdict in the Project navigator to show the individual language files. Open Localizable.stringsdict (Spanish) and make the following changes:

  1. NSStringLocalizedFormatKey: Has vendido %@ aplicaciones en %#@months@
  2. one: %d mes
  3. other: %d meses

Spanish stringsdict file

Build and run. Tap You like? until you have seen all three values and see that the grammar is now correct. And you didn’t change a bit of code! It’s worth internationalizing your app just to get plural handling for free!

English singular correct

Edit your scheme and change the Application Language to Spanish. Build and run. Tap ¿Es bueno? several times to see that the Spanish localization is working correctly.

Notice that although you have left the localizations for your sales volume in the various Localizable.strings files, those localizations are superseded by the ones in Localizable.stringsdict.

Adding Another Language

You may be wondering why there are so many options in the Values dictionary. While many languages such as English and Spanish have one form for singular and one form for plural, other languages have more complex rules for plurals, decimals, zero and so on. iOS implements all of the rules the for languages it supports. To see details on these rules, check out the CLDR Language Plural Rules specified by the Unicode organization.

One language that has more than one plural form is Polish. You’re going to add a Polish localization to iLikeIt in order to see it in action. You have already performed all of these steps in this tutorial to add your Spanish localization so this should be easy for you.

Polish storyboard strings

  • Hello label text: Cześć
  • Sales label text: Sprzedałeś 1000 aplikacji w 20 miesięcy
  • You like button title: Lubisz to?
  1. Select the blue iLikeIt icon in the Project navigator to reveal the project localizations. Click the + to add Polish. Select all three files to be localized.
  2. Under Main.storyboard, open Main.strings (Polish). Change the values as follows:
  3. Under Localizable.strings, open Localizable.strings (Polish). Replace the contents with:
  4. "You like?" = "Lubisz to?";
    "imageName" = "LubieTo";
    
  5. Under Localizable.stringsdict, open Localizable.stringsdict (Polish) and make the following changes:
    1. NSStringLocalizedFormatKey: Sprzedałeś %@ aplikacji w %#@months@
    2. one: %d miesiąc
    3. few: %d miesiące
    4. many: %d miesięcy
    5. other: %d miesiąca
  1. NSStringLocalizedFormatKey: Sprzedałeś %@ aplikacji w %#@months@
  2. one: %d miesiąc
  3. few: %d miesiące
  4. many: %d miesięcy
  5. other: %d miesiąca
"You like?" = "Lubisz to?";
"imageName" = "LubieTo";

Polish stringsdict file

And that’s all there is to it! Edit your scheme and change the Application Language to Polish. Build and run. Tap Lubisz to? several times to see the various singular and plural forms of the sales message. Notice the formatting of the number 1000 has changed as well.

Polish localization complete