Timing of language setting

27 views
Skip to first unread message

Soichi Ishida

unread,
Oct 17, 2012, 5:25:34 AM10/17/12
to rails...@googlegroups.com
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

--
Posted via http://www.ruby-forum.com/.

Christopher McGrath

unread,
Oct 17, 2012, 5:56:57 AM10/17/12
to rails...@googlegroups.com

On 17 Oct 2012, at 10:25, Soichi Ishida wrote:

> 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.

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

top 3.

unread,
Aug 9, 2013, 12:22:49 AM8/9/13
to rails...@googlegroups.com
منتديات توب عرب للمبدعين احساس منتديات عامة
افضل المنتديات العربية
http://www.top3rab.mrsaal.com
http://www.top3rab.mrsaal.com/forums/toparab10
http://www.top3rab.mrsaal.com/forums/toparab14
http://www.top3rab.mrsaal.com/forums/toparab41
http://www.top3rab.mrsaal.com/forums/toparab47
http://www.top3rab.mrsaal.com/forums/toparab54
http://www.top3rab.mrsaal.com/forums/toparab49
http://www.top3rab.mrsaal.com/forums/toparab49
http://www.top3rab.mrsaal.com/forums/toparab48
http://www.top3rab.mrsaal.com/forums/toparab48
http://www.top3rab.mrsaal.com/forums/toparab50
http://www.top3rab.mrsaal.com/forums/toparab51
http://www.top3rab.mrsaal.com/forums/toparab52
http://www.top3rab.mrsaal.com/forums/toparab53
http://www.top3rab.mrsaal.com/forums/toparab53
http://www.top3rab.mrsaal.com/forums/toparab55
http://www.top3rab.mrsaal.com/forums/toparab56
http://www.top3rab.mrsaal.com/forums/toparab57
http://www.top3rab.mrsaal.com/forums/toparab57
http://www.top3rab.mrsaal.com/forums/toparab58
http://www.top3rab.mrsaal.com/forums/toparab59
http://www.top3rab.mrsaal.com/forums/toparab60
http://www.top3rab.mrsaal.com/forums/toparab61
http://www.top3rab.mrsaal.com/forums/toparab62
http://www.top3rab.mrsaal.com/forums/toparab62
http://www.top3rab.mrsaal.com/forums/toparab64
http://www.top3rab.mrsaal.com/forums/toparab65
http://www.top3rab.mrsaal.com/forums/toparab66
http://www.top3rab.mrsaal.com/forums/toparab67
http://www.top3rab.mrsaal.com/forums/toparab67
http://www.top3rab.mrsaal.com/forums/toparab68
http://www.top3rab.mrsaal.com/forums/toparab69
http://www.top3rab.mrsaal.com/forums/toparab69
http://www.top3rab.mrsaal.com/forums/toparab70/
http://www.top3rab.mrsaal.com/forums/toparab71
http://www.top3rab.mrsaal.com/forums/toparab72
http://www.top3rab.mrsaal.com/forums/toparab72
http://www.top3rab.mrsaal.com/forums/toparab73
http://www.top3rab.mrsaal.com/forums/toparab74
http://www.top3rab.mrsaal.com/forums/toparab75
http://www.top3rab.mrsaal.com/forums/toparab75
http://www.top3rab.mrsaal.com/forums/toparab75
http://www.top3rab.mrsaal.com/forums/toparab76
http://www.top3rab.mrsaal.com/forums/toparab76
http://www.top3rab.mrsaal.com/forums/toparab77
http://www.top3rab.mrsaal.com/forums/toparab78
http://www.top3rab.mrsaal.com/forums/toparab79
http://www.top3rab.mrsaal.com/forums/toparab79
http://www.top3rab.mrsaal.com/forums/toparab80
http://www.top3rab.mrsaal.com/forums/toparab80
http://www.top3rab.mrsaal.com/forums/toparab81
http://www.top3rab.mrsaal.com/forums/toparab81
http://www.top3rab.mrsaal.com/forums/toparab81
http://www.top3rab.mrsaal.com/forums/toparab82
http://www.top3rab.mrsaal.com/forums/toparab82
http://www.top3rab.mrsaal.com/forums/toparab82
http://www.top3rab.mrsaal.com/forums/toparab42
http://www.top3rab.mrsaal.com/forums/toparab43
http://www.top3rab.mrsaal.com/forums/toparab43
http://www.top3rab.mrsaal.com/forums/toparab45
http://www.top3rab.mrsaal.com/forums/toparab46
http://www.top3rab.mrsaal.com/forums/toparab44
http://www.top3rab.mrsaal.com/forums/toparab44
Reply all
Reply to author
Forward
0 new messages