C# 我们应该在资源中存储格式字符串吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/963270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:20:48  来源:igfitidea点击:

Should we store format strings in resources?

c#localizationresourcesstring-formatting

提问by jpoh

For the project that I'm currently on, I have to deliver specially formatted strings to a 3rd party service for processing. And so I'm building up the strings like so:

对于我目前正在进行的项目,我必须将特殊格式的字符串交付给 3rd 方服务进行处理。所以我正在像这样构建字符串:

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

Rather then hardcode the string, I was thinking of moving it into the project resources:

而不是对字符串进行硬编码,我正在考虑将其移动到项目资源中:

string someString = string.Format(Properties.Resources.SomeString, token1, token2, token3, number); 

The second option is in my opinion, not as readable as the first one i.e. the person reading the code would have to pull up the string resources to work out what the final result should look like.

在我看来,第二个选项不如第一个选项可读,即阅读代码的人必须调出字符串资源才能计算出最终结果应该是什么样子。

How do I get around this? Is the hardcoded format string a necessary evil in this case?

我该如何解决这个问题?在这种情况下,硬编码格式字符串是必要的邪恶吗?

采纳答案by Michael Petrotta

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.

..至少在代码足够稳定之前,我可能会因为其他人看到而感到尴尬。

回答by Matthew Flaschen

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.

我同意将它保留在资源中是不必要的间接引用。一个可能的例外是如果您的程序需要本地化,并且您正在通过资源文件进行本地化。

回答by Nick Haddad

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.

  1. 尽可能在要本地化的资源字符串集中包含格式字符串。这将允许翻译人员重新排列格式化项目的位置,使它们更好地适应翻译文本的上下文。

  2. 避免在格式标记中使用您的语言中的字符串。最好将这些用于数字。例如,消息:

    “您指定的值必须介于 {0} 和 {1} 之间”

    如果 {0} 和 {1} 是 5 和 10 之类的数字,那就太好了。如果您要格式化为“五”和“十”之类的字符串,这将使本地化变得困难。

  3. 你可以通过简单地命名你的资源来解决你正在谈论的可读性问题。

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

  4. 评估您是否在代码中的正确抽象级别生成用户可见的字符串。通常,我倾向于将代码中所有用户可见的字符串尽可能靠近用户界面进行分组。如果某些低级文件 I/O 代码需要提供错误,则应该使用您在应用程序中处理的异常和一致的错误消息来执行此操作。这也将整合所有需要本地化的字符串,而不是让它们遍布整个代码。

回答by 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/

您可以帮助添加硬编码字符串甚至加快向资源文件添加字符串的一件事是使用 CodeRush Xpress,您可以在此处免费下载: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.

编写字符串后,您可以访问 CodeRush 菜单并一步提取到资源文件。非常好。

Resharper has similar functionality.

Resharper 具有类似的功能

回答by Basheer AL-MOMANI

yes you can enter image description here

是的你可以 在此处输入图片说明

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

顺便说一下,我正在使用ResXManager