Sha256: 0b5e08131c413ca1d242e120021c1a927c2066ccfd260307d3121625bc0ab1dc

Contents?: true

Size: 787 Bytes

Versions: 7

Compression:

Stored size: 787 Bytes

Contents

# frozen_string_literal: true

require_dependency "decidim/application_controller"

module Decidim
  # A controller to allow users switching their locale.
  class LocalesController < ApplicationController
    authorize_resource :locales, class: false

    def create
      if current_user && params["locale"] && available_locales.include?(params["locale"])
        current_user.update_attribute(:locale, params["locale"])
      end

      redirect_to referer_with_new_locale
    end

    private

    def referer_with_new_locale
      uri = URI(request.referrer || "/")
      query = uri.query.to_s.gsub(/locale\=[a-zA-Z\-]{2,5}/, "")
      params = URI.decode_www_form(query) << ["locale", current_locale]
      uri.query = URI.encode_www_form(params)

      uri.to_s
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
decidim-core-0.4.2 app/controllers/decidim/locales_controller.rb
decidim-core-0.4.1 app/controllers/decidim/locales_controller.rb
decidim-core-0.4.0 app/controllers/decidim/locales_controller.rb
decidim-core-0.3.2 app/controllers/decidim/locales_controller.rb
decidim-core-0.3.1 app/controllers/decidim/locales_controller.rb
decidim-core-0.3.0 app/controllers/decidim/locales_controller.rb
decidim-core-0.2.0 app/controllers/decidim/locales_controller.rb