Sha256: ce3233c6f4a5257bbda5a587b305d49b81626a597aa3f4e4c5110f36d2e2c1ea

Contents?: true

Size: 1015 Bytes

Versions: 6

Compression:

Stored size: 1015 Bytes

Contents

module Homura
  module SetLocale
    extend ActiveSupport::Concern

    included do
      before_filter :set_locale
    end

    def available_locales
      @available_locales ||= I18n.available_locales.map(&:to_s)
    end

    def locale_available?(locale)
      available_locales.include?(locale)
    end

    def set_locale
      if (param_locale = params.delete('locale')) && locale_available?(param_locale)
        cookies.permanent['locale'] = param_locale
        I18n.locale = param_locale
        redirect_to params and return
      end

      if (cookie_locale = cookies['locale']) && locale_available?(cookie_locale)
        I18n.locale = cookie_locale and return
      end

      http_accept_language = request.headers['HTTP_ACCEPT_LANGUAGE'] || ''
      preferred_locales = http_accept_language.split(',').map { |l|
        l.split(';').first.downcase.gsub(/-[a-z0-9]+$/i) { |x| x.upcase }
      }
      I18n.locale = preferred_locales.find { |l|
        locale_available?(l)
      }
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
homura-0.2.4 lib/homura/set_locale.rb
homura-0.2.3 lib/homura/set_locale.rb
homura-0.2.2 lib/homura/set_locale.rb
homura-0.2.1 lib/homura/set_locale.rb
homura-0.2.0 lib/homura/set_locale.rb
homura-0.1.4 lib/homura/set_locale.rb