I'm working on an IOS app and i want to have the language changed upon pressing a button. Everything works great but the content is not refreshed when i press the language button, i can only see it when i leave the app and come back, is there a way to dynamically change the language from within the app? Here is the action i created to change the language, the app is already localized.

-(IBAction) selectLanguage:(UIButton *) currentBtn {

if ([currentBtn.currentTitle isEqualToString:@"fr"]) {
    [prefs setObject:@"fr" forKey:@"appsLanguage"];
}
else {
    [prefs setObject:@"en" forKey:@"appsLanguage"];
     } }
有帮助吗?

解决方案

You can load the localization-file from the bundle

NSBundle* locBundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj"]];

and then get the localized string like this

NSString* str = [locBundle localizedStringForKey:key value:key table:nil];

If you already use NSLocalizedString you can create a Singleton with a helper function and replace the NSLocalizedString-Makro with your function

#undef NSLocalizedString
#define NSLocalizedString(key, _comment) [[Singleton sharedInstance] yourMethodWithKey:key]

Reload/refresh your view after that and you should be good.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top