简体   繁体   中英

NSLocale is not returning correct Language code

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.

You should get it with

let currentDeviceLanguage = Locale.current.languageCode

or Objective-C

NSString *languageCode = [NSLocale currentLocale].languageCode;

I solved this issue by this line

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

Swift :

let identifier = NSLocale.autoupdatingCurrent.localeIdentifier

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM