c# -当应用程序是另一种语言时,获取英语异常消息



我正在尝试本地化我的程序,但我希望发送给开发人员的错误消息以英语出现。我还没有找到一种方法来实现这一点,因为似乎如果在抛出错误时将UI文化设置为另一种语言,则会以该语言抛出错误。由于这个程序不是我自己写的,而且它相当大,我认为创建新的try catch块来尝试在发生错误或可能发生错误时将文化设置回英语是不切实际的。目前,CurrentCulture和DefaultThreadCurrentCulture在整个应用程序中始终被设置为英语,而CurrentUICulture被设置为最终用户的语言。

作为我的工作,我目前有一个函数,已经通过错误列表迭代(使用System.Resources.ResourceSets(我假设是Windows错误,但我也需要一种方法来找到。net错误,所以我可以通过这些迭代)。该字符串替换其他语言中的匹配错误,并用英语替换它们,以便在应用程序崩溃时将大量错误消息发送给开发人员。实际上,我没有太多需要翻译的内容,因为大部分消息都是服务器堆栈跟踪并显示抛出异常的位置。最大的问题是翻译内部异常消息,有时是主异常消息,因为有时抛出的错误不会出现在ResourceSet中,有时使用格式化项,如'{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
    }

我也试过使用这里张贴的方法(英语异常消息?)但没有多少运气。我仍然得到相同的本地化错误消息。这里是代码,我知道一个错误被抛出,但它似乎不像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框架错误的列表?是否有一个类,我可以访问这些从我的程序或其他地方的列表?我试着看看像unlocalize.com和finderr.net这样的网站,但我真的不想写一些东西来爬遍所有的页面来找到每一个可能的错误。

抱歉,如果我完全错过了什么。我对c#和编程都很陌生,这个问题让我有点疯狂!

编辑:我忘了补充,我更喜欢避免像这样的解决方案,你必须不断改变当前文化:

强制异常语言

防止异常消息被翻译成用户的语言?

或者在调试时删除用户计算机上的语言包或其他东西,或者只是将错误设置为英语的解决方案(这些错误日志也将在应用程序崩溃的情况下从最终用户的计算机发送,如果他们使用另一种语言,我们需要用英语记录错误)。

而且,我知道我可以用谷歌搜索错误信息,或者使用unlocalize.com或finderr.net来翻译这些信息,但我认为如果其他开发人员必须这样做,他们可能会杀了我,哈。我想如果情况更糟,我可以查询未本地化的站点?不过看起来真的很痛苦。

这里可以找到解决问题的方法。长话短说:

try
{
  System.IO.StreamReader sr=new System.IO.StreamReader(@"c:does-not-exist");
}
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();
}

其中ExceptionLogger类看起来像:

class ExceptionLogger
{
  Exception _ex;
  public ExceptionLogger(Exception ex)
  {
    _ex = ex;
  }
  public void DoLog()
  {
    Console.WriteLine(_ex.ToString()); //Will display en-US message
  }
}

异常框架根据当前线程的区域性加载错误消息。所以你能做的就是捕获异常,在catch块中你可以创建自己的logger对象,并根据需要设置它的区域性。

    try
{
//.....Code Goes Here
}
Catch (Exception ex)
{
   var eh = new ExceptionHandler("Your-Culture");
   //....code goes here
}
Public class ExceptionHandler
{
//code goes on
    public ExceptionHandler(string culture)
{
//set your culture here
}
}

试试这个:

Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-us");

最新更新