Setting Globalize locale based on the Accept-Language HTTP Header Field
This code sets the Globalize locale based on the http accept-language header field. Other options are also considered, in order or precedence:
- Explicetely locale set on URL
- Previous language selection (stored on user session)
- Accept-Language field contents
- Default locale
- Explicetely locale set on URL
- Previous language selection (stored on user session)
- Accept-Language field contents
- Default locale
before_filter :set_locale def set_locale default_locale = 'en-US' request_language = request.env['HTTP_ACCEPT_LANGUAGE'] request_language = request_language.nil? ? nil : request_language[/[^,;]+/] @locale = params[:locale] || session[:locale] || request_language || default_locale session[:locale] = @locale begin Locale.set @locale rescue @locale = default_locale Locale.set @locale end end