简体   繁体   中英

CultureInfo conversions (C#)

Is it possible to get a specific CultureInfo by the TwoLetterISOLanguageName? There is only a getter, not a setter... How would you do this. If possible without going through all cultures...

So not with:

// Get all available cultures on the current system.
CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.AllCultures);
foreach (var culture in cultures) {
   // Exclude custom cultures. 
   if ((culture.CultureTypes & CultureTypes.UserCustomCulture) == CultureTypes.UserCustomCulture) 
        continue;

   if (culture.TwoLetterISOLanguageName == "<Whatever>"){
        //Do some stuff
        break;
   }
}   

http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.twoletterisolanguagename.aspx

SECOND:

Is it possible to show the language as spoken in that language?
Like
* Dutch -> Nederlands
* German -> Deutsch
* Spanish -> Español

You can use constructor , eg

  CultureInfo germanCulture = new CultureInfo("DE");
  CultureInfo russianCulture = new CultureInfo("RU");


  String germanNativeLanguage = germanCulture.NativeName; // <- Deutsch
  String russianNativeLanguage = russianCulture.NativeName; // <- русский


  String germanLanguage = germanCulture.EnglishName; // <- German
  String russianLanguage = russianCulture.EnglishName; // <- Russian

Use CultureInfo(string) constructor:

var culture = new CultureInfo("<Whatever>");

It also works for cultures, that don't have two letter culture name

If ISO 639-1 does not define a two-letter language code for a particular culture, the TwoLetterISOLanguageName property returns a string that consists of three or more letters.

eg for Lower Sorbian (Germany) :

var culture = new CultureInfo("dsb");

您可以使用构造函数,但我更喜欢使用静态方法GetCultureInfo因为文档明确表示它返回一个缓存的实例:

var ci = CultureInfo.GetCultureInfo("<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