0

I have requirement like below: In my application I have provided list for language. Now when user change language then it should change all Titles and messages to particular language. Actual flow is like, I have one Screen (view) for setting in my app. In that setting screen I have provided language option.Now when user select language then in my application all Titles, Messages should get displayed in selected language without starting my app.

Now issue is when I select language for example Spanish then it is not taking effect without restarting my app. I have used function as given below:

-(void) SetUserLanguage
{
    NSString *appDataFilePath = [[NSBundle mainBundle] pathForResource:@"UserLanguageList" ofType:@"plist"];
    NSDictionary *appDataDictionary = [[NSDictionary alloc] initWithContentsOfFile:appDataFilePath];
    NSString* langName = [appDataDictionary objectForKey:self.pstrUserLanguage];
    if(langName == nil || [langName isEqualToString:@""])
    {
        langName = @"en";
    }
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSArray *langArray = [defaults objectForKey:@"AppleLanguages"];
    NSArray *currentLangArray = [NSArray arrayWithObject:langName];
    [defaults setObject:currentLangArray forKey:@"AppleLanguages"];
    [defaults synchronize];
}

above function is part of one interface called UserDataManager and instance of that is available globally for application using gpUserDataManager. So on selection of language I set language to this structure and then I have my predefined plist file for mapping.So when user select language Spanish it set value "es" to preferred language in above function. But to have effect of this change I need to restart my app. Kindly help how to achieve my requirement.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Premal
  • 551
  • 1
  • 6
  • 25

2 Answers2

2

TSLanguageManager will let you do what you ask. The usage is really simple;

When you want to change the language like by tapping a UIButton, just use the following code to change the language;

[TSLanguageManager setSelectedLanguage:kLMEnglish];

To change the text according to the new language,

yourTextField.text = [TSLanguageManager localizedString:@"yourLocalizedString"];

Please note that you need to keep all the localized strings in Localizable.strings file.

Engnyl
  • 1,380
  • 16
  • 24
  • It´s ok for me. Only say than in iOS7 [userDefaults objectForKey:@"AppleLanguages"] return "es" and in iOS9 return "es-ES" and it´s necessary to control in TSLanguageManager:selectedLanguage – Miguel Nov 15 '15 at 18:26
0

If you want to avoid restarting your app, take a look at this answer.

You basically need to use a different bundle, so use NSLocalizedStringFromTableInBundle instead of NSLocalizedString macro, and give it the correct bundle for the language chosen.

Community
  • 1
  • 1
HyBRiD
  • 688
  • 4
  • 23