Module: Decidim::TranslationsHelper
- Defined in:
- app/helpers/decidim/translations_helper.rb
Overview
Helper that provides convenient methods to deal with translated attributes.
Class Method Summary collapse
-
.multi_translation(key, locales = Decidim.available_locales) ⇒ Object
Public: Creates a translation for each available language in the list given a translation key.
Instance Method Summary collapse
-
#translated_attribute(attribute) ⇒ Object
Public: Returns the translation of an attribute using the current locale, if available.
Class Method Details
.multi_translation(key, locales = Decidim.available_locales) ⇒ Object
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.
27 28 29 30 31 32 33 |
# File 'app/helpers/decidim/translations_helper.rb', line 27 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 |
Instance Method Details
#translated_attribute(attribute) ⇒ Object
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.
13 14 15 16 17 |
# File 'app/helpers/decidim/translations_helper.rb', line 13 def translated_attribute(attribute) attribute.try(:[], I18n.locale.to_s).presence || attribute.try(:[], current_organization.default_locale).presence || "" end |