如何验证格式字符串
我们的资源文件中有很多包含格式 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这听起来像是一个经典的单元测试场景。 您能否从源代码提交触发器中驱动自动构建/单元测试? 单元测试将简单地检查每个格式字符串是否仍然可解析。
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.
看起来您验证了表达式中的字符串,因此您应该使用正则表达式
查看链接文本
It looks like you validate a string in expression,so you should use Regular Expressions
Look at link text
我提出了一个简单的解决方案,给出了合理的结果,我知道格式语句的参数数量,但不知道参数的类型。 然而大多数参数都是字符串。
所以,
当然,对于现实生活中的每个参数,这都不会用“如果”来写。
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
Of course this would be written without an “if” for each number of arguments in real life.