Search code examples
c#c#-4.0globalization

RegionInfo throws exception in Windows 8.1 app


I have the following code:

 var culture = CultureInfo.CurrentUICulture;
 var regionInfo = new RegionInfo(culture.TwoLetterISOLanguageName);

If CurrenUiCulture is Hebrew with ISOLanguageName "he" throws an exception

The region name he should not correspond to neutral culture; a specific culture name is required.

It works with other cultures like arab, spanish, chinese... What's wrong?


Solution

  • he is a region neutral culture as shown by this code

    new CultureInfo("he").IsNeutralCulture; // true
    

    The RegionInfo that takes an int constructor words the exception in a different way that I found easier to understand.

    new RegionInfo(new CultureInfo("he").LCID) // Culture ID 13 (0x000D) is a neutral culture; a region cannot be created from it.
    

    Try with a more specific culture name, for example he-il for Israel.

    new RegionInfo("he-il") does not throw an exception.

    As for your other examples...

    • es is both the region-neutral language code for Spanish AND the country code for Spain (RegionInfo also accepts a country code) so that's why it works
    • ar is both the region-neutral language code for Arabic AND the country code for Argentina (so it "worked", but you will not get expected results)
    • zh is the region-neutral code for Chinese and it should not have worked. Though if you tried cn it worked because it's the country code.