如何验证格式字符串

发布于 2024-07-30 17:18:21 字数 340 浏览 6 评论 0原文

我们的资源文件中有很多包含格式 .eg 的字符串

“{0} 已移至 {1}”

这些字符串由应用程序传递给 String.Format(),有时翻译人员会弄乱“格式标记”,因此我希望找到/编写一个检查资源文件中的所有字符串是否具有有效格式的工具。

我知道每个键传递给 String.Format 的参数数量,以便也可以输入到验证中。

那么,除了检查“{”是否与“}”匹配之外,是否有一种简单的方法可以找到格式字符串中的大多数错误?

(我正在使用 .NET,此检查当然将作为构建过程的一部分完成)

We have lots of strings in our resource files that contains format .e.g

“{0} has moved to {1}”

These strings are passed to String.Format() by the applications, sometimes the translators mess up the “formatting markers” Therefore I wish to find/write a tool that checks that all strings in the resource file has a valid format.

I know for each key the numbers of args that are passed to String.Format so that can feed into the validations as well.

So apart from checking that the “{“ match the “}” is there a easy way to find most errors in the format strings?

(I am using .NET, this checking will of course be done as part of the build process)

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

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

发布评论

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

评论(3

瀟灑尐姊 2024-08-06 17:18:21

这听起来像是一个经典的单元测试场景。 您能否从源代码提交触发器中驱动自动构建/单元测试? 单元测试将简单地检查每个格式字符串是否仍然可解析。

This sounds like a classic unit-test scenario. Can you drive an automated build/unit-test off the source-code commit trigger for these ? The unit test would simply check that each format string is still parse-able.

囍笑 2024-08-06 17:18:21

看起来您验证了表达式中的字符串,因此您应该使用正则表达式
查看链接文本

It looks like you validate a string in expression,so you should use Regular Expressions
Look at link text

够钟 2024-08-06 17:18:21

我提出了一个简单的解决方案,给出了合理的结果,我知道格式语句的参数数量,但不知道参数的类型。 然而大多数参数都是字符串。

所以,

if (numberOfArguments == 3)
{
  try
  {
    String.Format(theTranslatorString, "", "", "")
  }
  catch
  {
     // tell the translator there is a problem with the string format
  }
}

当然,对于现实生活中的每个参数,这都不会用“如果”来写。

I have come up with a simple solution that gives a reasonable result, I know the number of argument to the format statement but not the type of the arguments. However most arguments are strings.

So

if (numberOfArguments == 3)
{
  try
  {
    String.Format(theTranslatorString, "", "", "")
  }
  catch
  {
     // tell the translator there is a problem with the string format
  }
}

Of course this would be written without an “if” for each number of arguments in real life.

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