Daniel T.
Daniel T.

Reputation: 38410

In Grails, how do I save the locale when the user changes the language?

I have a Grails app with a language dropdown selector that looks something like this:

<li><a href="?lang=tr_TR">Turkish</a></li>
<li><a href="?lang=en_US">English</a></li>
<li><a href="?lang=fr_FR">French</a></li>

When the user clicks on the link, it appends the lang parameter to the querystring and the language is changed properly. However, now I have to save the language change to the database so that we know the last selected language for the user. Does anybody know how I can accomplish this?

Upvotes: 1

Views: 647

Answers (2)

Fran Garc&#237;a
Fran Garc&#237;a

Reputation: 2050

If user can change the language in any page of your app, I would suggest you to use interceptors http://grails.org/doc/latest/ref/Controllers/beforeInterceptor.html. With this interceptors you can get if the param 'lang' has been passed to the controller and set up properly the language for the logged in user.

Upvotes: 0

Rami Enbashi
Rami Enbashi

Reputation: 3556

You can retrieve the parameter in your controller using params["lang"]

Then to save the value for future visits, I can think of different ways:

  1. If you already have a User object with records in the DB, that just add a lang variable to it. This will be remembered permanently as long as your user records are saved in the DB .

    user.lang = params["lang"]

    user.save()

  2. Use the Session scope to store a lang variable.

    session.lang = params["lang"]

  3. Use a cookie to save the value in the user's browser history. http://grails.org/plugin/cookie

    cookieService.setCookie('lang', params["lang"]) // to set cookieService.get('lang') // to retrieve

Upvotes: 1

Related Questions