I'm working with an API that returns some information about audio streams in a file, more specifically the audio language in its three-letter ISO name (ISO 639-2) representation.
I would like to parse this information into a new CultureInfo object, but there is no constructor that takes a three-letter code. I could of course write an enormous select statement (switch for you C# people), but I figured it would be more cost-efficient to ask around for a better way first. So am I out of luck or is there a secret way to create a CultureInfo object using three letter names?
The CultureInfo class provides culture-specific information, such as the language, sublanguage, country/region, calendar, and conventions associated with a particular culture. This class also provides access to culture-specific instances of the DateTimeFormatInfo, NumberFormatInfo, CompareInfo, and TextInfo objects.
CurrentCulture = New CultureInfo("th-TH", False) Console. WriteLine("CurrentCulture is now {0}.", CultureInfo.CurrentCulture.Name) ' Display the name of the current UI culture. Console. WriteLine("CurrentUICulture is {0}.", CultureInfo.CurrentUICulture.Name) ' Change the current UI culture to ja-JP.
EDIT: sorry, I've used the wrong property:
public static CultureInfo FromISOName(string name)
{
return CultureInfo
.GetCultures(CultureTypes.NeutralCultures)
.FirstOrDefault(c => c.ThreeLetterISOLanguageName == name);
}
However, there are still duplicates in the list and no support for "dut".
I would go for Balazs solution, but it would be better in your case to use CultureTypes.NeutralCultures as you don't seem to be concerned with region/country data.
It would always return a single CultureInfo with no need of FirstOrDefault
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With