////
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
////


==== Reading Messages in the View


The most common place that you need messages is inside the view. Use the link:{gspdocs}/ref/Tags/message.html[message] tag for this:

[source,xml]
----
<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:

[source,groovy]
----
my.localized.content=Hola, me llamo John. Hoy es domingo.
----

Messages can also include arguments, for example:

[source,xml]
----
<g:message code="my.localized.content" args="${ ['Juan', 'lunes'] }" />
----

The message declaration specifies positional parameters which are dynamically specified:

[source,groovy]
----
my.localized.content=Hola, me llamo {0}. Hoy es {1}.
----

==== Reading Messages in Grails Artifacts with MessageSource

In a Grails artifact, you can inject `messageSource` and use the method `getMessage` with the arguments: message code, message arguments, default message and locale to retrieve a message.

[source,groovy]
----
import org.springframework.context.MessageSource
import org.springframework.context.i18n.LocaleContextHolder

class MyappController {

    MessageSource messageSource

    def show() {
        def msg = messageSource.getMessage('my.localized.content', ['Juan', 'lunes'] as Object[], 'Default Message', LocaleContextHolder.locale)
    }
----

==== Reading Messages in Controllers and Tag Libraries with the Message Tag

Additionally, you can read a message inside Controllers and Tag Libraries with the link:http://gsp.grails.org/latest/ref/Tags/message.html[Message Tag]. However, using the message tag relies on GSP support which a Grails application may not necessarily have; e.g. a rest application.

In a controller, you can invoke tags as methods.

[source,groovy]
----
def show() {
    def msg = message(code: "my.localized.content", args: ['Juan', 'lunes'])
}
----

The same technique can be used in link:theWebLayer.html#taglibs[tag libraries], but if your tag library uses a custom link:theWebLayer.html#namespaces[namespace] then you must prefix the call with `g.`:

[source,groovy]
----
def myTag = { attrs, body ->
    def msg = g.message(code: "my.localized.content", args: ['Juan', 'lunes'])
}
----
