I have following code to switch language runtime:
-(void) switchToLanguage:(NSString *)lang{
self.language = lang;
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:self.language, nil]
forKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
And I have a Helper function that retrieves localised strings:
+(NSString *) getLocalizedString:(NSString *)key{
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSString *path = [[NSBundle mainBundle] pathForResource:@"Localizable"
ofType:@"strings"
inDirectory:nil
forLocalization:appDelegate.language];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
return [dict objectForKey:key];
}
This is working. My storyboards are also localised, but they are not changing when I switch to another language.
How can I get localised values for the storyboard strings?
Changing the Language at run time is a bit tricky.
This is the best way I've used to do so with the help of this tiny class:
Language.m:
Language.h:
When you want to change the Language:
When you want to set a string: