haim770
haim770

Reputation: 49135

Using custom combination of language and region for CultureInfo

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.

Upvotes: 2

Views: 1307

Answers (1)

Alexei Levenkov
Alexei Levenkov

Reputation: 100575

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.

Upvotes: 1

Related Questions