Switching languages using localised strings runtime-Collection of common programming errors
So I have set up my Localizations accordingly and it works when I change the language in settings then start the Application. However, I wish to give the ability or option, to also change the language within the app runtime, and I am trying to do it using NSLocalizedString(key,cmt)
function and am having trouble getting it to actually work. In my code shown below I change the user language defaults which then if I check [[[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"] firstObject]
after the change it has changed it successfully.
However the localized string it returns is still the the language it was set before, i.e the language stays the same…no change. although if I restart the app the labels all change so, there must be some sort of concurrency issue or something I am not aware of, I am especially suspicious of this due to the word synchronize being used in the code. Any suggestions would be most welcome.
- (void)SetLanguage:(NSString*)lang
{
//en
//zh-Hans
//zh-Hant
NSLog(@"%@",[[[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"] firstObject]);
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:lang, nil] forKey:@"AppleLanguages"]; //switching to polish locale
[[NSUserDefaults standardUserDefaults] synchronize];
for ( int i = 0; i < [_menuButtons count]; ++i )
{
StylizedButton *button = [_menuButtons objectAtIndex:i];
StyleLabel *viewTemp = (StyleLabel*)[button viewWithTag:1001];
[viewTemp setText:NSLocalizedString([button localizedKey], @"")];
}
}