Quantcast

I18n and controllers

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

I18n and controllers

Fabian Topfstedt
Hi,

when it comes to success-/errormessages and internationalization, I
think even the controller needs translating skills using a method
comparable to what the g:message-taglib does for the views.

Pragmatically, I put most of the taglib's logic into a private method
inside the controller, like


import org.springframework.web.servlet.support.RequestContextUtils as RCU
...
private def message = { code |
def messageSource =
grailsAttributes.getApplicationContext().getBean("messageSource")
                def locale = RCU.getLocale(request)
                return messageSource.getMessage(code, null, code, locale) // third
parameter is fallback
}

This works fine, but in my opinion it's boilerplate code. What about a
dynamic method to translate with? Or did I miss essential parts of the
documentation?

cheers,
Fabian

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: I18n and controllers

graemer
Fabian,

The long term plan is to make tag library methods allable from
controllers as helper methods. So this issue will go away eventually

Cheers
Graeme

On 9/27/06, Fabian Topfstedt <[hidden email]> wrote:

> Hi,
>
> when it comes to success-/errormessages and internationalization, I
> think even the controller needs translating skills using a method
> comparable to what the g:message-taglib does for the views.
>
> Pragmatically, I put most of the taglib's logic into a private method
> inside the controller, like
>
>
> import org.springframework.web.servlet.support.RequestContextUtils as RCU
> ...
> private def message = { code |
> def messageSource =
> grailsAttributes.getApplicationContext().getBean("messageSource")
>                 def locale = RCU.getLocale(request)
>                 return messageSource.getMessage(code, null, code, locale) // third
> parameter is fallback
> }
>
> This works fine, but in my opinion it's boilerplate code. What about a
> dynamic method to translate with? Or did I miss essential parts of the
> documentation?
>
> cheers,
> Fabian
>
> ---------------------------------------------------------------------
> To unsubscribe from this list please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>


--
Graeme Rocher
Grails Project Lead
http://grails.org

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: I18n and controllers

Fabian Topfstedt
Cool :)

Graeme Rocher schrieb:

> Fabian,
>
> The long term plan is to make tag library methods allable from
> controllers as helper methods. So this issue will go away eventually
>
> Cheers
> Graeme
>
> On 9/27/06, Fabian Topfstedt <[hidden email]> wrote:
>> Hi,
>>
>> when it comes to success-/errormessages and internationalization, I
>> think even the controller needs translating skills using a method
>> comparable to what the g:message-taglib does for the views.
>>
>> Pragmatically, I put most of the taglib's logic into a private method
>> inside the controller, like
>>
>>
>> import org.springframework.web.servlet.support.RequestContextUtils as RCU
>> ...
>> private def message = { code |
>> def messageSource =
>> grailsAttributes.getApplicationContext().getBean("messageSource")
>>                 def locale = RCU.getLocale(request)
>>                 return messageSource.getMessage(code, null, code,
>> locale) // third
>> parameter is fallback
>> }
>>
>> This works fine, but in my opinion it's boilerplate code. What about a
>> dynamic method to translate with? Or did I miss essential parts of the
>> documentation?
>>
>> cheers,
>> Fabian
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list please visit:
>>
>>     http://xircles.codehaus.org/manage_email
>>
>>
>
>


---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: I18n and controllers

Maurice Nicholson old
In reply to this post by Fabian Topfstedt
Do you really need to translate something in your controller?

Can you defer it to the view? So in your controller:

def login = {
    // ... auth code
    // there was a problem
    flash["message"] = "login.incorrect"
    // render or redirect
}

then in your view just:

<g:if test="${flash.message}">
  <div id="message">
    <g:message code="${flash.message}" />
  </div>
</g:if>

Does that work for you?


Fabian Topfstedt wrote:

> Hi,
>
> when it comes to success-/errormessages and internationalization, I
> think even the controller needs translating skills using a method
> comparable to what the g:message-taglib does for the views.
>
> Pragmatically, I put most of the taglib's logic into a private method
> inside the controller, like
>
>
> import org.springframework.web.servlet.support.RequestContextUtils as RCU
> ...
> private def message = { code |
> def messageSource =
> grailsAttributes.getApplicationContext().getBean("messageSource")
>         def locale = RCU.getLocale(request)
>         return messageSource.getMessage(code, null, code, locale) //
> third parameter is fallback
> }
>
> This works fine, but in my opinion it's boilerplate code. What about a
> dynamic method to translate with? Or did I miss essential parts of the
> documentation?
>
> cheers,
> Fabian
>
> ---------------------------------------------------------------------
> To unsubscribe from this list please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: I18n and controllers

Fabian Topfstedt
You're right Maurice, that's a nicer/more MVC way to do it

Thanks,
Fabian


Maurice Nicholson schrieb:

> Do you really need to translate something in your controller?
>
> Can you defer it to the view? So in your controller:
>
> def login = {
>    // ... auth code
>    // there was a problem
>    flash["message"] = "login.incorrect"
>    // render or redirect
> }
>
> then in your view just:
>
> <g:if test="${flash.message}">
>  <div id="message">
>    <g:message code="${flash.message}" />
>  </div>
> </g:if>
>
> Does that work for you?
>
>
> Fabian Topfstedt wrote:
>> Hi,
>>
>> when it comes to success-/errormessages and internationalization, I
>> think even the controller needs translating skills using a method
>> comparable to what the g:message-taglib does for the views.
>>
>> Pragmatically, I put most of the taglib's logic into a private method
>> inside the controller, like
>>
>>
>> import org.springframework.web.servlet.support.RequestContextUtils as RCU
>> ...
>> private def message = { code |
>> def messageSource =
>> grailsAttributes.getApplicationContext().getBean("messageSource")
>>         def locale = RCU.getLocale(request)
>>         return messageSource.getMessage(code, null, code, locale) //
>> third parameter is fallback
>> }
>>
>> This works fine, but in my opinion it's boilerplate code. What about a
>> dynamic method to translate with? Or did I miss essential parts of the
>> documentation?
>>
>> cheers,
>> Fabian


---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Loading...