change NSLocale/Language of iPhone programmatically in running app

I have an issue. I had created a view, where user can select Language of application among English, Dutch & Spanish. Then texts of UILabel in next view should be according to selected language.

I have used following code to set current language :

 NSLog(@"%@", NSLocalizedString(@"Loading... Please wait...", nil));

 int index = 2;
 NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
 NSArray* languages = [userDefaults objectForKey:@"AppleLanguages"];

 switch (index) 
 {
    case 0:
        languages = [NSArray arrayWithObjects:@"en",@"de",@"es", nil];
        break;

    case 1:
        languages = [NSArray arrayWithObjects:@"de",@"en",@"es", nil];
        break;

    case 2:
        languages = [NSArray arrayWithObjects:@"es", @"en", @"de", nil];
        break;

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

   NSLog(@"%@", NSLocalizedString(@"Loading... Please wait...", nil));

** And in Localizable.strings (Spanish) : ** "Loading... Please wait...” = “Loading... in Spanish”;

** And in Localizable.strings (German) : ** "Loading... Please wait...” = “Loading... in German”;

I have used following code to get current language in next view :

 NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];
 NSArray* languages = [defs objectForKey:@"AppleLanguages"];
 NSString* preferredLang = [languages objectAtIndex:0];
 NSLog(@"preferredLang: %@", preferredLang);


 NSLog(@"%@", NSLocalizedString(@"Loading... Please wait...", nil));

But it prints "Loading... Please wait...” in Console. It should print "Loading... in German” instead.

Is am going wrong somewhere ?? Can I change language of application while application is running ??

Answers


These two are best tutorials first second I have came across.


Need Your Help

IntelliJ IDEA 10.5 error in R.java when adding images!

android intellij-idea r.java-file

I create a new Android project in IntelliJ IDEA 10.5 (IC edition), register Android library and all goes fine. Then I add /drawable directory to /res directory and place an image inside of it. If I...

placing popViewControllerAnimated: in viewDidLoad or viewDidAppear will not work

iphone objective-c ios

I have a view (viewB) that is pushed in using the navigation controller from another view (viewA) using pushViewController as usual, however for some reason, I want viewB's controller to pop the v...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.