5

I'm trying to get the correct language in my device (is NOT in the SIMULATOR) with the following code:

 NSString * languageLocale = [[NSLocale preferredLanguages] objectAtIndex:0];

And it is always 'en' but my current language is set to Spanish

Any thoughts why is retrieving always 'en' and not the current device language?

NobodyNada
  • 7,529
  • 6
  • 44
  • 51
ZetaPR
  • 964
  • 7
  • 32
  • Aren't the *preferred* language and the *device* language different things? – trojanfoe May 24 '16 at 14:39
  • According to this question no: http://stackoverflow.com/questions/3910244/getting-current-device-language-in-ios – ZetaPR May 24 '16 at 15:18
  • Why are you fetching the language identifier from NSLocale? What do you need it for? – wakachamo May 24 '16 at 18:52
  • To identify the language of the device and set it in the User settings, in my app the language can be change it but the first language, the on is selected by default, is the one from the device, so the one from NSLocale. – ZetaPR May 25 '16 at 06:17
  • This is a very confusing user experience. You should not have an in-app language switcher; users expect all their apps to follow the language chosen in Settings. – wakachamo May 25 '16 at 21:47
  • You're absolutely right, I will follow this approach. Thank you! – ZetaPR May 30 '16 at 09:29
  • 1
    I am still facing a problem while trying to do this, I've change my device into english but my app is not recognizing the language. Is still saying that is in 'en' – ZetaPR Jun 03 '16 at 08:59

2 Answers2

5

I've to face the same issue. In my case, it was solved by setting the Application Language as System Language in Edit Scheme.

Here is the way:

Edit Scheme -> Options -> Application language -> System Language.

Ramakrishna
  • 712
  • 8
  • 26
1

After a few research, I found out that the key AppleLanguages was being overwritten. So, the

NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
[defaults objectForKey:@"AppleLanguages"];

It was returning a dictionary list with the overwritten data. I just did this:

NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
NSDictionary * dict = [defaults dictionaryRepresentation];
for (id key in dict) {
    if([key isEqualToString:@"AppleLanguages"]){
        [defaults removeObjectForKey:key];
    }
}
[defaults synchronize];

In order to delete the whole key and the system reset it and return the correct list.

ZetaPR
  • 964
  • 7
  • 32
  • Something in your app is overwriting that key, which should probably be removed. – wakachamo Jun 08 '16 at 23:49
  • Yes, I found out where, but the thing is the app is already in the AppStore and the variable was already overwritten so I manage to do this in order to keep the app working as it should be working. – ZetaPR Jun 09 '16 at 07:05
  • @wakachamo who overrides the value? and why? my app has no such statement to override it. – Sazzad Hissain Khan Mar 13 '20 at 12:30
  • hi @SazzadHissainKhan it's been a while since this question. What are you trying to achieve? – ZetaPR Mar 15 '20 at 11:05
  • @ZetaPR I am facing the similar issue. Nothing works cz value for key AppleLanguages are being overwritten. I need to know why its being overwritten? – Sazzad Hissain Khan Mar 15 '20 at 11:36
  • Well in case of this applications was a legacy one, someone decided that in the main part of the application the Apple Languages keys were overwritten in toder to let decide the user inside the app whether to change or not the language. Despite that guidelines are clear to not do this, the key was already overwritten I had to check the dict to reset this by removing first the code in my main that do the AppleLanguage selection and override and then doing this in order to reset the dictionary back to normal – ZetaPR Mar 15 '20 at 12:04