C# - 系统的是/否值

7

在.NET框架中,有没有一种方法可以获取系统语言的是/否值?

当我只需要是和否时,我不想为每种语言制作语言文件...


3
你只需要“是”和“否”吗?你会问什么需要使用“是/否”回答且只能用“是”和“否”这两个词的问题? - R. Martinho Fernandes
问题可以由用户定义。这就是问题所在 ^^ - Van Coding
2
所以,你整个应用程序中唯一需要翻译的部分就是“是” / “否”吗? - RQDQ
5个回答

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);
}

在user32.dll中,您可以找到“Yes”和“No”作为字符串资源,尽管它们会有键盘快捷方式标记:“&Yes”和“&No”。 - Rup

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;
}

1
很棒 - 你正在使用.Substring(1)来截取&,但这可能会识别错误的键盘快捷方式,因为它不一定总是在第一个字符上。 (除非有一种语言中“是”和“否”以相同的字母开头,否则我认为它应该是在第一个字符上)。更安全的做法可能是使用.Replace("&","") - Rup
2
很高兴看到你解决了你的问题。请注意,函数GetModuleHandleLoadLibrary更便宜,但我不确定您是否可以在Dotnet中使用它。无论如何,与GetModuleHandle相比,LoadLibrary需要随后使用FreeLibrary - martinstoeckli
我可以用GetModuleHandle在CSharp中编写一个小例子,看看我的第一篇帖子。 - martinstoeckli

3

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

输入图像描述 输入图像描述 输入图像描述

您可以制作自己的图片,忽略上面两幅图中显示的文字,并使用“√”和“X”图片。我为我们的工厂工人使用这个方法,因为他们大多数是文盲,有时不会说英语。


5
不幸的是,很少有文化是普遍通用的。例如,我们芬兰人学习用“X”标记正确答案,在批改试卷时实际上使用勾号来标记错误答案。绿色和红色的按钮对于某些色盲患者来说无法识别。点头和摇头在大多数情况下是普遍通用的,但我听说有一些文化中它们的意义不同。如果考虑到文盲者(可能只接触过自己的文化),来自世界任何地方的人都不能安全地做出任何假设。 - Matti Virkkunen

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"}
                                                                             };

  }

3
当OP表示他不想为每种语言使用语言文件时,你认为在代码中硬编码语言值更好吗? - awe

0
这也可以使用本地化资源文件来查找翻译后的值。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接