我正在尝试本地化我的程序,但我希望发送给开发人员的错误消息以英语显示。我一直无法找到一种方法来实现这一点,因为似乎如果在引发错误时将 UI 区域性设置为另一种语言,则会以该语言引发错误。由于这个程序不是我自己编写的,而且它很大,所以我认为在发生错误或可能发生错误时创建新的 try catch 块来尝试将区域性设置回英语是不切实际的。目前,CurrentCulture 和 DefaultThreadCurrentCulture 在整个应用程序中始终设置为英语,而 CurrentUICulture 设置为最终用户的语言。
在我的解决方法中,我目前有一个一直在迭代错误列表的函数(使用 System.Resources.ResourceSets (我假设是 Windows 错误,但我还需要一种方法来查找 .NET 错误,以便我可以迭代这些)。它字符串替换了其他语言中的匹配错误,并将它们替换为相应的英语等效项,以构建大量错误消息,以便在应用程序崩溃时发送给开发人员。实际上,我没有太多要翻译的内容,因为大多数消息是服务器堆栈跟踪并显示抛出异常的位置。最大的问题是转换内部异常消息,有时是主要异常消息,因为有时抛出的错误不会出现在资源集中,有时会使用“{0”等格式项}'。
这是我迄今为止翻译剩余错误的代码。我仍在努力添加正则表达式模式来翻译使用“{0}”等内容的错误,因此如果有人对此有建议或更好的方法来做到这一点,我将不胜感激。
public static string TranslateExceptionMessageTest(string exmsg, Exception e, CultureInfo currentui)
{
CultureInfo test = Thread.CurrentThread.CurrentUICulture;
string matchingstr = "";
string newstr = exmsg;
string resourcecollection = "";
Assembly a = e.GetType().Assembly;
ResourceManager rm = new ResourceManager(a.GetName().Name, a);
ResourceSet rsOriginal = rm.GetResourceSet(currentui, true, true);
ResourceSet rsTranslated = rm.GetResourceSet(new CultureInfo("en-US"), true, true);
foreach (DictionaryEntry item in rsOriginal)
{
if (exmsg.Contains(item.Value.ToString()))
{
if (item.Key.ToString() == "Word_At") // sometimes with spanish errors this replaces words containing the letters 'en' with 'at'.
{
string newat = " " + item.Value.ToString() + " "; // this is the formatting the word 'at' appears with, in any culture I've tested.
matchingstr = " " + rsTranslated.GetString(item.Key.ToString(), false) + " ";
newstr = newstr.Replace(newat, matchingstr);
}
else
{
matchingstr = rsTranslated.GetString(item.Key.ToString(), false);
newstr = newstr.Replace(item.Value.ToString(), matchingstr);
}
}
resourcecollection = resourcecollection + Environment.NewLine + "Key: " + item.Key.ToString() + Environment.NewLine + "Value: " + item.Value.ToString();
}
return newstr; // success
}
我也尝试过使用这里发布的方法(异常消息是英文的吗? https://stackoverflow.com/questions/209133/c-sharp-exception-messages-in-english)但运气不佳。我仍然得到相同的本地化错误消息。这是我知道抛出错误的代码,但 catch 块似乎没有做任何事情来改变语言。我的 ExceptionLogger 类的设置方式与 Stackoverflow 示例中的方式相同,只是添加了一行将错误写入文件。该错误是在 wcf.Client.Ping() 处引发的(我不关心实际的错误。我用它来测试)。
private void PreloadWcfDlls(object state)
{
if (Global.Inst.ServerMode == ApplicationServerMode.Unknown)
return;
try
{
using (var wcf = ClientGrabber.GetSchemaClient(Global.Inst.ServerMode))
{
wcf.Client.Ping();
}
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString()); //Will display localized message
ExceptionLogger el = new ExceptionLogger(ex);
System.Threading.Thread t = new System.Threading.Thread(el.DoLog);
t.CurrentUICulture = new System.Globalization.CultureInfo("en-US");
t.Start();
}
}
如果没有别的,有没有办法获得英语和其他语言的所有可能的 .NET Framework 错误的列表?我可以访问一个类来从我的程序或其他地方的列表中获取这些内容吗?我尝试过查看 unlocalize.com 和 finderr.net 等网站,但我真的不想编写一些内容来爬行所有页面以查找每个可能的错误。
抱歉,如果我完全遗漏了一些东西。我对 C# 和一般编程还很陌生,这个问题让我有点抓狂!
编辑:我忘了补充一点,我宁愿避免这样的解决方案,您必须不断更改 CurrentUICulture:
强制例外语言为英语 https://stackoverflow.com/questions/2092298/force-exceptions-language-in-english
防止异常消息被翻译成用户的语言? https://stackoverflow.com/questions/197127/prevent-exception-messages-from-being-translated-into-the-users-language
或者是删除语言包或用户计算机上的某些内容的解决方案,或者只是在调试时将错误设置为英语(如果应用程序崩溃并且他们正在使用另一个应用程序,这些错误日志也将从最终用户计算机发送)我们需要用英语记录错误)。
另外,我知道我可以用 google 搜索错误消息,或者使用 unlocalize.com 或 finderr.net 来翻译消息,但我认为其他开发人员如果必须这样做的话可能会杀了我,哈哈。我想如果情况更糟的话我可以查询 unlocalize 网站吗?但看起来确实很痛苦。