Localized Languages: Capisce?

As you've been running the flashlight, you probably haven't noticed an important feature for users in Italy: The entire app has been translated into their madrelingua (mother tongue). Time to look at how this localization was performed.

Localizable.strings

Most of the effort in localization involves changing words. You say "white;" Italians say "bianco." In localized apps like this one, Cocoa helps you pick the right word with a file called Localizable.strings. This file acts as a small database that maps keys to values. If you look up a string with the key White, it will return the value Bianco on phones that are configured to use Italian. If any other language is selected, "White" is returned.

Note

The default language is defined as English in the Info.plist file.

Actually, it's a lie to say there's one Localizable.strings file—multiple copies exist, one for each language. If you open the Flashlight Pro project in the Finder, you see English.lproj and Italian.lproj in the Resources→Localizations folder. These folders contain the files used in each language.

When you used NSLocalizedString(), you performed the lookup (Strings in user interfaces). For example, in the FlashlightModel, this code is used:

NSLocalizedString(@"White", nil)

Italians will be loading files from Italian.lproj, so this line in Localizable.strings will come up as a match:

"White" = "Bianco";

Other users will have loaded the same file from English.lproj, so this will be the match:

"White" ...

Get iPhone App Development: The Missing Manual now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.