Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle translation of exception message?

Tags:

c#

exception

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?

like image 540
JiBéDoublevé Avatar asked Aug 01 '13 12:08

JiBéDoublevé


People also ask

Should error messages be translated?

Depends on the app. A highly technical application that will be used by people that understand English and the target language, you might be OK with English error messages. If it's a message that will be seen by the end user, it should be translated.

What is exception translation?

Exception translation (or exception conversion) is the process of converting one type of exception into another. Exception translation is typically applied if exceptions from third party libraries do not fit into your application.

What do I put in an exception message?

In my experience, when it comes to writing exception messages, most developers approach the job with one of these mindsets: Write the shortest possible exception message; if possible, ignore good grammar, punctuation, and proper spelling. Write lovingly crafted error messages for the end users.

What does exception message mean?

Exception messages depend on the transaction being carried out and are meant to inform you of an important or critical event (for example, start date lies in the past, safety stock has been exceeded). By means of the exception messages, you can easily sort out any materials that you need to reprocess manually.


Video Answer


3 Answers

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);
like image 141
Steven Avatar answered Oct 19 '22 13:10

Steven


Much better to only translate it when it needs to be displayed, IMHO. This applies to any localisable string, not just error messages.

Ideally, the logic of the code shouldn't care about the content - or language - of the message, it's only interested in the type of the exception. It's only the presentation layer that (might) need to display it in the local language.

like image 30
SteveLove Avatar answered Oct 19 '22 13:10

SteveLove


The external code should not translate messages

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

This is best solution in my mind

like image 1
Viacheslav Smityukh Avatar answered Oct 19 '22 12:10

Viacheslav Smityukh