Should we store format strings in resources?

无人久伴 提交于 2019-12-03 04:34:19

I do think this is a necessary evil, one I've used frequently. Something smelly that I do, is:

// "{0}{1}{2}: Some message. Some percentage: {3}%"
string someString = string.Format(Properties.Resources.SomeString
                                  ,token1, token2, token3, number);

..at least until the code is stable enough that I might be embarrassed having that seen by others.

There are several reasons that you would want to do this, but the only great reason is if you are going to localize your application into another language.

If you are using resource strings there are a couple of things to keep in mind.

  1. Include format strings whenever possible in the set of resource strings you want localized. This will allow the translator to reorder the position of the formatted items to make them fit better in the context of the translated text.

  2. Avoid having strings in your format tokens that are in your language. It is better to use these for numbers. For instance, the message:

    "The value you specified must be between {0} and {1}"

    is great if {0} and {1} are numbers like 5 and 10. If you are formatting in strings like "five" and "ten" this is going to make localization difficult.

  3. You can get arround the readability problem you are talking about by simply naming your resources well.

    string someString = string.Format(Properties.Resources.IntegerRangeError, minValue, maxValue );

  4. Evaluate if you are generating user visible strings at the right abstraction level in your code. In general I tend to group all the user visible strings in the code closest to the user interface as possible. If some low level file I/O code needs to provide errors, it should be doing this with exceptions which you handle in you application and consistent error messages for. This will also consolidate all of your strings that require localization instead of having them peppered throughout your code.

Coding Monkey

One thing you can do to help add hard coded strings or even speed up adding strings to a resource file is to use CodeRush Xpress which you can download for free here: http://www.devexpress.com/Products/Visual_Studio_Add-in/CodeRushX/

Once you write your string you can access the CodeRush menu and extract to a resource file in a single step. Very nice.

Resharper has similar functionality.

I don't see why including the format string in the program is a bad thing. Unlike traditional undocumented magic numbers, it is quite obvious what it does at first glance. Of course, if you are using the format string in multiple places it should definitely be stored in an appropriate read-only variable to avoid redundancy.

I agree that keeping it in the resources is unnecessary indirection here. A possible exception would be if your program needs to be localized, and you are localizing through resource files.

yes you can

new lets see how

String.Format(Resource_en.PhoneNumberForEmployeeAlreadyExist,letterForm.EmployeeName[i])

this will gave me dynamic message every time

by the way I'm useing ResXManager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!