Over a million developers have joined DZone.

Setting User Locale In Globalize, The Working Solution

·
This example application, provided as Globalize's, documentation doesn't work at all.

This is what worked for me:


    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
Topics:

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}