.NET Framework

.NET FrameworkMicrosoft .NET Framework has been localized into many languages worldwide. The .NET Framework supports up to 25 different languages, including the original English language. This was a nice idea for end users but often become a nightmare for developers. When some "average" developer receives some "common" error message like "Διαίρεση με το μηδέν." everything is alright, if he understands Greek that is. But what can he do if he does not understand it at all?

Why is an error message localized? Why it is not in English?

At the time of the exception the Framework exception code loads the error message from its resources,based on the current thread culture. So if the current culture is set to French (and the French .NET language is installed on the machine), the message will be localized to French. The StackTrace property is localized to the current culture as well. The following example shows how to get an exception message in French (1):

CultureInfo oldCI = Thread.CurrentThread.CurrentCulture;	Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("fr-FR");Thread.CurrentThread.CurrentUICulture = new CultureInfo("fr-FR");try{ System.IO.StreamReader sr = new System.IO.StreamReader(@"c:\does-not-exist");}catch (Exception ex){ Console.WriteLine(ex.ToString())}Thread.CurrentThread.CurrentCulture = oldCI;Thread.CurrentThread.CurrentUICulture = oldCI;

How to get the original English exception message?

Unfortunately, as described above, it is not possible to get the original message once the exception is thrown. The error message cannot be returned back to English even if the current culture is changed to English. The StackTrace property is localized at the moment of accession so if the current culture is changed to English then the StackTrace will be in English too. A very nice example on how to get the StackTrace in English follows:

try{ System.IO.StreamReader sr = new System.IO.StreamReader(@"c:\does-not-exist");}catch (Exception ex){ Console.WriteLine(ex.ToString()); // ex contains localized StackTrace
 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();}

Where the ExceptionLogger class looks something like:

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

The example above has been published by the mdb user at stackoverflow.

(1) The French language for .NET framework must be installed on the machine to get the French exception message.

Sources:
http://msdn.microsoft.com/en-us/library/aa480240.aspx
http://stackoverflow.com/questions/209133/c-sharp-exception-messages-in-english

Other links:
Browsing .NET Framework catalog