Change i18n locale from a controller before calling the view

View: New views
6 Messages — Rating Filter:   Alert me  

Change i18n locale from a controller before calling the view

by AleCaste :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello everyone,
I have a simple problem I don't know how to resolve.

I get the params.lang = 'en' from the user, but within the controller we detect where the IP comes from and depending on that, we want to change the locale from 'en' to whatever the language corresponds to the geographic location of the user.

For instance, if we want the view to be rendered in Spanish, just by simply setting lang = 'es' in the controller code does not do anything. The view is still being rendered in English 'en'.

Anyone knows how to use the u18n support in Grails to change a locale within a controller before the view is called?

Thanks a million!

Re: Change i18n locale from a controller before calling the view

by AleCaste :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello guys,

I found the answer myself.
I want to post it here in case someone else has the same need in the future.
The controller code would look like this:


import org.springframework.web.servlet.support.RequestContextUtils as RCU
class myController {
  def index = {
    'Let's change the Locale to, let's say...  'es_ES'
    def newLocale = new Locale('es', 'ES')
    RCU.getLocaleResolver(request).setLocale(request, response, newLocale)
    'DONE! Now when the view is rendered, it'll be done using the new locale
  }
}

Re: Change i18n locale from a controller before calling the view

by Moe-25 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Good job! 
But I was wondering if you'd mind sharing the code for the nation ip lookup? :D

Moe



Parent Message unknown RE: Change i18n locale from a controller before calling the view

by Finn Herpich | Marfinn Software GmbH :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

What about proxy-users? e.g. the tor-network?

Cheers

-----Original Message-----
From: AleCaste [mailto:alecaste@...]
Sent: Friday, July 03, 2009 2:49 PM
To: user@...
Subject: Re: [grails-user] Change i18n locale from a controller before calling the view


Hello guys,

I found the answer myself.
I want to post it here in case someone else has the same need in the future.
The controller code would look like this:


import org.springframework.web.servlet.support.RequestContextUtils as RCU
class myController {
  def index = {
    'Let's change the Locale to, let's say...  'es_ES'
    def newLocale = new Locale('es', 'ES')
    RCU.getLocaleResolver(request).setLocale(request, response, newLocale)
    'DONE! Now when the view is rendered, it'll be done using the new locale
  }
}
--
View this message in context: http://www.nabble.com/Change-i18n-locale-from-a-controller-before-calling-the-view-tp24321530p24322627.html
Sent from the grails - user mailing list archive at Nabble.com.


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

    http://xircles.codehaus.org/manage_email




Marfinn Software GmbH
Firmensitz: Endenicher Straße 262, 53121 Bonn
Registergericht: Amtsgericht Bonn, HRB 16233
Geschäftsführer: Martin Hensel, Finn Herpich


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

    http://xircles.codehaus.org/manage_email



Re: Change i18n locale from a controller before calling the view

by Simon Polak :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Then the ip would resolve to the country proxy server is located in.

On Sat, Jul 4, 2009 at 8:14 PM, Finn Herpich <finn.herpich@...> wrote:
What about proxy-users? e.g. the tor-network?

Cheers

-----Original Message-----
From: AleCaste [mailto:alecaste@...]
Sent: Friday, July 03, 2009 2:49 PM
To: user@...
Subject: Re: [grails-user] Change i18n locale from a controller before calling the view


Hello guys,

I found the answer myself.
I want to post it here in case someone else has the same need in the future.
The controller code would look like this:


import org.springframework.web.servlet.support.RequestContextUtils as RCU
class myController {
 def index = {
   'Let's change the Locale to, let's say...  'es_ES'
   def newLocale = new Locale('es', 'ES')
   RCU.getLocaleResolver(request).setLocale(request, response, newLocale)
   'DONE! Now when the view is rendered, it'll be done using the new locale
 }
}
--
View this message in context: http://www.nabble.com/Change-i18n-locale-from-a-controller-before-calling-the-view-tp24321530p24322627.html
Sent from the grails - user mailing list archive at Nabble.com.


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

   http://xircles.codehaus.org/manage_email




Marfinn Software GmbH
Firmensitz: Endenicher Straße 262, 53121 Bonn
Registergericht: Amtsgericht Bonn, HRB 16233
Geschäftsführer: Martin Hensel, Finn Herpich


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

   http://xircles.codehaus.org/manage_email





--
Most of what we call management consists of making it difficult for people to get their work done.

Re: Change i18n locale from a controller before calling the view

by AleCaste :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Sure,
We are using google maps in our website so we have a key to use google APIs.
Once you have yours, you add the following code in the web page:

<script type="text/javascript">
    // Detects country by IP
    google.setOnLoadCallback(function() {
      if (google.loader.ClientLocation) {
        currentCountry = google.loader.ClientLocation.address.country_code;
      } else {
        //Can't find location
      }
    });
</script>

Apart from the country code, you can get other area info too, but we only need the country.
Our page redirects to the main page of our site, adding the country code as a param.
On the server side we have a table in a database with all countries and the languages spoken in each one of them.
You get the country code, look up the table, get the language code, and set the proper locale before calling the view (as I posted in my previous response)

Alex


Moe-25 wrote:
Good job! But I was wondering if you'd mind sharing the code for the nation
ip lookup? :D

Moe