Bug 3471 - RegionInfo.CurrentRegion is null
Summary: RegionInfo.CurrentRegion is null
Status: RESOLVED FIXED
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: 5.2
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Sebastien Pouliot
URL:
Depends on:
Blocks:
 
Reported: 2012-02-15 08:21 UTC by Tim Dawson
Modified: 2012-02-17 13:48 UTC (History)
3 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Tim Dawson 2012-02-15 08:21:13 UTC
This static property should never return null, and currently it does, leading people from lots of places around the world unable to use our deployed app. We will of course work around this, but it needs to be fixed.

To reproduce, on an iPad go into Settings -> General -> International and change the region to Hong Kong SAR China. I've also had reports that Belgium is affected but have not yet substantiated.

Thank you.
Comment 1 Sebastien Pouliot 2012-02-15 10:21:52 UTC
Confirmed. "Hong Kong SAR China" and "Belgium" (likely others, but "Canada" was fine) presently returns null.
Comment 2 Sebastien Pouliot 2012-02-15 13:11:48 UTC
The construct_internal_locale_from_current_locale (in CultureInfo) internal call fails and Mono fallback to the InvariantCulture, which in turns returns a null RegionInfo.
Comment 3 Sebastien Pouliot 2012-02-15 17:15:23 UTC
iOS creates the locale from the current language + the international settings. So an English device using "Hong Kong SAR China" will return "en-HK".

Mono (and .NET) does not know about this (many "en-*" but only "zh-HK") and you can't create a "en-HK" CultureInfo on .NET (MS or Mono). That's why this presently fallbacks to an invariant culture (leading to the null RegionInfo). 

Part of this situation can (and will) be fixed but, even then, the result won't always be 100% identical to iOS since the culture/region tables (both the types and values of data) inside Mono also differ from the ones from iOS.

I'm not sure what you need from RegionInfo but if it's available from NSLocale [1] then I suggest you to use it (both as an immediate solution and for consistency with iOS settings).

[1] http://docs.go-mono.com/?link=T%3aMonoTouch.Foundation.NSLocale
Comment 4 Tim Dawson 2012-02-16 04:19:58 UTC
In our particular case, we're simply looking at the two-letter ISO country name of where the user is in order to present sensible defaults. The most important thing here is that RegionInfo.CurrentRegion is not null. As long as it returns something reasonably sensible, then the situation will be much improved.

I suggest somebody needs to create a table of mappings between iOS region settings and available .net cultures that is part of the spec for MonoTouch.
Comment 5 Sebastien Pouliot 2012-02-16 08:56:31 UTC
You can get the two letter code from:
    NSLocale.CurrentLocale.CountryCode
and it will match the current iOS settings and works in existing versions of MonoTouch.

RegionInfo will be fixed but since it's definition (the properties it offers) and data (values) differs from iOS there will always be small mismatch between the two (but you can safely assume the two letters ISO code to be identical since it's the initialization key).
Comment 6 Tim Dawson 2012-02-16 14:07:31 UTC
Thanks, that seems like a reasonable workaround for us. I wonder if it's safe to assume that NSLocale.CurrentLocale will never be null :)
Comment 8 Sebastien Pouliot 2012-02-17 13:45:20 UTC
Newer MonoTouch version (5.3+) will initialize RegionInfo.CurrentRegion from NSLocale.CurrentLocale.CountryCode if no Mono/.NET data is available for the current culture. 

That should ensure RegionInfo.CurrentRegion won't ever return null on MonoTouch.