Analysis of culture information in .NET.

Hi friends enthusiasts

I work with an API that returns some information about audio streams to a file, or rather, the audio language in its three-letter name ISO ( ISO 639-2 ).

I would like to parse this information into a new CultureInfo object, but there is no constructor that accepts three-letter code. I could, of course, write a huge select clause (switch C # people for you), but I decided it would be more economical to ask the best way first. So I'm out of luck or is there a secret way to create a CultureInfo object using three letter names?

+6
source share
4 answers

EDIT: Sorry, I 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 there is no support for "dut".

+5
source

I would go for a Balazs solution, but it would be better in your case to use CultureTypes.NeutralCultures, since you do not seem to care about the data of the region / country.

It will always return a single CultureInfo file without having to use FirstOrDefault

+2
source

There is nothing to help with this analysis.

Instead of selecting, you can create a Dictionary(Of string, CultureInfo) to map to each other. It is more ease of use.

0
source

Here is the extension method for the Silverlight class System.Globalization.CultureInfo , which gets the ISO 639-2 three-letter code for the language of the current System.Globalization.CultureInfo object. It uses an ISO-639-2 table , which is retrieved using the custom Utils.GetResourceStream() method.

The implementation is also based on the custom String.NthIndexOf() method .

 public static string ThreeLetterISOLanguageName(this CultureInfo cultureInfo) { const string separator = "|"; using (var reader = new StreamReader(Utils.GetResourceStream("ISO-639-2_utf-8.txt"))) { while (!reader.EndOfStream) { string line = reader.ReadLine(); //two-letter ISO code is in the third column, ie after the second separator character string twoLetterISOCode = line.Substring(line.NthIndexOf(separator, 1) + separator.Length, 2); if (!twoLetterISOCode.Equals(cultureInfo.TwoLetterISOLanguageName)) continue; return line.Substring(0, 3); } } return null; } 

Full text

0
source

Source: https://habr.com/ru/post/911502/


All Articles