Question

All throughout an application wherever error messages (or other user messages) are used I typically hard-code a string. Obviosly this can be really bad (especially when you may have to come back and localize an app). What is the best approach to centralize these strings? A static class? Constants? An XML File? Or a combination (like creating a static class with constants that are used to read from an xml file).

Was it helpful?

Solution

Create the strings in a resource file. You can then localise by adding additional resource files.

Check out http://geekswithblogs.net/dotNETPlayground/archive/2007/11/09/116726.aspx

OTHER TIPS

Use string resources.

I've always defined constants wherever they make the most sense based on your language (a static class? application-wide controller? resource file?) and just call them where/whenever needed. Sure they're still "hard-coded" in a way at that point, but they're also nicely centralized, with naming conventions that make sense.

Create a Resource (.resx) file and add your strings there. VS will generate a class for you for easy access to these resources with full intellisence. You can then add localised resources in the same manner.

.net has a pretty good support for so-called ressource-files where you can store all strings for one language.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top