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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
E6dcb4743b83392a4fc39693f110cc26?d=identicon&s=25 Eugenol (Guest)
on 2006-05-15 23: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 =
ENV['HTTP_ACCEPT_LANGUAGE'].match(ENV['APP_AVAILABLE_LOCALES'] )
session[:locale] = locale[0]  # else we're not lucky  else
session[:locale] = ENV['APP_DEFAULT_LOCALE']  endend
8d30b78dcd3ae8ff8d5e6085059060c7?d=identicon&s=25 Martin Bernd Schmeil (thebernd)
on 2006-07-04 11: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:

http://www.textsnippets.com/posts/show/546

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.

Cheers,

Thiago Arrais

-- end --
This topic is locked and can not be replied to.