Module: Locomotive::Liquid::Filters::Translate

Defined in:
lib/locomotive/liquid/filters/translate.rb

Instance Method Summary collapse

Instance Method Details

#translate(input, locale = nil, scope = nil) ⇒ String

Return the translation described by a key.

Parameters:

  • key (String)

    The key of the translation.

  • locale (String) (defaults to: nil)

    By default, it uses the value returned by I18n.locale

  • scope (String) (defaults to: nil)

    If specified, instead of looking in the translations, it will use I18n instead.

Returns:

  • (String)

    the translated text



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/locomotive/liquid/filters/translate.rb', line 14

def translate(input, locale = nil, scope = nil)
  locale ||= I18n.locale.to_s

  if scope.blank?
    translation = @context.registers[:site].translations.where(key: input).first

    # key not found
    return input if translation.nil?

    if translation.values[locale].present?
      translation.values[locale]
    else
      translation.values[I18n.default_locale.to_s]
    end
  else
    I18n.t(input, scope: scope.split('.'), locale: locale)
  end
end