Localize your iOS and Mac apps

Since Xcode 6, it is possible to export strings in the app to an XLIFF file for translation, which makes localization easier. Here is how to do it:

Get your project ready for localization

Prepare your code

To get your code ready for localization, you will need to use the NSLocalizedString macro like the following:

Objective-C

// Instead of this
NSString *title = @"Hello";
// Do this
NSString *title = NSLocalizedString(@"Hello", @"title in alert");

Swift

// Instead of this
let title = "Hello"
// Do this
let title = NSLocalizedString("Hello", comment: "title in alert")

The second argument is called “comment”, it has no actual effect, but it provides hints for translators. So it is better to fill in something about the context. It will be useful later.

Get your resource files ready

After modifying your code, the next step is resource files such as xib and storyboard.

  1. Select your project from the sidebar, click Info and add a new language by clicking the little + button under Localization.

    Select an language and add it

  2. Select resource files that you would like to localize. For example interface files.

    Select the resource files to localize

  3. If you want to add new languages to a existing file, you can click the Localize... button in the utility sidebar. Then select the supported languages.

    Click Localize…

Export XLIFF file

After getting your project ready, the next step would be export the strings so they can be translated.

  1. Select the project at the sidebar (not target)
  2. Click Editor -> Export For Localization...
  3. Translate strings in the XLIFF files.

Edit the XLIFF file

There are few apps and sites that can open and edit XLIFF files, here I will use Xliffie as an example.

All translatable strings in the project

You may notice there are some info at the sidebar telling you it is an UILabel, they are comments generated by Xcode.

Your comments are here as well

Take care of printf formatted string

Printf-formatted strings need special care, it may causes bugs or even app crashes if they are not properly handled. For example translating a %@ to %d will causes the app to crash.

Xliffie warns you when the format doesn’t seem right

Using translation services

When there are lots of strings in your app, it maybe a time consuming task to translate all of them and some people prefer using translation services like Google / Bing Translate to localize some simple phrases. It worths noting that some languages doesn’t use the same symbol as English, and may cause problem with printf-formatted string. For example % is translated to in Chinese, which is a full-width character instead of half-width.

Translation issue is detected in Google Translate result, that is another % sign!

Import the XLIFF file

After editing the XLIFF file, it is time to apply it back to your project.

  1. Click Editor -> Import For Localization...

  2. Confirm the changes

    Xcode shows the changes

Done, Your app is translated!


Xliffie is an XLIFF editor designed for developers, it integrates Google / Bing Translate, supports printf-formatted strings, and you can search with regular expression too!