how to set locale automatically on ruby on rails? For instance, if the web pages is opened up in Spain then the locale=es, similarly if it is in united kingdom then the locale=en and alike?
Please help me out.
how to set locale automatically on ruby on rails? For instance, if the web pages is opened up in Spain then the locale=es, similarly if it is in united kingdom then the locale=en and alike?
Please help me out.
If i get what u mean, u will have to work with geolocation, get the user location based on his ip, and then set the locale based on it, but u must not forgot to set some default in case u can't get nothing of the user's ip.
Im sorry to say, but i didnt worked with geolocation on ruby yet. I did a fast research on rubygems and couldnt find any gem to simplify your work. =(
From the Rails Guide, include this code in the ApplicationController
Essentially, this strips the final identifier (.en, .id, etc) from a url like application.co.id or example.com and sets the locale based on that. If a user comes from a locale you don't support, it returns to the default locale - English, unless set otherwise.
Include this is
ApplicationController
orBaseController
This means set
locale
to default or use locale from request, priority to the one sent in request. If you want to expand this to user, session, request you can do this.More info here Similar question.
try using
gem geocoder
and i18n_data gem and have a before_filter to a method which doesYou can implement it like this in your
ApplicationController
:It will "inspect" the received request, find the language of the client's browser and set it as your I18n locale.
Take a look at the RubyOnRails Guide about I18n for further instructions.
I highly recommend to have a set of supported locales and fallback to a default locale. Something like this: