1

I'm new in developing iOS app. So I've recently faced a problem with localization of my app. I would like to perform some actions in response to language change in system settings. To get language ID I use

[[NSLocale preferredLanguages] objectAtIndex:0];

in applicationDidFinishLaunchingWithOptions method. First time it returns language ID right (e.g. "en" for English). Then, I close my application (or send it to background) and change language to German. After that I restart my app, but instead of "de", it still returns "en". I think my app might probably store old settings somewhere... but where exactly? How to retrieve valid current value?

Yarlik
  • 216
  • 1
  • 8
  • I found the source of my problem. It was iOS Simulator. I installed my app on device and all problems disappeared. – Yarlik Mar 30 '11 at 12:31
  • if this solution solved your problem then you should write an answer to your question and accept it as the correct answer (not only a comment). – antf May 26 '12 at 09:43

3 Answers3

0

I found the source of my problem. It was iOS Simulator. I installed my app on device and all problems disappeared.

Yarlik
  • 216
  • 1
  • 8
0

take a look at this old answer:

Getting current device language in iOS?

EDIT:

NSString *testLang = [[NSLocale preferredLanguages] objectAtIndex:0];
NSString *testLang1 = [[NSLocale preferredLanguages] objectAtIndex:1];
NSString *testLang2 = [[NSLocale preferredLanguages] objectAtIndex:2];
NSString *testLang3 = [[NSLocale preferredLanguages] objectAtIndex:3];
NSString *testLang4 = [[NSLocale preferredLanguages] objectAtIndex:4];
NSLog(@"LINGUA SYS:%@:%@:%@:%@:%@",testLang,testLang1,testLang2,testLang3,testLang4);

in debugger console:

LINGUA SYS:en:it:fr:de:ja

or:

LINGUA SYS:es:en:it:fr:de

or:

LINGUA SYS:pt-PT:en:es:it:fr

so the code you posted is right, the error should be somewhere else...

Community
  • 1
  • 1
meronix
  • 6,175
  • 1
  • 23
  • 36
  • I had done some homework before decided to ask my question. I had read that topic. It's useful, but does not solve my problem. The call '[[NSLocale preferredLanguages] objectAtIndex:0];' returns the same value before and after language change. I tested this function in small test app, and it worked correctly... But still I cannot understand, why it doesn't work in my main application. Anyway, thank you for your answer. – Yarlik Mar 30 '11 at 07:44
  • @user68...: mhm, that's has no sense to me... i tried your line of code and it changes the returned value correctly, and so if i ask for objectAtIndex:1 (or 2, 3...) i get always "en" for 1 (not in case en is in 0, of course) and then for 2, 3... the language i had in 0 before my last change... so the error for you may be somewhere else... are you sure you are checking it in the right way, where do you put the value you get form that line of code? i add new edit in my answer... – meronix Mar 30 '11 at 08:14
  • To my mind the problem is in some project settings. I found another topic with similar question [link](http://stackoverflow.com/questions/4294597/how-to-prevent-need-for-rebuilding-app-after-changing-language-region) – Yarlik Mar 30 '11 at 08:18
  • yes, i do agree... could you post a capture of your app-info.plist? – meronix Mar 30 '11 at 08:21
  • I posted a screenshot here [screenshot of .plist](http://img683.imageshack.us/i/plistscreenshot.png/) – Yarlik Mar 30 '11 at 08:41
  • ...similar to mine... sorry, it should work... did you try the "NSLocalizedString" way? NSLocalizedString(@"Lang", @""); (you need to localize a text file called "Localizable.strings") with this content:/* Lang */ "Lang" = "eng"; (<- for the english file... change "eng" with "ita", "fr"... for every lang you intend to support...) – meronix Mar 30 '11 at 09:12
  • Yes, I did, It was a part of my task. I have Localizable.strings files for four languages. But I need to rebuild my app to force my app to change language. I also localized the name of my app and splash screen and they work correctly without rebuilding. It is getting a bit clearer now. I will try to find out where my app saves the value of current language and why it does this. Thank you for your help. – Yarlik Mar 30 '11 at 10:13
0

if you do something like that:

[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"en",@"es", nil] forKey:@"AppleLanguages"];

the value is stored on NSUSerDefaults and changes on settings don't apply.

EdChum
  • 376,765
  • 198
  • 813
  • 562
Eren
  • 1
  • 1
  • 1
    Thanks for your concern, but read the comment to my question. I've already solved this problem. – Yarlik Jun 08 '11 at 06:49