如何处理异常消息的翻译? [英] How to handle translation of exception message?

查看:87
本文介绍了如何处理异常消息的翻译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的不知道在多语言应用程序中处理异常的最佳方法。



在哪里处理错误消息( Exception.Message )的翻译?



我抛出异常后,是否应立即在ctor中翻译消息?

 抛出新的MyException( Error message,Resource.MyException_TranslatedMessage); 

还是我抛出异常,我使用了一个自制的帮助程序,该帮助程序会使用视图逻辑中的异常类型?

 尝试
{
// ...
}
catch(例外ex)
{
myLabel.Text = new ExceptionTranslator(ex).Translate();
}

或者Microsoft是否提供工具或机制来做到这一点? p>

总而言之:处理异常消息翻译的良好实践是什么?

解决方案

大多数例外都是出于技术目的,除非您的操作和维护人员也位于不同的国家/地区,否则这些例外应该仅以编写和维护该应用程序的人员的语言显示。

.NET框架包含本地化的异常消息。对于我作为开发人员而言,这很烦人,因为以我的本地语言(荷兰语)发送的异常消息的含义不如原始的英语异常消息大。但是,对于Microsoft来说,将这些框架异常消息本地化似乎是合理的,因为它们的目标受众可以位于任何地方。



但是,某些类型的异常被明确抛出以显示出来。给用户。这些是您的 ValidationException BusinessLayerException 。他们的明确合同将显示给用户。当然,您应该对这些异常消息进行本地化,但是与翻译它们相比,抛出异常时从本地化资源中提取异常消息通常会更好,更容易:

 抛出新的ValidationException(Resources.InvalidUserName); 


I don't really know the best way to handle exception in an multi-language application.

Where should I handle the translation of the error message (Exception.Message)?

Shall I translate the message in the ctor as soon as I throw the exception?

throw new MyException("Error message", Resource.MyException_TranslatedMessage);

Or do I throw the exception and I use a home made helper that will find the error message using the type of the exception in the logic of the View?

try
{
    //...
}
catch(Exception ex)
{
    myLabel.Text = new ExceptionTranslator(ex).Translate();
}

Or, does Microsoft offer a tool or a mechanism to do that?

In a word: what are the good practices to handle exception messages translation?

解决方案

Most exceptions are there for technical purposes, unless your operations and maintenance crew are also located in different countries, those exceptions should simply have messages in the language of the people who write and maintain the application.

The .NET framework contains localized exception messages. For me as a developer, that's very annoying, since the exception messages in my local language (Dutch) don't mean as much as the original English exception messages. However, it seems reasonable for Microsoft to localize those framework exception messages, since their target audience can be located anywhere.

Some types of exceptions however, are explicitly thrown to be displayed to the user. These are your ValidationException or BusinessLayerException. Their clear contract is to be displayed to the user. Of course you should localize those exception messages, but instead of translating them, it's often much better and easier to pull the exception message from a localized resource when throwing the exception:

throw new ValidationException(Resources.InvalidUserName);

这篇关于如何处理异常消息的翻译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆