# Timing of language setting

**URL:** <https://www.ruby-forum.com/t/timing-of-language-setting/223772>\
**Category:** Rails I18n\
**Created:** [October 17, 2012, 11:25am UTC](https://www.ruby-forum.com/t/timing-of-language-setting/223772 "2012-10-17T11:25:34Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [October 17, 2012, 11:25am UTC](https://www.ruby-forum.com/t/timing-of-language-setting/223772/1 "2012-10-17T11:25:34Z")

</div>

Rails 3.1.3

Hi. My application’s i18n seems to work, but the timing of the language  
setting is wrong.

I have an User model, which has a column, ‘language’, so that users can  
choose their default language. When each user logged in, the  
applications should always apply itself the language of his/her choice.

Say, an user wishes to change the default language from Japanese to  
French. The user inputs the new language of his/her choice in the  
setting page and hits ‘save’. The app jumps to to the top page having a  
flash message appeared, ‘Your setting is updated’ but in JAPANESE.  
Since the user has just changed the language, the flash is also to be  
shown in French.

Translation yml files are properly set. The application\_controller.rb is  
the following.

Could anyone give me an advice how to set the language setting according  
to user login session?

require ‘geoip’  
private  
def set\_locale  
if user\_signed\_in?  
extracted\_locale = current\_user.language  
else  
extracted\_locale = params[:locale] ||  
extract\_locale\_from\_subdomain ||  
extract\_locale\_from\_tld ||  
extract\_locale\_from\_accept\_language ||  
extract\_locale\_from\_ip

```
end
print current_user
I18n.locale = (I18n::available_locales.include?

```

extracted\_locale.to\_sym) ?  
extracted\_locale : I18n.default\_locale  
end

def extract\_locale\_from\_subdomain  
request.subdomains.first  
end

def extract\_locale\_from\_tld  
request.domain.split(’.’).last  
end

def extract\_locale\_from\_accept\_language  
request.env[‘HTTP\_ACCEPT\_LANGUAGE’].scan(/^[a-z]{2}/).first  
end

def extract\_locale\_from\_ip  
geoip ||= GeoIP.new(Rails.root.join(“lib/GeoIP.dat”))  
country\_location = @geoip.country(request.remote\_ip)  
country\_location.country\_code2.downcase  
end

soichi

---

<div class="post-metadata">

**Author:** ![soujiro0725](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/57b2e6/32.png) [@soujiro0725](https://www.ruby-forum.com/u/soujiro0725)\
**Post date:** [October 17, 2012, 11:58am UTC](https://www.ruby-forum.com/t/timing-of-language-setting/223772/2 "2012-10-17T11:58:04Z")

</div>

On 17 Oct 2012, at 10:25, Soichi I. wrote:

> French. The user inputs the new language of his/her choice in the  
> setting page and hits ‘save’. The app jumps to to the top page having a  
> flash message appeared, ‘Your setting is updated’ but in JAPANESE.  
> Since the user has just changed the language, the flash is also to be  
> shown in French.

I’m pretty sure this is because the flash message is looked up _before_  
you change the domain and stored in the session. You can get the correct  
locale by doing something like:

I18n.t(‘my.flash.message’, :locale =\> user.language)

In the controller before you redirect.

HTH,

Chris

* * *

Chris McGrath  
[http://www.localeapp.com](http://www.localeapp.com)
