Sha256: 1f065667383942e4c724f0d610367070b0d2fbaed8addf2ef2f0dfb1a8441ae5

Contents?: true

Size: 924 Bytes

Versions: 3

Compression:

Stored size: 924 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 params['locale'] && locale_available?(params['locale'])
        cookies.permanent['locale'] = locale
        I18n.locale = locale and return
      end

      if cookies['locale'] && locale_available?(locale)
        I18n.locale = 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

3 entries across 3 versions & 1 rubygems

Version Path
homura-0.1.2 lib/homura/set_locale.rb
homura-0.1.1 lib/homura/set_locale.rb
homura-0.1.0 lib/homura/set_locale.rb