Sha256: c7c3ff589b49444c47d90fc3c0996c136466acf712c0cdd85bd31942540ebfb4

Contents?: true

Size: 1.37 KB

Versions: 3

Compression:

Stored size: 1.37 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 passa nother 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.t(id, scope: "locales")
        end
      end

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
decidim-core-0.0.3 app/helpers/decidim/localized_locales_helper.rb
decidim-core-0.0.2 app/helpers/decidim/localized_locales_helper.rb
decidim-core-0.0.1 app/helpers/decidim/localized_locales_helper.rb