-2

i have a option like arabic and english in homepage.if the user selects a arbic then the entire app will be show arbic language.i have heared about nslocalizedstring also i have found a solution for this at here. i am not clear how to show a enitre app.i may have ten viewcontroller then how i will change from home viewcontroller.how to make global file for this.

thanks in advance.

karthikeyan
  • 3,821
  • 3
  • 22
  • 45
  • http://stackoverflow.com/questions/34130853/how-to-force-an-app-to-change-language-in-ios-objective-c/34131794#34131794 – user3182143 Dec 09 '15 at 08:36

2 Answers2

2

Try this tutorial it goes through the whole configurations and files you need to create. You will not have to change the 10 ViewControllers language from home ViewController, all you need is to set the user chosen language in the NSUserDefault values and check it later in your ViewControllers if you have to do a specific action based on the language.

This sets the language to Arabic

NSUserDefaults*userDefaults=[NSUserDefaults standardUserDefaults];   
[userDefaults setObject:[NSArray arrayWithObjects:@"ar", @"en", nil];

and this for English

[userDefaults setObject:[NSArray arrayWithObjects:@"en", @"ar", nil];

Then later in your ViewControllers you can check using this

- (NSString*) getLanguage{

  NSArray* languages = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"];

  NSString *preferredLang = [languages objectAtIndex:0];

  return preferredLang;
}

The storyboard and NSLocalizedString know which language it should load from their prospective string files automatically using the value you sit for the AppleLanguages key.

Community
  • 1
  • 1
Radwa Ebrahim
  • 337
  • 2
  • 8
1

NSLocalizedString works by following the OS language setting. So you create a strings file for each language instead of hardcoding your strings. Then the app will automatically display the correct language based on the users device settings. I don't think (although I could be wrong) this approach works with a button. You may be able to load the strings files manually on the button press. The other solution is an app full of if statements to load the correct language.