DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Setting User Locale In Globalize, The Working Solution

08.14.2008
| 4165 views |
  • submit to reddit
        <a href="http://www.globalize-rails.org/pages/example-application">This example application</a>, provided as Globalize's, documentation doesn't work at all.

This is what worked for me:

<pre>
    before_filter :set_locale

    def set_locale
      request_language = request.env['HTTP_ACCEPT_LANGUAGE']
      request_language = request_language.nil? ? nil : request_language[/[^,;]+/].split('-')[0]

      @locale = params[:locale] || request_language || Locale.base_language.code

      if !params[:locale].nil? && LOCALES.keys.include?(params[:locale].to_sym)
        Locale.set LOCALES[@locale.to_sym]
      else
        redirect_to params.merge('locale' => @locale)
      end
    end
</pre>