Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# - Yes / No value of System

Tags:

c#

Is there a way I can get the Yes / No value of the System Language in the .Net framework?

I don´t want to make language files for each language when I only need Yes & no...

like image 744
Van Coding Avatar asked Apr 06 '11 17:04

Van Coding


3 Answers

You can indeed use the windows resources.

I once made an example (unfortunately in Delphi), but you can surely do it in Dotnet as well. It can be really useful, you are not limited to "Yes" and "No", but can use phrases like "do you want to continue...".

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

Sorry that i can't provide an example in C#.

Edit: Well, now i found the time to write a small class in 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);
}
like image 195
martinstoeckli Avatar answered Oct 25 '22 19:10

martinstoeckli


I´ve found a solution:

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;
}
like image 6
Van Coding Avatar answered Oct 25 '22 17:10

Van Coding


You could take a Universal Approach and use pictures.

enter image description hereenter image description hereenter image description here

You could make your own, ignoring the use of the words shown in two of the images above and go with the Check and X images. I use this for our factory workers who are not only mostly illiterate but sometimes non-English speaking.

like image 3
jp2code Avatar answered Oct 25 '22 17:10

jp2code