Wednesday, February 16, 2011

Localizing an iPhone App




Introduction

Internationalization is the process of designing and building an application to facilitate localization. Localization, in turn, is the cultural and linguistic adaptation of an internationalized application to two or more culturally-distinct markets. When users launch a well-localized application, they should feel fully at home with it and not feel like it originated from some other country.
Internationalization and localization are complementary activities. By internationalizing an application, you create the programmatic infrastructure needed to support localized content. By localizing that application, you then add resources that are customized for people of a different culture

Steps below reflects localization only,


a. When app is closed & restarted
b. All the localized text is set using code not using your nib files

Steps for integrating Localization in an App


1. First of all add new file





Now you can see the added file in Xcode as shown in the picure below


2. We need to add localization to this file

For this,
a) Select .strings file
b) Right click on the file & select getInfo, you will see something like




3. Now, select the bottom button ( Make File Localization ), you can see the screen as shown in the picture below



4 . Now, select the General Tab on top (in the above picture)



5. Here you can add the languages you want to support, by clicking on bottom button ( Add Localization)
Now, you can see the files as shown in the picture below


6. Next step is to set text in the localized file

In the string file, add

/* Questions */ 
"Questions" = "test2";

/* Settings */
"Settings" = "test3";


Left part (Questions) act as key & right part act as corresponding value (test2)


7. Using localized text in the code,

For example you want to set a label's text, then use

lbl.text = NSLocalizedString(@"Questions", @"Questions");



As you can see in the above picture value for key "Questions" is " test2", so for English the label's text would be "test2"



8. In .main, add the below code

int main(int argc, char *argv[]) {
 [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
 NSArray *arr = [[NSMutableArray alloc]initWithObjects: @"en",
 @"de",
 @"fr",
 @"es",
 @"it",
 @"ja",
 @"nl",
 @"pt",
 @"pt-PT",
 @"da",
 @"fi",
 @"nb",
 @"sv",
 @"ko",
 @"zh-Hans",
 @"zh-Hant",
 @"ru",
 @"pl",
 @"tr",
 @"uk", nil];

 if(![[[NSUserDefaults standardUserDefaults] objectForKey:@"Localization"] isEqualToString:@"NO"]){

 NSArray* preferredLangs = [NSLocale preferredLanguages]; 
 NSLog(@"%d",[preferredLangs count]);

 if (!([[preferredLangs objectAtIndex:0] isEqualToString:@"en"] 
 ||[[preferredLangs objectAtIndex:0] isEqualToString:@"es"] 
 ||[[preferredLangs objectAtIndex:0] isEqualToString:@"de"] 
 ||[[preferredLangs objectAtIndex:0] isEqualToString:@"fr"]
 ||[[preferredLangs objectAtIndex:0] isEqualToString:@"it"]

 )){

 [[NSUserDefaults standardUserDefaults] setObject:arr forKey:@"AppleLanguages"];
 }

 }else{
 [[NSUserDefaults standardUserDefaults] setObject:arr forKey:@"AppleLanguages"];
 }

 [arr release];
 int retVal = UIApplicationMain(argc, argv, nil, nil);
 [pool release];
 return retVal;
}

Above example supports english(en), spanish (es), German (de), French (fr) & italian (it)


9.Providing language change option, something like changing  language on row selection

Auto detects device default language.It is the first row in the code below

In the code,




- ( void)tableView:(UITableView *)tableView1 didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

 UITableViewCell *cell=[table cellForRowAtIndexPath:indexPath];
 UILabel *lbl=(UILabel *)[cell.contentView viewWithTag:101];
 NSUserDefaults *def = [NSUserDefaults standardUserDefaults];

 if(useLocalization == YES){

 NSIndexPath *indexPath1 = [NSIndexPath indexPathForRow:appDel.languageRow inSection:0];
 UITableViewCell *cell1=[tableView1 cellForRowAtIndexPath:indexPath1];
 UIImageView *imgView=(UIImageView *)[cell1.contentView viewWithTag:102];
 imgView.image=nil;

 NSArray *lang;

 if(indexPath.row == 0 || indexPath.row == 1 || indexPath.row ==2 || indexPath.row ==4 || indexPath.row == 3 || indexPath.row == 5){

 [appDel setLanguageRow:indexPath.row];

 [def setInteger:appDel.languageRow forKey:@"languageRow"];

 appDel.languageUsed = lbl.text;
 [def setObject:appDel.languageUsed forKey:@"languageUsed"];

 if(indexPath.row == 0){

 [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"AppleLanguages"];

 }

 if(indexPath.row != 0){

 if(indexPath.row == 1)

 lang = [NSArray arrayWithObjects:@"en", nil];

 else if (indexPath.row == 2)

 lang = [NSArray arrayWithObjects:@"de", nil];

 else if (indexPath.row == 3)

 lang = [NSArray arrayWithObjects:@"es", nil];

 else if (indexPath.row == 4)

 lang = [NSArray arrayWithObjects:@"fr", nil];

 else if (indexPath.row == 5)

 lang = [NSArray arrayWithObjects:@"it", nil];
 }

 if(indexPath.row !=0)
 [[NSUserDefaults standardUserDefaults] setObject:lang forKey:@"AppleLanguages"];

 }

 [tableView1 reloadData];

 [NSUserDefaults resetStandardUserDefaults];
 }

}

Please create the necessary variables in the app delegate.In the above code , I am just changing the language on row selection.

No comments:

Post a Comment