I am starting with Grails and want to have one page with multilanguage content.
I started using the tag which works fine. But here is what I want to do:
I want to include the default text of the default language right in the text, to avoid switching back and forth in between files.
<g:message code="homepage.feature.headline1" default="This is an english text" />
The above works.
But now I a have a message which should include a link like this:
<g:message code="homepage.feature.headline1" default="This is an english text with <a href='somefile.html'>a link</a>" />
This gives me an exception:
org.codehaus.groovy.grails.web.pages.exceptions.GroovyPagesEx开发者_StackOverflow社区ception: Error processing GroovyPageView: Grails tags were not closed! [[<g:message>]] in GSP
How can I achieve that I can include a link there? I have tried escaping the <> brackets but still no luck. I really would like to avoid splitting up this one sentence into multiple smaller sentences.
Thanks Christoph
You have two possible ways:
<g:message code="homepage.feature.headline1" default="This is an english text with ${'<a href=\'somefile.html\'>a link</a>'}" />
or
<% def link = "<a href='somefile.html'>a link</a>"%>
<g:message code="homepage.feature.headline1" default="This is an english text with $link" />
Closures can be used for nested i18n messages. I find it useful for when the link generation needs additional logic.
<g:message code="homepage.feature.headline1" default="This is an english text with a {0}" encodeAs="raw" args="[link(controller: 'someController', action: 'someAction') { message(code:'homepage.feature.headline1.link')}]"/>
I usually create 2 messages: the original one and the key other to be replaced. This makes it optional to create the link or not. Considering both keys are provided (leaving the verification out of this snippet):
packagesUpdate.error.server.unreachable=The packages repository server "{0}" is unreachable. This usually happens behind a network proxy server.
packagesUpdate.error.server.proxyReplace=network proxy server
Replacing the string in the controller, e.g., on ref msgWithLink:
def msg = message(code: 'packagesUpdate.error.server.unreachable') def proxyReplace = message(code:'packagesUpdate.error.server.proxyReplace') def msgWithLink = msg.replace(proxyReplace, "<a href='/csvn/packagesUpdate/available'>${proxyReplace}</a>")
Replacing in the g:message tag:
< g:set var="msg" value="${message(code:'packagesUpdate.error.server.unreachable')}" />
< g:set var="proxyReplace" value="${message(code:'packagesUpdate.error.server.proxyReplace')}" />
< g:set var="link" value="${proxyReplace}" />
< g:set var="msgWithLink" value="${msg.replace(proxyReplace, link)}" />
精彩评论