if else或switch的当前CultureName?

时间:2015-08-27 15:51:30

标签: c# if-statement localization cultureinfo currentculture

我需要if或switch语句当前名称。我找不到解决方案。

此代码不起作用。

        if (Thread.CurrentThread.CurrentCulture == new CultureInfo("en"))
        {
            // EN (default)
        }

        if (Thread.CurrentThread.CurrentCulture == new CultureInfo("de"))
        {
            // DE
        }

1 个答案:

答案 0 :(得分:4)

取决于您的意思" CultureName "

  // "ru-RU" on my workstation: Russian language (Russia dialect/subculture)
  String name = Thread.CurrentThread.CurrentCulture.Name;
  // "ru" - just Russian
  String isoName = Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName;

所以在你的情况下

  // German, Germany (and not Austria)
  if (Thread.CurrentThread.CurrentCulture.Name.Equals("de-DE")) {
    ...
  }
  else if (Thread.CurrentThread.CurrentCulture.Name.Equals("de-AT")) {
    // German, but Austrian subculture
    ...
  }

  // German, any subculture, dialect ect.
  if (Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName.Equals("de")) {
    ...
  }