Sha256: f55ad1fbd6f926c766ac4db672c7b5f030a8799e3c9d3642c76743cd63b660fa

Contents?: true

Size: 1.4 KB

Versions: 6

Compression:

Stored size: 1.4 KB

Contents

# frozen_string_literal: true

module Decidim
  # A helper that converts an array of locale IDs (two-letter identifiers, e.g.
  # `"en"`) to an array of Objects that have both the ID and their name in
  # their own language (e.g., `"English"`).
  #
  # This is intended to be used in forms, when selecting the default locale
  # form a given list, or when creating a list of radio buttons, for example.
  #
  # Examples:
  #
  #   <%= form.collection_select :default_locale, localized_locales(current_organization.available_locales), :id, :name %>
  #
  #   <% localized_locales(current_organization.available_locales).each do |locale| %>
  #     <p><%= locale.name %></p>
  #   <% end %>
  #
  module LocalizedLocalesHelper
    # Converts a given array of strings to an array of Objects representing
    # locales.
    #
    # collection - an Array of Strings. By default it uses all the available
    #   locales in Decidim, but you can pass another collection of locales (for
    #   example, the available locales for an organization)
    def localized_locales(collection = Decidim.available_locales)
      klass = Class.new do
        def initialize(locale)
          @locale = locale
        end

        def id
          @locale.to_s
        end

        def name
          I18n.with_locale(@locale) { I18n.t("name", scope: "locale") }
        end
      end

      collection.map { |locale| klass.new(locale) }
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
decidim-core-0.29.1 app/helpers/decidim/localized_locales_helper.rb
decidim-core-0.29.0 app/helpers/decidim/localized_locales_helper.rb
decidim-core-0.29.0.rc4 app/helpers/decidim/localized_locales_helper.rb
decidim-core-0.29.0.rc3 app/helpers/decidim/localized_locales_helper.rb
decidim-core-0.29.0.rc2 app/helpers/decidim/localized_locales_helper.rb
decidim-core-0.29.0.rc1 app/helpers/decidim/localized_locales_helper.rb