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.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Internationalizing Your iOS App: Getting Started
25 mins
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:
Here’s what each section of the dictionary does:
- 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. - 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@
. - You must include a Variable dictionary for each variable contained in the Localized Format Key. It defines the rules and substitutions for the variable.
- There are two rule types for a variable: Plural Rule and Size Rule. This tutorial will only cover the former.
- The Number Format Specifier is optional and tells iOS the data type of the value being used to make the substitution.
- 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.
Edit the dictionary to match this picture; specific changes are listed below.
Here are the specific changes you are making:
- Change the name of the Localized String Key to
You have sold 1000 apps in %d months
- Change the value of the Localized Format Key to
You have sold %@ apps in %#@months@
This defines a variablemonths
for use in the dictionary. - Rename the Variable dictionary to months
- Set the Number Format Specifier (NSStringFormatValueTypeKey) to
d
- Set the one key’s value to
%d month
Use this key when you want the singular form of the phrase. - 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.
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:
-
NSStringLocalizedFormatKey:
Has vendido %@ aplicaciones en %#@months@
-
one:
%d mes
-
other:
%d meses
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!
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.
- 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.
- Under Main.storyboard, open Main.strings (Polish). Change the values as follows:
- Under Localizable.strings, open Localizable.strings (Polish). Replace the contents with:
- Under Localizable.stringsdict, open Localizable.stringsdict (Polish) and make the following changes:
-
NSStringLocalizedFormatKey:
Sprzedałeś %@ aplikacji w %#@months@
-
one:
%d miesiąc
-
few:
%d miesiące
-
many:
%d miesięcy
-
other:
%d miesiąca
-
NSStringLocalizedFormatKey:
"You like?" = "Lubisz to?"; "imageName" = "LubieTo";
-
NSStringLocalizedFormatKey:
Sprzedałeś %@ aplikacji w %#@months@
-
one:
%d miesiąc
-
few:
%d miesiące
-
many:
%d miesięcy
-
other:
%d miesiąca
"You like?" = "Lubisz to?"; "imageName" = "LubieTo";
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.