Best practice. What is the best way to store exceptions / errors or informational messages in C # .net for internationalization?

When throwing user exceptions or throwing messages to the end user, you can use hard-coded strings (including string constants), use assemblies only for resources, or retrieve rows from a table in the database.

I want my application to easily switch to another language without the need for recompilation. Although storing string resources in an assembly or database would achieve this, this adds to the complexity of the program logic, which in turn increases the cost of the product.

My question is: what is the best way to go with the goal in mind without ignoring the costs associated with each option? If you have a practice that is better than what has been listed, I would like to hear it.

Technologies: OS: Windows family Platform: .NET Frame 2 and higher Language: C # Database: MS SQL 2005 and higher

Thanks guys!

Cullen

+4
source share
2 answers

Use resources:

How does this complicate the program logic?

try { //do something with System.Net.Mail with invalid email.. } catch (FormatException fex) { throw new Exception(Resources.ErrorMsg.Invalid_Email, fex); } 

Edit

In VS2008, when creating a resource, you can determine whether it is internal or public. Therefore, suppose we installed it publicly, in an assembly called ClassLibrary1, we can access the property, for example:

ClassLibrary1.Properties.Resources.InvalidError

Where InvalidError is the name of the error. Again, I don't think this adds any complexity to the logic.

+4
source

.NET already supports multiple resources across multiple cultures using a naming convention:

  <default resource file name>.<culture>.resx 

Essentially, as Josh pointed out, VS2008 creates a secure, secure wrapper to access these resources.

However, the VS UI provides a minimal bear of what you can do.

If you are creating a new resource file, which is called exactly the same as the default, add culture information before resx. (NOTE: You will need to create it somewhere else and then copy it to the magic properties folder.)

Then your application will, if you apply the culture to the thread accessing the resource, pull the correct string from certain resources.

For instance:

  // Using the default culture string s = Resources.Error1Msg; Thread.CurrentThread.CurrentUICulture = new CultureInfo("es-CO"); // Using the specific culture specified as above: s = Resources.Error1Msg; 

If you need to parameterize your message, use string.Format to parameterize the output.

One caveat is to try to archive your application tiers so that your exceptions carry a rich payload (to describe the error) instead of relying solely on text.

Thus, your presentation level can provide the best user interface experience that can use the payload.

NTN

Philip

+2
source

All Articles