{"id":2464,"date":"2022-08-30T15:25:05","date_gmt":"2022-08-30T15:25:05","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/01\/17\/switching-languages-using-localised-strings-runtime-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:25:05","modified_gmt":"2022-08-30T15:25:05","slug":"switching-languages-using-localised-strings-runtime-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/switching-languages-using-localised-strings-runtime-collection-of-common-programming-errors\/","title":{"rendered":"Switching languages using localised strings runtime-Collection of common programming errors"},"content":{"rendered":"<p>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 <code>NSLocalizedString(key,cmt)<\/code> 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 <code>[[[NSUserDefaults standardUserDefaults] objectForKey:@\"AppleLanguages\"] firstObject]<\/code> after the change it has changed it successfully.<\/p>\n<p>However the localized string it returns is still the the language it was set before, i.e the language stays the same&#8230;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.<\/p>\n<pre><code>- (void)SetLanguage:(NSString*)lang\n{\n    \/\/en\n    \/\/zh-Hans\n    \/\/zh-Hant\n\n    NSLog(@\"%@\",[[[NSUserDefaults standardUserDefaults] objectForKey:@\"AppleLanguages\"] firstObject]);\n\n    [[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:lang, nil] forKey:@\"AppleLanguages\"]; \/\/switching to polish locale\n    [[NSUserDefaults standardUserDefaults] synchronize];\n\n\n    for ( int i = 0; i &lt; [_menuButtons count]; ++i )\n    {\n        StylizedButton *button = [_menuButtons objectAtIndex:i];\n        StyleLabel *viewTemp = (StyleLabel*)[button viewWithTag:1001];\n        [viewTemp setText:NSLocalizedString([button localizedKey], @\"\")];\n    }\n}\n<\/code><\/pre>\n<p id=\"rop\"><small>Originally posted 2014-01-17 07:09:05. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2464","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2464","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2464"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2464\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2464"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2464"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2464"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}