Monday, November 22, 2010

Best Practice Localization/Internationalization Xcode iPhone project - Localizable.strings

This article is about how to create a Localizable.strings file for an Xcode iPhone project, in order to provide translations for Strings that were used within the class files. Also common problems that can accure when dealing with internationalization of an xcode project are discussed.

I also wrote an article about how to add localization for .xib files (NIB-files).

1. Create Localizable.strings files for all supported languages


1. In any xcode iPhone project right-click on the Resources folder and select "Add->New File...". 2. Under the available iPhone templates is no .strings file available so we select "Mac OS X -> Resource -> Strings File" and click Next.
3. Name the file Localizable.strings.
4. Right-click on the file in the Resource folder and select "Get Info".
5. Under "General" click "Make file Localizable".
6. Then again under "General" repeat "Add Localization" for all languages you want to support.

The result for the localized Localizable.strings file in the Groups & Files view of Xcode should look like this:



2. Change plain Strings in class files to the NSLocalizedString macro


Everywhere in the .m files where you are using NSStrings (e.g. @"text";) you need to replace the part with NSLocalizedString(@"textKey", @"comment");.

If you don't want to fill in a comment with your key leave the according filed nil.
The textKey will later be looked up in the according strings file for the language of the iPhone user and replaced with the translated text.

Example entry in a Localizable.strings file:
/* comment */
"exitLabel" = "Exit";

3. Automatically extracting the key's into the Localizable.strings files


Often you can find on the Web that you should create an en.lproj, de.lproj, fr.lproj folder etc. to hold the Localizable.strings files. When we perform our first step Xcode generates English.lproj, German.lproj, French.lproj folders and places the Localizable.strings files in.

That is for newer versions of Xcode, in my case Xcode 3.2.4.

To extract all the keys used in your .m-files run the below command that will write the keys with the according command in the according Localizable.strings files. You have to run the command in the command line of your console and you have to be in the according xcode project.

genstrings -o English.lproj *.m
genstrings -o German.lproj *.m
genstrings -o French.lproj *.m

Important! This command will override the old entries. Be aware and save the old file.

Now you are good to go and when you Build and Run your app the changes should be applied.

Problem: The Localizable.strings file contains inverted question marks


This can happen when the according file is not in UTF-16. You can solve this problem in Xcode though. Right-click on the file and select "Get Info". There select File Encoding UTF-16, when a popup asks you: "Do you want to convert the file to the new encoding or reinterpret it using the new encoding?" Select "Convert". That's it.

Note: Sometimes I have to run the genstrings commands again in order that the encoding works, after I changed it.

Problem: NSLocalizedString only returns the key


Try to:

1. Make sure you got the case right for each character in the key.
2. Clean the Target.
3. Remove the app from the iPhone emulator. (Hold left mouse button down on any app icon until the icons wiggle. Click x for delete. Click iPhone Home button.)
4. Build and Run again.

Sometimes deleting the build folder could also help.

1 comments:

Greg David said...

Hi! To easily localize your strings, I suggest using a software localization tool like POEditor that can improve your workflow.

Post a Comment