简体   繁体   中英

How to get language culture from given language name?

I take source and target languages from the user as input and I want to generate culture of these inputs as following using.Net libraries;

For example, the user chooses 'English' and 'Turkish' from two combo boxes. Then I'd like to generate 'En' from 'English' and 'Tr' from Turkish. I'd like to make this happen for all possible languages, what's the correct way to do this?

I'll use these cultures in my request to translate a text, instead of this;

string url = String.Format("https://translate.googleapis.com/translate_a/single?client=gtx&sl={0}&tl={1}&dt=t&q={2}",
       "En", "Tr", Uri.EscapeUriString(input));

I want to use this;

string url = String.Format("https://translate.googleapis.com/translate_a/single?client=gtx&sl={0}&tl={1}&dt=t&q={2}",
       sourceLanguage, targetLanguage, Uri.EscapeUriString(input));

I do not want to use a dictionary because then I'll need to put all the information manually for each language, I'd like to do it with CultureInfo class but I haven't figure it out how to do.

Use System.Globalization .

Add using statement: using System.Globalization;

CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.AllCultures);

foreach (CultureInfo ci in cultures)
{
    string output = String.Format("Name: {0} DisplayName: {1} 2-Letter Name: {2} 3-Letter Name: {3}", ci.Name, ci.DisplayName, ci.TwoLetterISOLanguageName, ci.ThreeLetterISOLanguageName);
    System.Diagnostics.Debug.WriteLine(output);
}

Note : The data you want is in property TwoLetterISOLanguageName .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM