C# exceptions language

LWlodar 1 Reputation point
2022-03-03T14:11:52.637+00:00

Are the System.Exception objects available only in english?
Doing a project where user can choose the language, the point is to show each (also System's) Exceptions in the correct one. If there is an option to change the language, how to do that?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,389 questions
{count} votes

2 answers

Sort by: Most helpful
  1. satya karki 986 Reputation points MVP
    2022-03-06T15:55:26.183+00:00

    Hi @LWlodar

    Anyway, if you want to capture error into another language then you create an extension class.

    Example:

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

    Implementation example: You can do this as a separate thread because this ensures there won't be any side effects.

    try  
    {  
     //code  
    }  
    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("fr-FR"); // as per your language  
      t.Start();  
    }  
    

    https://stackoverflow.com/questions/209133/exception-messages-in-english

    1 person found this answer helpful.
    0 comments No comments

  2. Michael Taylor 49,251 Reputation points
    2022-03-03T15:50:02.633+00:00

    The words you're using have different meanings in different contexts so I'm a little unclear what you are trying to ask.

    An Exception is used to report an error. It is a programmer to programmer construct. It is not designed for users to see (although many people do anyway). Therefore the locale (e.g. English) doesn't really matter as far as error reporting/handling goes.

    Having said that ultimately an exception just takes a string. If you need your exception messages (which you really shouldn't be showing to the user) to be localized to the user's system then you need to bring in a resource manager such as ResourceManager. You then use the RM to retrieve the exception message to be used by the exception rather than using a string literal.

       //Using a string literal  
       throw new Exception("Something went wrong");  
         
       //Using a localized string  
       throw new Exception(Resources.Error_SomethingWentWrong);  
    

    The assumption here is that you added all your exception messages to your application's resources (via the Resources tab of the project). The Resources auto-generated class uses the RM to get the string localized for the user. If you don't want to use the auto-generated stuff then you have to create your own RM instance and pass it around. How you would do that is documented in the link I gave earlier.

    Of course for any of this to work you must also create separate resource assemblies for each language you want to localize your application to. If the system doesn't find a localized version of the value it is looking for then it will return the original string (English most likely).

    As for your question "does a project where user can choose the language" it doesn't make sense to me. The only user who works with a project would be a developer and therefore the language they choose is the programing language. Error messages are not specific to programming languages so that wouldn't matter.

    In terms of how you change the language I assume you mean for the program's UI. That is determined by CultureInfo.CurrentUICulture and is set to the locale that Windows itself runs under. If the application is running on a Spanish Windows system then the UI culture is set to the Spanish-equivalent culture. Your app can change that by setting the earlier mentioned property. But note that just because you change the UI culture of your program doesn't mean suddenly it'll start using that new language. Windows itself doesn't install all UI cultures so changing from English to German won't have any noticeable impact unless the user has also installed the German language pack on Windows. You would also need to have provided your own German resource assembly so your application can use the localized resources. .NET Framework is also localized but the language packs are not installed for other languages. If you need to support multiple languages on the same machine you need to install the language pack for .NET as well otherwise .NET messages will be in English. The current language packs are here.

    Lastly note that there is also a CurrentCulture value that is independent of the UI. The UI culture is used for UI stuff like dialog text, formatting of date/times, etc. The culture is for everything else. Generally you need to set both to the same value otherwise you might see odd behavior.

    0 comments No comments