views:

425

answers:

2

Where is the best location to place resource strings? Does it depend on scope? Currently most of our strings are being placed at the project level, but certain strings are only needed for one form. My thoughts would be that these strings should be placed as close to their use as possible.

EDIT: This is a WinForms question. I've added a tag for clarity.

+1  A: 

Here's what I do (not necessarily the best way, but it's worked well so far) for adding programmatic strings, in addition to using language-sensitive strings directly on forms:

Aside from adding a ridiculous number of folders into the project to group by form, I haven't been able to find a clean solution other than keeping the .resx files in their own Resources folder. Usually I'll have something like:

Resources\Language
Resources\Xml
Resources\Images
etc.

I've found that using one .resx file per form is the best way to go, because you never know when you're going to need to add another string. I also create a Common.resx for strings such as "Save", "Load", etc., that are applicable to the whole application and will be used in multiple spots. With the one-.resx-per-form model, you have to be strict about only using strings in that form, which will probably mean repeating a few strings along the way as your application grows. But this is far easier to maintain than having a spiderweb of references between forms and .resx files.

For custom controls, I put the .resx files with the source files, since they usually have their own folder, and the strings won't be used outside that custom control anyway. (Note: this also means the custom controls should NOT use strings outside its own .resx file.) This will also allow you to easily extract a whole custom control into a separate assembly if necessary.

For user controls, usually they're application-specific, so it's not so bad tying the common language stuff to it, but certainly I'll use a separate .resx for strings specific to the user control, and follow the same folder structure as with the custom controls.

Jon Seigel
I think this approach is what I was starting to think. In the past I've just dumped all resources at the project level, but a common.resx for shared resources makes sense for resources that should span across multiple forms. Good thoughts!
Blake Blackwell
Edited answer for WinForms.
Jon Seigel
+2  A: 

You should strongly favor using resources at the form level (set the form's Localizable property to True in the designer). The designer has very good support for it and the WinRes.exe utility is available to localize strings off-line. No code is required.

Localizing project resource strings is possible but not well supported by the IDE. Check my answer in this thread for an approach.

Hans Passant