Sha256: 56a08c190ffd83fa586b3b9cff9b46537ed6d187c479d8092d6f18b1df886c18
Contents?: true
Size: 1.67 KB
Versions: 46
Compression:
Stored size: 1.67 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, :default_locale # Sets the locale for the current session. # # Returns nothing. def set_locale locale = if params["locale"] params["locale"] elsif current_user && current_user.locale.present? current_user.locale end I18n.locale = available_locales.include?(locale) ? locale : default_locale 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 == 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 ||= current_organization.available_locales end # The default locale of this organization. # # Returns a String with the default locale. def default_locale @default_locale ||= (current_organization || Decidim).public_send(:default_locale) end end end end
Version data entries
46 entries across 46 versions & 2 rubygems