# \[Rails3\] Issue in modifying locale and redisplaying page

**URL:** <https://www.ruby-forum.com/t/rails3-issue-in-modifying-locale-and-redisplaying-page/191285>\
**Category:** Rails\
**Created:** [July 8, 2010, 8:40pm UTC](https://www.ruby-forum.com/t/rails3-issue-in-modifying-locale-and-redisplaying-page/191285 "2010-07-08T20:40:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [July 8, 2010, 8:40pm UTC](https://www.ruby-forum.com/t/rails3-issue-in-modifying-locale-and-redisplaying-page/191285/1 "2010-07-08T20:40:10Z")

</div>

In a test app, I have a drop-down selection to change the language of  
the site.  
I use an Ajax request to an action in which the locale is changed, and  
the it’s redirected to the home page  
unfortunately, the locale seems to be changed but the home page  
doesn’t reflect the new language …

application\_controller.rb  
… before\_filter :set\_locale  
def set\_locale  
I18n.locale = params[:locale]  
end

def default\_url\_options(options={})  
{:locale =\> I18n.locale}  
end

jquery Ajax request in application.js

jQuery(document).ready(function() {  
// handle change events from language selector  
$(“select[id^=‘language\_’]”).change(function(event) {  
var selected = $("#" + event.target.id + " option:selected");  
var value = selected.val();  
$.ajax({  
data: ‘locale=’+ value,  
type: ‘post’,  
url: ‘/locale’  
});  
});  
});

routes.rb  
match ‘/locale’ =\> “welcome#switch\_language”

welcome\_controller.rb  
…  
def switch\_language  
I18n.locale = params[:locale].to\_sym  
redirect\_to root\_url  
end

what could be wrong ?

thanks fyh

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [July 9, 2010, 5:11pm UTC](https://www.ruby-forum.com/t/rails3-issue-in-modifying-locale-and-redisplaying-page/191285/2 "2010-07-09T17:11:19Z")

</div>

On 8 July 2010 19:39, Erwin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> In a test app, I have a drop-down selection to change the language of  
> the site.  
> I use an Ajax request to an action in which the locale is changed, and  
> the it’s redirected to the home page  
> unfortunately, the locale seems to be changed but the home page  
> doesn’t reflect the new language …

That doesn’t surprise me…

> application\_controller.rb  
> … before\_filter :set\_locale  
> def set\_locale  
> I18n.locale = params[:locale]  
> end

This filter will execute on your Ajax action (as you have a  
params[:locale])  
but on your redirect it’s a fresh request without that parameter so it  
will  
fallback to default.

I’d imagine you want to store params[:locale] in the session[] or  
cookie[]  
arrays and then use that value if set.

Also, remember the browser will normally send a preferred language in  
the  
headers (Accept-language) so you can use that by default. I can’t  
remember  
if I18n.locale uses that (or just the system locale).

Finally, your default\_url\_options is only used when generating urls for  
links using url\_for and related functions - it doesn’t set it as a  
default  
parameter on incoming request.

Cheers,

Andy
