我在http://blog.armbruster-it.de/2010/07/getting-a-list-of-all-i18n-properties-used-in-a-grails-application/上找到了这个很棒的gant脚本,谢谢Stefan!
说明:创建在groovy代码和gsp模板中使用的所有i18n属性的列表
def properties = []
new File(".").eachFileRecurse {
if (it.file) {
switch (it) {
case ~/.*\.groovy/:
def matcher = it.text =~ /code:\s*["'](.*?)["']/
matcher.each { properties << it[1] }
break
case ~/.*\.gsp/:
def matcher = it.text =~ /code=["'](.*?)["']/
matcher.each { properties << it[1] }
break
}
}
}
println properties.sort().unique().join("\n")
我试图通过以下方式扩展它。假设我们有soem消息属性,例如:
message(code: 'product.label', default: 'Product')
我们想要的脚本输出如下所示:
product.label=Product
我试图为正则表达式添加一些条件:
def matcher = it.text =~ /code=["'](.*?)["'] | default=\s*["'](.*?)["']/
并将其填充到属性中。但是由于正则表达式无法找到成对的“代码和默认”部分,因此这是行不通的。
任何想法如何更改正则表达式或整个脚本来做到这一点?
最佳答案
您的正则表达式不正确。对于以下消息方法调用:
message(code: 'product.label', default: 'Product')
它看起来应该像这样:
def properties = [:]
def txt = "message(code: 'product.label', default: 'Product')"
def matcher = txt =~ /code:\s*["'](.*?)["'].*default:\s*["'](.*?)["']/
matcher.each{ properties[it[1]] = it[2] }
assert properties == ['product.label':'Product']
关于regex - 正则表达式查找成对的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5716900/