在.NET框架中,有没有一种方法可以获取系统语言的是/否值?
当我只需要是和否时,我不想为每种语言制作语言文件...
在.NET框架中,有没有一种方法可以获取系统语言的是/否值?
当我只需要是和否时,我不想为每种语言制作语言文件...
你确实可以使用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);
}
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;
}
.Substring(1)
来截取&
,但这可能会识别错误的键盘快捷方式,因为它不一定总是在第一个字符上。 (除非有一种语言中“是”和“否”以相同的字母开头,否则我认为它应该是在第一个字符上)。更安全的做法可能是使用.Replace("&","")
。 - RupGetModuleHandle
比LoadLibrary
更便宜,但我不确定您是否可以在Dotnet中使用它。无论如何,与GetModuleHandle
相比,LoadLibrary
需要随后使用FreeLibrary
。 - martinstoeckli您可以采用通用的方法,使用图片。
您可以制作自己的图片,忽略上面两幅图中显示的文字,并使用“√”和“X”图片。我为我们的工厂工人使用这个方法,因为他们大多数是文盲,有时不会说英语。
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"}
};
}