Adding localization support in your iOS apps tagged: ,

Adding localization support in your iOS apps

Posted by in How-to, iOS

In xcode 4.0 I found adding localization language files a bit of a pain. They seem to be a mission to make work properly and sometimes just randomly stop working. Anyway, here’s a quick guide to try and get your localization working if you starting to tear your hair out.

For my demo project, I have created a view based application. In the main view controller, I added a label.

To add a localization file. Select File->New->New File. Select the Resource side selection and select Strings File. Name this file “Localizable.strings”. This file is added to your project. Select this file and open the right side properties window.

Under the properties window, Select UTF-16 as your text encoding. You will be prompted to convert your file. Select “Convert”.

Under “Localization”, select the + button. This will jump to some other file (I’m not sure why). Select the Localizable.strings file again. Under “Localization” in the project settings, you will now see English added.

To add more languages, click the + button. For this example, we will add Dutch. Under your file list, you will now see 2 Localization files for English and Dutch.

For each file you must provide a key and string that you wish to be localized. This must be in the format

"helloKey" = "hello"

To display this in your code, replace all NSStrings with

self.helloLabel.text = NSLocalizedString(@"helloKey", nil);

This is the most basic form of localization. These string files can also be used but not tied to your system language. You can create a custom .strings file and call it madmob.strings for example. To display your strings, you would use this method.

NSLocalizedString(@"helloKey", @"madmob", nil);

You can apply this to your xib files aswell. You can have multiple xib files for different languages. It is also possible to extract strings from your xib files via Terminal.

ibtool --generate-strings-file madmob.strings en.lpoj/MadmobLanguageTestViewController.xib

Translate them and generate your other localized xib files

ibtool --strings-file nl.lproj/madmob.strings en.lproj/MadmobLanguageTestViewController.xib –write nl.lproj/MadmobLanguageTestViewController.xib

To extract all strings in your project, you can use the genstrings command via Terminal.

genstrings -o en.lproj *.m

Sometimes if you are getting your key returned in your strings, check that your Localization file is formatted correctly. You can also use the Terminal command.

plutil -lint madmob.strings