Sha256: 9a4d647841718328228aa66ba42a9a078b157f94640a094f1f7ba1bbe2e3ca6d

Contents?: true

Size: 1.3 KB

Versions: 5

Compression:

Stored size: 1.3 KB

Contents

# frozen_string_literal: true

module Decidim
  # Helper that provides convenient methods to deal with translated attributes.
  module TranslationsHelper
    # Public: Returns the translation of an attribute using the current locale,
    # if available. Checks for the organization default locale as fallback.
    #
    # attribute - A Hash where keys (strings) are locales, and their values are
    #             the translation for each locale.
    #
    # Returns a String with the translation.
    def translated_attribute(attribute)
      attribute.try(:[], I18n.locale.to_s).presence ||
        attribute.try(:[], current_organization.default_locale).presence ||
        ""
    end

    # Public: Creates a translation for each available language in the list
    # given a translation key.
    #
    # key     - The key to translate.
    # locales - A list of locales to scope the translations to. Picks up all the
    #           available locales by default.
    #
    # Returns a Hash with the locales as keys and the translations as values.
    def multi_translation(key, locales = Decidim.available_locales)
      locales.each_with_object({}) do |locale, result|
        I18n.with_locale(locale) do
          result[locale.to_sym] = I18n.t(key)
        end
      end
    end
    module_function :multi_translation
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
decidim-core-0.8.4 app/helpers/decidim/translations_helper.rb
decidim-core-0.8.3 app/helpers/decidim/translations_helper.rb
decidim-core-0.8.2 app/helpers/decidim/translations_helper.rb
decidim-core-0.8.1 app/helpers/decidim/translations_helper.rb
decidim-core-0.8.0 app/helpers/decidim/translations_helper.rb