Module: TranslateFilter

Defined in:
lib/liquidum/liquid/filters/translate_filter.rb

Instance Method Summary collapse

Instance Method Details

#translate(input, options = {}) ⇒ Object Also known as: t

Translate text

Example:

<div class="summary">{{'.title' | t}}</div>

provide optional locale to translate the text in, if you don’t pass it it will use I18n.locale

<div class="summary">{{'.title' | t: locale: 'nl'}}</div>

you can provide additional arguments to be used for interpolation:

<div class="summary">{{'.title' | t: gender: 'm', locale: 'nl'}}</div>


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/liquidum/liquid/filters/translate_filter.rb', line 18

def translate(input, options = {})
  result = nil

  Liquidum.config.i18n_store(@context) do |obj|
    locale = options.delete("locale")

    key = input
    scope = nil

    if key.start_with?(".")
      key = input[1..-1]
      scope = obj.translation_scope if obj.respond_to?(:translation_scope)
    end

    result = I18n.t(key, locale: locale, scope: scope, cascade: {skip_root: false},
                         **options.symbolize_keys)
    if result
      result = I18n::Backend::Simple.new.send(:interpolate, I18n.locale, result,
        options.symbolize_keys)
    end
  end

  result
end