Monday, July 4, 2011

Localization of nib file.

Ok. In previous we post we talked about how to get the localized string values on your .m files. LINK.

In this post, we will talk about how to localize the strings present in the nib file.

One way would be to make the connections of all the elements in the view and then set their title text with NSLocalizedString(@"key", @""); .Sometimes this is the only way to do it when the text in the nib files are dynamic.

Next way(For static strings in the nib files), recommended by Apple is following. We have created a nib file for us. And it looks like following.

Because it is in english, we will put in en.lproj directory.

 Now we open Terminal to write some commands to extract the english strings from our nib file into a new string file. By using cd I am inside the Localization folder. Following is my dir structure seen on Finder window.

I write following command in Terminal window to extract strings file.

ibtool --generate-strings-file en.lproj/HomeController.strings en.lproj/HomeController.xib

(Note: the command above will generate the HomeController.strings file in en.lproj  folder, you will have to add it in your Xcode yourself manually ) HomeController.strings  string file which looks like following:

We create the copy of  HomeController.strings in de.lproj folder and replace the english strings with german strings. Manually add it in Xcode. 
HomeController.strings  string file which looks like following:

Now we will create the  german version of the nib file by merging the german string file (I mean : de.lproj> HomeController.strings) with original nib file by using following command

ibtool --strings-file de.lproj/HomeController.strings --write de.lproj/HomeController.xib en.lproj/HomeController.xib

This  will generate german translated nib file in de.lproj folder. You add it manually in your Xcode. It looks like following:
Now we just add the HomeController in our window and run the app. You will english version of nib file when language of device is english and german when language of device is set as german.

Download the code HERE

In case, you want to know How to change Language in Simulator. LINK

No comments:

Post a Comment