简体   繁体   中英

Grails Difference between <g:message code=“”/> and ${message(code)}

In Grails there are two (main) ways to insert an internationalized string into a gsp:

<g:message code="your.code.here" />

and

 ${message(code:'your.code.here')}

I thought that I saw at one point that the first one above is recommended when in the middle of HTML code

<h1><g:message code="title.header.text" /></h1>

and the second one above is recommended when in JavaScript code (with quotes around it) like

var messageToDisplay = "${message(code:'your.code.here')}";

However, I have also seen people do

<h1>${message(code:'title.header.text')}</h1>

Is this last line below also acceptable/recommended in Grails?
Is there a reason to do/not do the last option above?

(Edit added below)

One other question about encoding: In the middle of HTML it is (sometimes) necessary to do

<h1><g:message code="title.header.text" encodeAs="HTML"/></h1>

And in the middle of JavaScript

var messageToDisplay = "${message(code:'your.code.here').encodeAsJavaScript()}";

Then is there a difference? Can the default encoding be changed to HTML for a Tag and to JavaScript for the ${message(code)}?

They are all perfectly valid. It all depends on context and style. Most people prefer to use the tags when possible. They all, ultimately, use the same tag library/code.

The message(code:) format is particularly useful when you need to resolve messages within a controller, other tag library, or service.

标签和scriptlet在下面是同一回事,但我更喜欢在网页中尽可能使用标签,因为它们更适合HTML标记的其余部分,并且更易于阅读。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM