iPhone Tutorial (Part 2): Localizing your iPhone application

Please read part one (Tutorial: iPhone Localization in Xib Files) before you read this tutorial. In previous tutorial I explain how you can localized your iPhone application in Xib(nib) files. This time I explain how you can localized your iPhone application using code. So I am going to use the old project code(you can grab that from here) and localized the answer’s for the questions. In previous tutorial questions were localized inside xib files and in this tutorial the answer will be localized using code(genstrings)

Output will look like this:
t l 12 iPhone Tutorial (Part 2): Localizing your iPhone applicationt l 13 iPhone Tutorial (Part 2): Localizing your iPhone application

Follow these steps to make your application localized in French language:
1. Open LocalizediPhone Project in Xcode(source code link). Open LocalizediPhoneViewController.h file and write down this code between Import and @end
[lang = 'c']
@interface LocalizediPhoneViewController : UIViewController {
IBOutlet UILabel *lblAns_Name;
IBOutlet UILabel *lblAns_Nationality;
IBOutlet UILabel *lblAns_Profession;
IBOutlet UILabel *lblAns_Hobby;
}

@property (nonatomic, retain) IBOutlet UILabel *lblAns_Name;
@property (nonatomic, retain) IBOutlet UILabel *lblAns_Nationality;
@property (nonatomic, retain) IBOutlet UILabel *lblAns_Profession;
@property (nonatomic, retain) IBOutlet UILabel *lblAns_Hobby;
[/lang]

2. Now move to LocalizediPhoneViewController.m file and write this code, after Implementation

@synthesize lblAns_Name,lblAns_Nationality,lblAns_Profession,lblAns_Hobby;

3. Open your English and fr Xib files in Interface Builder and place 4 labels inside each of them and then bind them with your controller(LocalizediPhoneViewController).
t l 1 iPhone Tutorial (Part 2): Localizing your iPhone applicationt l 2 iPhone Tutorial (Part 2): Localizing your iPhone application

4. Now in LocalizediPhoneViewController.m file, replace viewDidLoad code
[lang ='c']
- (void)viewDidLoad {
NSLocale *locale = [NSLocale currentLocale];
NSString *titleOFApplication = [locale displayNameForKey:NSLocaleIdentifier value:[locale localeIdentifier]];
self.title = titleOFApplication;
self.lblAns_Name.text = NSLocalizedString(@”AnsName”,@”Answer Question to your name”);
self.lblAns_Profession.text = NSLocalizedString(@”AnsProfession”,@”Answer Question to your Profession”);
self.lblAns_Hobby.text = NSLocalizedString(@”AnsHobby”,@”Answer Question to your Hobby”);
self.lblAns_Nationality.text = NSLocalizedString(@”AnsNationality”,@”Answer Question to your Nationality”);
[super viewDidLoad];
}
[/lang]

5. Now go to /Applications/Utilities/ and open Terminal application. Navigate to the directly where you safe this code( In my case I place this code on Desktop). So write >cd desktop< and then >cd LocalizediPhone<. Next step is to run the ‘genstrings’ command. What it will do is, it will find all the occurrences of NSLocalizedString in our .m files in the classes folder. To run this command you will type >genstrings ./Classes/*.m<.
t l 3 iPhone Tutorial (Part 2): Localizing your iPhone application
6. Once you run the command you will see nothing on terminal screen. But if you open the LocalizediPhone project, You will see a new file called ‘Localizable.strings’.
t l 4 iPhone Tutorial (Part 2): Localizing your iPhone application
7. Open this file and you will see that it automatically generate the strings. It will look like this

/* Answer Question to your Hobby */
"AnsHobby" = "AnsHobby";

/* Answer Question to your name */
"AnsName" = "AnsName";

/* Answer Question to your Nationality */
"AnsNationality" = "AnsNationality";

/* Answer Question to your Profession */
"AnsProfession" = "AnsProfession";

8. Drag that file to Xcode in Resources folder, but when it prompts do not press on Add button. Localizable.strings files are encoded in UTF-16, which is a two-byte version of Unicode. So un-check the box that says ‘Copy items into destination group folder’ because the file is already in our project folder. Also change the ‘Text Encoding’ to ‘Unicode (UTP-16)’ form combo box. Now press ‘Add’ button.
t l 5 iPhone Tutorial (Part 2): Localizing your iPhone applicationt l 6 iPhone Tutorial (Part 2): Localizing your iPhone application
9. You can check the file in resources folder again. Now do the same thing which we did in first tutorial. Right click on localizable.strings and select ‘get info’. Now in ‘Info’ window select ‘General’ tab and click on bottom left button ‘Make File Localizable’
t l 7 iPhone Tutorial (Part 2): Localizing your iPhone applicationt l 8 iPhone Tutorial (Part 2): Localizing your iPhone application
10. Do not close the ‘Info’ window and select ‘General’ tab. Now in bottom left click on ‘Add Localization’ button and in popup write fr, press ‘Add’ button.
t l 9 iPhone Tutorial (Part 2): Localizing your iPhone applicationt l 10 iPhone Tutorial (Part 2): Localizing your iPhone application

t l 11 iPhone Tutorial (Part 2): Localizing your iPhone application

11. Close info window and expand ‘localizable.strings’ and you will see two string files (English, fr). Actually these are the same string files, so now we will change in English.strings file first. Place these inside of it

/* Answer Question to your Nationality */
"AnsNationality" = "I am Pakistani";

/* Answer Question to your Hobby */
"AnsHobby" = "I love travelling";

/* Answer Question to your name */
"AnsName" = "My name is Adeem Basraa";

/* Answer Question to your Profession */
"AnsProfession" = "I am Senior Software Engineer";

12. Now in fr.strings file replace with this:

/* Answer Question to your Nationality */
"AnsNationality" = "Je suis pakistanais ";

/* Answer Question to your Hobby */
"AnsHobby" = "J'adore voyager";

/* Answer Question to your name */
"AnsName" = "Mon nom est adeem basraa";

/* Answer Question to your Profession */
"AnsProfession" = "Je suis Senior Software Engineer ";

That’s it for now. Run your application and you will see the changes when you change the language from Setting Application inside iPhone.

t l 12 iPhone Tutorial (Part 2): Localizing your iPhone applicationt l 13 iPhone Tutorial (Part 2): Localizing your iPhone application
You can grab the code for this tutorial from here.

Please remember this note if your localization didnt work on simulator/iPhone:
[Note: If it does not show the changes we made. Do not worry about this, iPhone or iPhone simulator stored your application information in cache. So you have to remove your application from iPhone/iPhone simulator first. Then move to ~/Library/Application Support/iPhone Simulator/ ( here ~ means your home directory) and delete "User" folder. Clean your Xcode project by pressing Cmd+ Shift+K (or in menu go to "Build" > "Clean". Run the application again and it will show you the changes]

About genstrings, you can run this any time to re-create your base language file. Its good that you do not edit it by yourself. Change in your strings files inside your code and then regenerate the localizable.strings using genstrings.

Popularity: 16% [?]

My name is Adeem M Basraa. I am a software engineer from Lahore, PK. I’ve been involved in software development for nearly 4 years, with the last 1 year focused on application development for mobile devices (iPhone & Android).

11 Comments on "iPhone Tutorial (Part 2): Localizing your iPhone application"

  1. very nice tutorial, i have an application which need a localization module and thanks to your tutorial i know how to do it, thanks

    keep up the good work

  2. Rob says:

    thought you might be interested in my new (beta) translation/localisation site.

    Once you have marked your localisable strings with the NSLocalizedString macro, then you can use my helper to do pretty much all the rest of the process you describe.

    -Plus, it uploads your strings to a web server where your users can provide translations for you and handles downloading strings in the languages you choose and integrating them straight back into the project.

    -It handles xib files too!

  3. admin says:

    @Rob
    Is your API free of cost?

  4. Waqas Qurehsi says:

    Hi adeem, thanks for sharing Localization tutorial, it’s work fine but when I am again add some more keys in localizable.strings (file) & Run on an iPhone then it didn’t show any change on it.If anyone know, so please guide me , I think I am doing something wrong with it .
    Thanks in Advance

  5. stk says:

    What does [lang ='c'] mean?

  6. Adeem Basraa says:

    actually a plugin issue … sorry for that.. will update the blog post. Thanks Sean for pointing out :)

  7. iDeveloepr says:

    I need help for localization for languages US , GB ( Great Britain) &
    AU ( Australia ) for iPhone SDK.

    I created 3 different folder en_US,en_AU & en_GB & localized
    “Localizable.string” file in all three respective folders.

    Now When I set device language to English & Regional Format to US or
    AU or GB through Device Settings & install my project, it will not
    take respective localized strings as well as images.

    I search alot but not success till yet. so is it possible to
    differentiat resource between US,GB & AU for iPhone SDK ? Help is
    appreciable. Thanks in Advance….

  8. I thought I would leave my first comment, Thank you and I will want to read more from you.

  9. pravara says:

    Hi,
    I have created a localizable.strings file, also imported it in resourses folder in xcode.
    now, when i try to make it localizable by opening the “Get info” pop-up, it displays blank strings files for both English and French languages.

    i m not able to figure out the reason behind this kind of behaviour.

    the process works very well in my sample application.

    please help me as soon as possible.

  10. Raghu says:

    Hi,
    Thanks a lot for your valuable post. Right now i am unable to generate Localizable.Strings files using genString tool!am getting the error as follows:
    “Bad entry in file ChatWindowController.m (line = 17): Argument is not a
    literal string.”
    what lemme know the clear procedure regarding this

Trackbacks for this post

  1. Tutorial for iPhone | Localized your iPhone application | Localization of your iPhone application | Different language for your iPhone applciation | iPhone Diary

Got something to say? Go for it!