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

发布于 2024-07-23 07:23:48 字数 481 浏览 8 评论 0原文

对于我当前正在进行的项目,我必须将特殊格式的字符串传递给第三方服务进行处理。 因此,我正在像这样构建字符串:

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

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

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

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

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

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:

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

泪意 2024-07-30 07:23:48

我确实认为这是一种必要的罪恶,我经常使用它。 我所做的一些臭事情是:

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

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

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.

背叛残局 2024-07-30 07:23:48

您想要这样做的原因有很多,但唯一重要的原因是您要将应用程序本地化为另一种语言。

如果您使用资源字符串,则需要记住以下几点。

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

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

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

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

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

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

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

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.

北座城市 2024-07-30 07:23:48

为了帮助添加硬编码字符串,甚至加快向资源文件添加字符串的速度,您可以做的一件事是使用 CodeRush Xpress,您可以在此处免费下载:http://www.devexpress.com/Products/Visual_Studio_Add-in/CodeRushX/

编写字符串后,您可以访问 CodeRush 菜单并解压一步即可生成资源文件。 很不错。

Resharper 具有类似功能

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.

意中人 2024-07-30 07:23:48

我不明白为什么在程序中包含格式字符串是一件坏事。 与传统的未记录的幻数不同,它的作用乍一看很明显。 当然,如果您在多个位置使用格式字符串,那么它绝对应该存储在适当的只读变量中以避免冗余。

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

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.

时光无声 2024-07-30 07:23:48

是的你可以
输入图片这里的描述

新让我们看看

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

每次

我使用 ResXManager

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文