Localizing Strings in Objective-C

This post is based off Making Your App World-Ready, a WWDC 2013 session video.

There are a lot of things to consider when trying to make an app available in multiple languages:

  • Text length variations
  • Text direction
  • Translations

Luckily, Objective-C makes localizing text a relatively less painful experience. In just a few steps you can have your application ready to be translated into a number of different languages.

Translating Interface Files with ibtool

Interface files contain much of the user visible text. These files should be set up to use Base Internationalization. With Base Internationalization you only need to edit one set of interface files (in the base language) and subsequent language files can be updated using the ibtool script. For example:

ibtool SomeNibFile.xib --generate-strings-file StringsFile.strings

Base Internationalization can be turned on inside the project settings. You should also use Auto Layout, which does exactly what it says — lay out text and images based on the size of their content.

Translating Strings in Code with Localized String

Sometimes user visible text needs to be generated in code. Luckily, Objective-C provides NSLocalizedString as a substitute for NSString. NSLocalizedString takes a key as the first parameter. Be sure not to duplicate keys! Its second parameter is a comment. The comment is available for localizers to have a better context when choosing a translation.

Using the command line tool genstrings, it is easy to create a script to generate strings for all of the project files. For example:

find . -name \*.m | xargs genstrings -o en.lproj

One tricky aspect of strings is pluralization. Objective-C provides [NSString localizedStringWithFormat:] to handle pluralization. This will create a stringsdict file with several keys for a string. Using this correctly will discern between pluralization for one, few, and many for each language.

Following these few steps, making your next application user friendly in multiple languages will be less painful and less error prone than ever before!