Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing three-letter language names as culture info

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?

like image 571
Steven Liekens Avatar asked Mar 23 '12 14:03

Steven Liekens


People also ask

What is CultureInfo?

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.

How to add CultureInfo in c#?

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.


2 Answers

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".

like image 160
Balazs Tihanyi Avatar answered Sep 23 '22 06:09

Balazs Tihanyi


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

like image 34
user484189 Avatar answered Sep 25 '22 06:09

user484189