Sha256: 1b7a95ab81946a4e530f0c4f105ed276cb8fa81188845e37b58b813e0b574f67

Contents?: true

Size: 1.17 KB

Versions: 4

Compression:

Stored size: 1.17 KB

Contents

# frozen_string_literal: true

module TranslateFilter
  # 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>
  #

  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
  alias_method :t, :translate
end

Liquid::Template.register_filter(TranslateFilter)

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
liquidum-1.2.1 lib/liquidum/liquid/filters/translate_filter.rb
liquidum-1.2.0 lib/liquidum/liquid/filters/translate_filter.rb
liquidum-1.1.2 lib/liquidum/liquid/filters/translate_filter.rb
liquidum-1.1.1 lib/liquidum/liquid/filters/translate_filter.rb