NSLocale is not returning correct Language code

1.5k Views Asked by At

I'm trying to get device current selected language code by using the code below.

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

This line works fine for the first time after installation. When I change the device language from device setting, then I'm trying to retrieve device language that time NSLocale returns previously selected language.

Help to resolve this.

2

There are 2 best solutions below

0
Ladislav On

You should get it with

let currentDeviceLanguage = Locale.current.languageCode

or Objective-C

NSString *languageCode = [NSLocale currentLocale].languageCode;
0
KP26 On

I solved this issue by this line

NSString *identifier = [[NSLocale autoupdatingCurrentLocale ] localeIdentifier];

Swift :

let identifier = NSLocale.autoupdatingCurrent.localeIdentifier