问题
I just deployed our application to our azure staging environment and ran into an issue with the en-EN
culture not being supported. After some digging I find out that there is no such culture, and I should use en-GB
or en-US
instead.
But, now to my question. On my local development machine I've got no problems creating a CultureInfo
with en-EN
.
CultureInfo ci = new CultureInfo("en-EN");
Console.WriteLine("culture: "+ ci.ThreeLetterISOLanguageName);
Outputs culture: eng
I also tried enumerating all cultures with CultureInfo.GetCultures(CultureTypes.AllCultures);
and there's no corresponding en-EN
culture in the result.
So, what's going on, why can I create a culture which shouldn't exist?
回答1:
If you are using Windows 10 the culture behaviour has changed: "unkown", but "maybe existing" cultures (3-letter iso) will only result in an "Unkown locale" culture with LCID 4096. Pre-Windows 10 this would result in an exception (unless you have installed such a custom culture)
MSDN-Link
try
{
// ok
CultureInfo culture1 = new CultureInfo("foo");
CultureInfo culture2 = new CultureInfo("xyz");
CultureInfo culture3 = new CultureInfo("en-xy");
// not ok - exception
CultureInfo culture4 = new CultureInfo("foox");
}
catch (Exception exc)
{
}
回答2:
As the documentation states:
Starting with apps that run under the .NET Framework 4 or later on Windows 7 or later, the method attempts to retrieve a CultureInfo object whose identifier is name from the operating system; if the operating system does not support that culture, and if name is not the name of a supplementary or replacement culture, the method throws a CultureNotFoundException exception.
So basically, your OS has en-EN
as an alternate name of an existing culture (more likely en-US
) while it's not there on the Azure VM.
The solution is: use a real culture name (which you can find here)
来源:https://stackoverflow.com/questions/34138398/can-create-culture-for-en-en-on-local-dev-machine