2

I have a web application in which I set the CultureInfo on Thread.CurrentThread to ar-IL (Arabic-Israel):

Thread.CurrentThread.CurrentCulture = new CultureInfo("ar-IL")

The problem is that on my local machine (Windows 10 with IIS 10) it works just fine and no exception is being thrown. However, on Azure (Windows Server 2012 R2 with IIS 8.5) it throws CultureNotFoundException:

Culture is not supported. Parameter name: name 'ar-IL' is an invalid culture identifier

I checked the source of CultureInfo and realized that the native call to nativeInitCultureData() is the culprit. On my Windows 10 machine it returns true but on Windows Server 2012 it returns false.

Also, checking the SSCLI for nlsinfo.cpp file reveals this comment:

// Call GetLocaleInfoEx and see if the OS knows about it.
// Note that GetLocaleInfoEx has variations:
// * Pre-Vista it fails and has to go downlevel
// * Vista succeeds, but not for neutrals
// * Win7 succeeds for all locales.
// * Mac does ???

So, how can I handle custom combinations of languages and regions (ar-IL, he-US etc) that are not recognized by Windows?

P.S I'm aware of the possibility to create and register a custom locale (using CultureAndRegionInfoBuilder) but it will take too much effort to cater for all the combinations I'm planning to support.

haim770
  • 48,394
  • 7
  • 105
  • 133

1 Answers1

1

Yes, Windows 10 is able to provide somewhat sensible information for any culture name.

Constructing culture info yourself is an option - as long as calendar information is compatible you can merge strings from neutral ("HE") culture with data for any specific one (like "en-US"). CultureAndRegionInfoBuilder can be used to safely combine information as shown in the MSDN sample:

  // Create a custom culture for ru-US.
  CultureAndRegionInfoBuilder car1 = new CultureAndRegionInfoBuilder("ru-US", 
                                         CultureAndRegionModifiers.None);
  car1.LoadDataFromCultureInfo(CultureInfo.CreateSpecificCulture("ru-RU"));
  car1.LoadDataFromRegionInfo(new RegionInfo("en-US"));

Aleternatively you can build XML files defining cultures you need to support and either load them at run-time or even install on the servers using CultureAndRegionInfoBuilder.Register - Create custom culture in ASP.NET.

Community
  • 1
  • 1
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • Thanks but I already mentioned in the question that I'm looking for an alternative approach to custom culture registration. But, do you have any reference/source for the claim that Windows 10 can return data for any culture combination? – haim770 Sep 08 '15 at 19:03
  • @haim770 no, I don't have any links (something like [GetLocaleInfoEx](https://msdn.microsoft.com/en-us/library/windows/desktop/dd318103(v=vs.85).aspx) should have info, but it does not). If combining existing cultures via `CultureAndRegionInfoBuilder` to create new one (and either just set as culture for the tread or register) does not work for you I'm not sure there is anything else that could help you in .Net framework. – Alexei Levenkov Sep 08 '15 at 19:58