Suggested code for auto finding the client locale

Put in your environment.rb :#

----------------------------ENV[‘APP_AVAILABLE_LOCALES’] = /^fr|en|es/ #
suggestion…ENV[‘APP_DEFAULT_LOCALE’] = ‘fr’ # YASuggestion

And in application.rb :# -----------------------

before_filter :set_locale

def set_locale # if user clicked on a link for selecting his locale if
locale = params[:locale].match( ENV[‘APP_AVAILABLE_LOCALES’] )
session[:locale] = locale[0] # or if the locale is already set in the
session elsif session[:locale].match( ENV[‘APP_AVAILABLE_LOCALES’] ) #
or if the browser sends its locale elsif locale =
session[:locale] = locale[0] # else we’re not lucky else
session[:locale] = ENV[‘APP_DEFAULT_LOCALE’] endend

Another mail on the Globalize mailing list about this topic by Thiago
Arrais, (Globalize flavor this time):

– begin –

Here is the code I came up with:

It considers not only the accept-language field, but other user options.
In order of precedence they are:

  • Explicit locale passed on URL
  • Previous language selection (stored on user session)
  • Accept-Language field contents
  • Default locale

You may package this snippet with Globalize in any way you’d like.


Thiago A.

– end –

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs