Reading Messages in the View
The most common place that you need messages is inside the view. To read messages from the view just use the
message tag:
<g:message code="my.localized.content" />
As long as you have a key in your
messages.properties
(with appropriate locale suffix) such as the one below then Grails will look-up the message:
my.localized.content=Hola, Me llamo John. Hoy es domingo.
Note that sometimes you may need to pass arguments to the message. This is also possible with the
message
tag:
<g:message code="my.localized.content" args="${ ['Juan', 'lunes'] }" />
And then use positional parameters in the message:
my.localized.content=Hola, Me llamo {0}. Hoy es {1}.
Reading Messages in Controllers and Tag Libraries
Since you can invoke tags as methods from controllers it is also trivial to read messages within in a controller:
def show = {
def msg = message(code:"my.localized.content", args:['Juan', 'lunes'])
}
The same technique can be used on
tag libraries, but note if your tag library has a different
namespace then you will need to
g.
prefix:
def myTag = { attrs, body ->
def msg = g.message(code:"my.localized.content", args:['Juan', 'lunes'])
}