Pregunta

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

¿Fue útil?

Solución

You can use constructor, e.g.

  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

Otros consejos

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.

e.g. for Lower Sorbian (Germany):

var culture = new CultureInfo("dsb");

You could use the constructor, but I would prefer using the static method GetCultureInfo because the docs explicitly say it returns a cached instance:

var ci = CultureInfo.GetCultureInfo("<TwoLetterISOLanguageName>");
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top