16.3 Reading Messages 6.0.0

(Quick Reference) # 16.3 Reading Messages Version: 6.0.0 ## 16.3 Reading Messages #### Reading Messages in the View The most common place that you need messages is inside the view. Use the message tag for this:
    <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.
Messages can also include arguments, for example:
    <g:message code="my.localized.content" args="${ ['Juan', 'lunes'] }" />
The message declaration specifies positional parameters which are dynamically specified:
    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.
    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 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.
    def show() {
        def msg = message(code: "my.localized.content", args: ['Juan', 'lunes'])
    }
The same technique can be used in tag libraries, but if your tag library uses a custom namespace then you must prefix the call with `g.`:
    def myTag = { attrs, body ->
        def msg = g.message(code: "my.localized.content", args: ['Juan', 'lunes'])
    }
| Quick Reference (hide)