I am new in grails and trying to set value from messages.property (i18n) in g:set tag on gsp and then use it in java script on the same gsp page. For example:
my messages.property should look like: operation.hello=Hello '{0}'
in the gsp there should be:
<g:set var="foo" value="${operation.hello('Patty')}" scope="page"/>
and
<g:javascript>
alert( $foo )
</g:javascript>
not sure how to handle this.Can anyone help?
our basic intention is to use a parametrized-messages.property value from javascript.
Tried JAWR plug-in and that worked well but JAWR had other issues that we do not want in our case
I am new in grails and trying to set value from messages.property (i18n) in g:set tag on gsp and then use it in java script on the same gsp page. For example:
my messages.property should look like: operation.hello=Hello '{0}'
in the gsp there should be:
<g:set var="foo" value="${operation.hello('Patty')}" scope="page"/>
and
<g:javascript>
alert( $foo )
</g:javascript>
not sure how to handle this.Can anyone help?
our basic intention is to use a parametrized-messages.property value from javascript.
Tried JAWR plug-in and that worked well but JAWR had other issues that we do not want in our case
Share Improve this question edited Aug 22, 2012 at 22:44 Gregg 35.9k22 gold badges114 silver badges221 bronze badges asked Aug 22, 2012 at 22:22 VictorGramVictorGram 2,66111 gold badges56 silver badges90 bronze badges3 Answers
Reset to default 3So long as you understand that all this is pocessed on the server side, I think all you need to do is use the correct syntax:
<g:set var="foo" value="${g.message(code: 'operation.hello', args: ['Patty'])}" scope="page"/>
<g:javascript>
alert( "${foo}" );
</g:javascript>
By the time this reaches the browser, it should simply read:
<script type="text/javascript">
alert( "Hello Patty" );
</script>
If I got what you mean, basically you can't.
G tags are server side stuff. Their value args must be present while the g tags are rendered, you cannot pass a javascript (client side) value to g tag (which basically it's a server side snippet). You can put the result of a g tag into a javascript string, not the opposite.
Does this answer your question?
<script type="text/javascript">
var helloTo = "Patty";
var message = "${g.message(code: 'operation.hello', args: ["+ helloTo + "])}";
</script>