Sha256: b9bb657281e14eba59c10a656ee6cfd15be6cb9a32db829c953deacdc903150c

Contents?: true

Size: 1.49 KB

Versions: 5

Compression:

Stored size: 1.49 KB

Contents

# frozen_string_literal: true

require "active_support/concern"

module Decidim
  # Common logic to switch between locales.
  module LocaleSwitcher
    extend ActiveSupport::Concern

    included do
      before_action :set_locale
      helper_method :current_locale, :available_locales

      # Sets the locale for the current session.
      #
      # Returns nothing.
      def set_locale
        I18n.locale = if params["locale"] && available_locales.include?(params["locale"])
                        params["locale"]
                      elsif current_user && current_user.locale.present?
                        current_user.locale
                      else
                        I18n.default_locale
                      end
      end

      # Adds the current locale to all the URLs generated by url_for so users
      # experience a consistent behaviour if they copy or share links.
      #
      # Returns a Hash.
      def default_url_options
        return {} if current_locale == I18n.default_locale.to_s

        { locale: current_locale }
      end

      # The current locale for the user. Available as a helper for the views.
      #
      # Returns a String.
      def current_locale
        @current_locale ||= I18n.locale.to_s
      end

      # The available locales in the application. Available as a helper for the
      # views.
      #
      # Returns an Array of Strings.
      def available_locales
        @available_locales ||= I18n.available_locales.map(&:to_s)
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
decidim-core-0.0.1.alpha9 app/controllers/concerns/decidim/locale_switcher.rb
decidim-core-0.0.1.alpha8 app/controllers/concerns/decidim/locale_switcher.rb
decidim-core-0.0.1.alpha7 app/controllers/concerns/decidim/locale_switcher.rb
decidim-core-0.0.1.alpha6 app/controllers/concerns/decidim/locale_switcher.rb
decidim-core-0.0.1.alpha5 app/controllers/concerns/decidim/locale_switcher.rb