Friday, April 8, 2011

Localization in iPhone

In this post we shall discuss about localization and how it is achieved in iPhone.

What is localization: The iPhone device is capable of presenting the UI in the language that is the user’s locale (his current language) and when I say locale I mean that let say the applications UI is made in English but as per the locale the application will be able to change its UI’s text. For example you must have seen different website they give support to localization like if the site is open in USA their UI’s text will be in English and when the same site is opened in Germany the UI text will change from English to german. This is called localization, now as per your needs you can allot this feature into your application.

Design Phase: For this demo I have used one single label and when I will change my locale from English to france the text present in my label will change based upon my locale that I have set.

Step 1: Open Xcode and select the windows based application template and then add a subclass of UIViewController and name it myviewController, in this class add an instance of the UILabel class and give it any text of your choice,

Step 2: Now we will be adding a localizable string into our project, and this string will help us to manipulate the localization in our project,
Click on resource -> add -> new file -> from the mac os x template select other -> you will see a string file their, save this file with the name “Localizable.strings”. Now you will see that this file has been added into your resource group

Step 3: Once you have added the Localizable.string file into your resource group, right click on that file and select get info their will be a screen in front of you which will look just like the one given below,

from their select the option make file localizable you will be traversed to the target tab make sure you navigate back to the general tab once you are in the general tab you will see a screen just like below.

Select add localization to add localization and enter the name of the localization that you wish to add in this case I have selected france that’s fr.

Once you add the localization that you want you will see that under the localizable.string file you have two files one is the current locale that you are working and the other is the localization that you want.

Step 4: In these two files we will keep the data in the key value pair so that we will give the key and the value will be directly accessed from the localizable.string file based upon the current user locale, here’s the code that I have wrote in both of my files.

Code in the English File

"LabelText" = "Hi, i am radix";

Code in the fr file

"LabelText" = "Salut, je suis radix";

In both the file the "LabelText" is acting like a key, and in the myviewController.m the control for which you want to assign the localization just add this function in front of it NSLocalizedString(“key”,”comment”);

lbl.text = NSLocalizedString(@"LabelText", @"some comment");

Step 5: Now in order to test this application all you need to do is run the iPhone simulator and select the settings -> General -> International -> Language -> here you will get a list of language select the language for which you have performed the localization and then select done and after this run your application. Here's the final output

English locale output

French locale output

No comments:

Post a Comment