Search This Blog

Monday 28 May 2012

Localization in iPhone


Hello friends,

I am back after a long time. I would like to share the code of localizing your iPhone app. Its too simple if you see with minimal code.

Let's try it out.

Create a new project in xCode. now add a new file in the "Supporting Files" folder. The file type should be ".strings" and the name of the file should exactly be "Localizable.strings". Now keep thi sfile selected and go to file inspector. For going till there, go in View, select Utilities and select File inspector. a window will slide out from the right.

Now scroll down, there will be an option called localiztion. Below that there will be + and - sign for adding locale file for language of your choice.

Click on + sign add language of  your choice from the drop down. English will be by default. Let's add dutch. Now under Localizable.strings there will be two strings files, in english and in dutch.

Write the following lines in English Localizable.strings :

"name" = "My name is Riddhi Wala";
"work" = "I am a software engineer";
"friends" = "I ve got many friends - Sara, Dora";
"hobbies" = "Working, painting, reading, etc.";

REMEMBER: name, work, friends and hobbies are the keys which will remain same in all localized files. Only assigned values will change according to the language.

Now in Dutch Localizable Strings add following:

"name" = "Mijn naam is Riddhi Wala";
"work" = "Ik ben een software engineer";
"friends" = "Ik heb veel vrienden - Sara, Dora";
"hobbies" = "Werken, schilderen, lezen, enz.";

As you see the keys are same. If keys are wrong error will not be thrown but the value of the key will not be printed, instead the key will be printed.

Now  take four labels in view controller xib file and add their property in .h file. After that synthesize in .m file and then add the following piece of code in ViewDidLoad.

 The .h file code:


@interface ViewController : UIViewController

@property (retain, nonatomic) IBOutlet UILabel *label1;
@property (retain, nonatomic) IBOutlet UILabel *label2;
@property (retain, nonatomic) IBOutlet UILabel *label3;
@property (retain, nonatomic) IBOutlet UILabel *label4;

@end

The .m file code:

@implementation ViewController
@synthesize label1;
@synthesize label2;
@synthesize label3;
@synthesize label4;

- (void)viewDidLoad
{
    [super viewDidLoad];
    label1.text = [NSString stringWithString:NSLocalizedString(@"name", nil)];
    label2.text = [NSString stringWithString:NSLocalizedString(@"work", nil)];
    label3.text = [NSString stringWithString:NSLocalizedString(@"friends", nil)];
    label4.text = [NSString stringWithString:NSLocalizedString(@"hobbies", nil)];
   
}

BINGO!!

Try running your app when the language setting is English. Now change the language to Nederlands in simulator by going to Settings > General > International > Language > Nederlands. Then tap on Done button. And run your app again.  Note that if you change to any other language, your output will be in English unless you do not add the strings file of your desired language...

 
Geniet van! Een prettige dag!!


1 comment:

  1. I want to recommend an online localization tool, namely https://poeditor.com/ to better manage your iPhone app localization. It has a simple work interface and many useful features (translation memory, API, Gengo integration for human translation orders, WordPress plugin, automatic translation) that will make things easier for developers and project managers.

    ReplyDelete