c# - 是/否系统值

时间:2011-04-06 17:17:46

标签: c#

有没有办法在.Net框架中获得系统语言的是/否值?

我不想为每种语言制作语言文件,因为我只需要是&没有...

5 个答案:

答案 0 :(得分:10)

您确实可以使用Windows资源。

我曾经做过一个例子(不幸的是在Delphi中),但你也可以在Dotnet中做到这一点。它可以非常有用,你不仅限于“是”和“否”,但可以使用“你想继续......”之类的短语。

http://www.martinstoeckli.ch/delphi/delphi.html#StringWindowsRes

很抱歉,我无法在C#中提供示例。

编辑:好吧,现在我找到了在C#写一个小班的时间:

internal static class StoWindowsString
{
  /// <summary>
  ///   Searches for a text resource in a Windows library.
  ///   Sometimes, using the existing Windows resources, you can
  ///   make your code language independent and you don't have to
  ///   care about translation problems.
  /// </summary>
  /// <example>
  ///   btnCancel.Text = StoWindowsString.Load("user32.dll", 801, "Cancel");
  ///   btnYes.Text = StoWindowsString.Load("user32.dll", 805, "Yes");
  /// </example>
  /// <param name="LibraryName">Name of the windows library like
  ///   "user32.dll" or "shell32.dll"</param>
  /// <param name="Ident">Id of the string resource.</param>
  /// <param name="DefaultText">Return this text, if the resource
  ///   string could not be found.</param>
  /// <returns>Desired string if the resource was found, otherwise
  ///   the DefaultText</returns>
  public static string Load(string libraryName, uint Ident, string DefaultText)
  {
    IntPtr libraryHandle = GetModuleHandle(libraryName);
    if (libraryHandle != IntPtr.Zero)
    {
      StringBuilder sb = new StringBuilder(1024);
      int size = LoadString(libraryHandle, Ident, sb, 1024);
      if (size > 0)
        return sb.ToString();
      else
        return DefaultText;
    }
    else
    {
      return DefaultText;
    }
  }

  [DllImport("kernel32.dll", CharSet=CharSet.Auto)]
  private static extern IntPtr GetModuleHandle(string lpModuleName);

  [DllImport("user32.dll", CharSet = CharSet.Auto)]
  private static extern int LoadString(IntPtr hInstance, uint uID, StringBuilder lpBuffer, int nBufferMax);
}

答案 1 :(得分:6)

我找到了解决方案:

class Program {
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    static extern int LoadString(IntPtr hInstance, uint uID, StringBuilder lpBuffer, int nBufferMax);
    [DllImport("kernel32")]
    static extern IntPtr LoadLibrary(string lpFileName);
    static void Main(string[] args) {
        StringBuilder sb = new StringBuilder(256);    
        IntPtr user32 = LoadLibrary(Environment.SystemDirectory + "\\User32.dll");
        LoadString(user32, 805, sb, sb.Capacity);
        YES = sb.ToString().Replace("&","");
        LoadString(user32, 806, sb, sb.Capacity);
        NO = sb.ToString().Replace("&","");
    }            
    public static string YES;
    public static string NO;
}

答案 2 :(得分:3)

您可以采用通用方法并使用图片。

enter image description here enter image description here enter image description here

您可以创建自己的,忽略使用上面两张图片中显示的单词,然后使用Check和X图像。我将这个用于我们的工厂工人,他们不仅是文盲,而且有时非英语。

答案 3 :(得分:0)

你可以这样做:

  public static class YesNo{


       public static string GetYesOrNo(this bool boolean)
       {
           var dictionary = boolean ? _yesDictionary : _noDictionary;
           if(dictionary.ContainsKey(CultureInfo.CurrentUICulture.TwoLetterISOLanguageName))
           {
               return dictionary[CultureInfo.CurrentUICulture.TwoLetterISOLanguageName];
           }
           return boolean ? DEFAULT_YES : DEFAULT_NO;
       }

      const string DEFAULT_YES = "yes";
      const string DEFAULT_NO = "no";

      private static readonly Dictionary<string, string> _yesDictionary = new Dictionary<string, string>
                                                                              {
                                                                                  {"en","yes"},
                                                                                  {"de", "ja"},
                                                                                  {"es", "si"}
                                                                              };
      private static readonly Dictionary<string, string> _noDictionary = new Dictionary<string, string>
                                                                             {
                                                                                 {"en", "no"},
                                                                                 {"de", "nein"},
                                                                                 {"es", "no"}
                                                                             };

  }

答案 4 :(得分:0)

这也可能是使用本地化资源文件查找翻译值的情况。