我在 c# windows 应用程序中开发了一个示例软件。如何让它成为一个多语言的配套软件。
例如:其中一个消息框显示“欢迎使用示例应用程序”
我在中文操作系统上安装了该软件,但它只显示英文消息。
我正在使用“字符串表”(资源文件)来解决这个问题。
在字符串表中,我需要为每条英文和中文消息创建条目。
这是一个及时的过程。还有其他方法吗?
最佳答案
为您想要支持的每种语言创建资源文件,如下所述。
alt text http://geekswithblogs.net/images/geekswithblogs_net/dotNETPlayground/resx.gif
根据用户的语言/当前文化,从相应的语言资源文件中读取值并显示在标签或消息框中。下面是一些示例代码:
public static class Translate
{
public static string GetLanguage()
{
return HttpContext.Current.Request.UserLanguages[0];
}
public static string Message(string key)
{
ResourceManager resMan = null;
if (HttpContext.Current.Cache["resMan" + Global.GetLanguage()] == null)
{
resMan = Language.GetResourceManager(Global.GetLanguage());
if (resMan != null) HttpContext.Current.Cache["resMan" + Global.GetLanguage()] = resMan;
}
else
resMan = (ResourceManager)HttpContext.Current.Cache["resMan" + Global.GetLanguage()];
if (resMan == null) return key;
string originalKey = key;
key = Regex.Replace(key, "[ ./]", "_");
try
{
string value = resMan.GetString(key);
if (value != null) return value;
return originalKey;
}
catch (MissingManifestResourceException)
{
try
{
return HttpContext.GetGlobalResourceObject("en_au", key).ToString();
}
catch (MissingManifestResourceException mmre)
{
throw new System.IO.FileNotFoundException("Could not locate the en_au.resx resource file. This is the default language pack, and needs to exist within the Resources project.", mmre);
}
catch (NullReferenceException)
{
return originalKey;
}
}
catch (NullReferenceException)
{
return originalKey;
}
}
}
在 asn asp.net 应用程序中,您可以按如下方式使用它:
<span class="label">User:</span>
你现在将:
<span class="label"><%=Translate.Message("User") %>:</span>
关于c# - C# 中的多语言支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2941391/