smeeb
smeeb

Reputation: 29477

Injecting Grails 2.4.x i18n messages with controller-provided model args

Grails 2.4.4 here. In the i18n docs they provide the following example:

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

The problem here is that 'Juan' and 'lunes' are hardcoded Strings provided at the GSP layer. But what if we wanted these to be injected - by the controller - as part of the GSP's data model? For instance:

class MyController {
    SomethingService somethingService

    def something() {
        String str1 = somethingService.doSomething() // Might return 'Juan'
        String str2 = somethingService.doSomethingElse() // Might return 'lunes'

        render(view: 'something', model: [ str1: str1, str2: str2 ])            
    }
}

// Inside the GSP:
<h1><g:message code="my.localized.content" args=??? /></h1>

Here I need to configure args to inject the localized <g:message /> with str1 and str2 from my model (again, provided upstream by the controller). Any ideas?

Upvotes: 0

Views: 80

Answers (1)

Joshua Moore
Joshua Moore

Reputation: 24776

Using model variables in the <g:message> tag as arguments is the simplest way to accomplish this.

For example:

<g:message code="my.localized.content" args="${ [str1, str2] }" />

Upvotes: 2

Related Questions