Forum: Rails I18n Suggested code for auto finding the client locale

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Eugenol (Guest)
on 2006-05-16 01:20
(Received via mailing list)
# 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
Martin Bernd S. (Guest)
on 2006-07-04 13:43
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 topic is locked and can not be replied to.