Platform.device.language is returning the wrong language when I change my phone language setting


#1

If I start my application and my device (Android in this case) is set to Spanish, making a call to platform.device.language returns the ISO code for Spanish as expected. If I minimize the application, change the language of the phone to something else and then resume usage of my app, platform.device.language still returns Spanish even though I have changed it to something else like German.

If I close the application entirely and then re-open it, I’m able to get the correct language. I’ve used this same workflow in past versions of NS and have not run across this issue before. Is there a newer way of getting the device language that I’m unaware of?

I’m using the following versions:

  • Nativescript: 3.4.1
  • tns-core-modules: 3.4.0
  • tns-android: 3.4.1
  • tns-ios: 3.4.1

#2

NativeScript reads the device language when you make first call to it. It will not keep monitoring the language. On your app / activity’s resume event, directly read it from native apis, so you will get the updated language.

java.util.Locale.getDefault().getLanguage();

#3

Yeah that’s what I ended up doing for Android. I didn’t realize it wouldn’t always get the correct language, seems strange that they only do it on the first call. I don’t remember that being the case in past versions. For iOS, I’m trying
lang = NSLocale.currentLocale;


#4

NSLocale.preferredLanguages.firstObject; Ended up being the way to get the current locale setting of the device.