Resource String Location

后端 未结 2 1453
生来不讨喜
生来不讨喜 2020-12-10 23:09

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

相关标签:
2条回答
  • 2020-12-10 23:48

    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.

    0 讨论(0)
  • 2020-12-11 00:01

    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.

    0 讨论(0)
提交回复
热议问题