Can I use System.Globalization's RegionInfo.CurrentRegion for detecting user's region in device for a xamarin forms based project? When I used it, the region detected was based on the device language I had set in the device settings. If I should't use it, please lead me in the right direction to implement user's region detection geographically. NOTE: 1. I also have a requirement to set the app language based on device language. (Somewhere I saw that regioninfo detects region based on the culture set in cultureinfo) 2. I basically want to detect whether a user is geographically in Europe or not
It will be very good if some experienced person points the direction.
You should be fine...
iOS & macOS are special-cased within the framework in that it obtains the CurrentRegion
via a native call to
NSLocale.CurrentLocale.CountryCode
re: https://developer.apple.com/documentation/foundation/nslocale?language=objc
So it is impossible not to get an ISO country code as defined within iOS as it is a part of the setup of the OS itself and is not skippable (I do not believe iOS|macOS even support a way to define a invariant culture at a native OS level)
On Android, the default Mono/.Net code is executed and the country is extracted:
Thread.CurrentThread.CurrentCulture
Note: The Mono runtime itself is determining the initial setting of the thread culture (which of course is programmatically changeable by your code)
So, assuming the device's locale is set to a known country you will be fine to use RegionInfo.CurrentRegion
.
But you should be aware that CultureInfo.InvariantCultureId
is a valid culture and thus a return of an ISO country IV
is possible:
iso2Name = "IV";
iso3Name = "ivc";
win3Name = "IVC";
nativeName = englishName = "Invariant Country";
Personally, I do not known of a Euroupean country that is not covered by an direct ISO code that is not covered by an Android device (but there are a LOT of Android OEMs across Europe and Asia, so maybe(?) you can get an IV
return...).
I got it working by adopting the below methods for my xamarin.forms app
For detecting location, I used Xamarin.Essentials geolocation plugin for Android and iOS - https://learn.microsoft.com/en-gb/xamarin/essentials/geolocation?context=xamarin%2Fandroid&tabs=android
For detecting device language, I stumbled upon official localization Xamarin docs that helped me a lot:
Android - https://learn.microsoft.com/en-us/xamarin/android/app-fundamentals/localization
iOS - https://learn.microsoft.com/en-gb/xamarin/ios/app-fundamentals/localization/#language
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With