问题描述
以为这会更容易....想象一下< g:select />
就像这样:
< g:select name =type.idfrom =$ {Type.list()}
value =$ {domainInstance?.type? .id}/>
有两个这样的域类(请原谅我,如果这些人造类没有错误) p>
class域名{
类型类型
}
类别类型{
字符串名称
}
现在我想翻译select元素的条目。以下代码首先看起来不错:
< g:select name =type.idfrom =$ {Type。 list()}
valueMessagePrefix =type.name
value =$ {domainInstance?.type?.id}/>
在messagebundle中的条目是这样的:
type.name.type1 = red
type.name.type2 = green
问题:不仅文本被翻译,而且还有选项键!
所以我试着添加一个 optionKey ='id'
:
< g:select name =type.id from =$ {Type.list()}
valueMessagePrefix =type.name
value =$ {domainInstance?.type?.id}
optionKey ='id' />
这会将按键切换到ID - 很好,但文字切换到ID也是: - (
任何想法如何解决这个问题? grails beeing开源,我只是检查了代码:
似乎只要使用optionKey或optionValue,valueMessagePrefix就会被忽略。 optionValue可以关闭:
< g:select name =type.idfrom =$ {Type.list ()}
value =$ {domainInstance?.type?.id}
optionKey =id
optionValue =$ {{name-> g.message(code :'type.name'+ name)}}/>
thought this would be easier.... imagine a <g:select />
like this:
<g:select name="type.id" from="${Type.list()}"
value="${domainInstance?.type?.id}" />
with two domain classes like this (please forgive me if these artificial classes are not error free)
class Domain {
Type type
}
class Type {
String name
}
I would now like to translate the entries of the select element. The following code first looked good:
<g:select name="type.id" from="${Type.list()}"
valueMessagePrefix="type.name"
value="${domainInstance?.type?.id}" />
with entries in the messagebundle like this:
type.name.type1 = red
type.name.type2 = green
Problem: not only the text was translated, but the option keys, too!
So I tried to add a optionKey='id'
:
<g:select name="type.id" from="${Type.list()}"
valueMessagePrefix="type.name"
value="${domainInstance?.type?.id}"
optionKey='id' />
This switched the keys to the id - great, but the text switched to the id, too :-(
Any idea how to solve this?
thanx to grails beeing open source, I just checked the code: http://grails.org/doc/latest/ref/Tags/select.html#select
It seems that valueMessagePrefix is ignored as soon as you use optionKey or optionValue. But optionValue can take a closure:
<g:select name="type.id" from="${Type.list()}"
value="${domainInstance?.type?.id}"
optionKey="id"
optionValue="${ {name->g.message(code:'type.name'+name) } }"/>
at least, this works.
这篇关于翻译Grails中的HTML select元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!