Sha256: 16bab61c0607318e91e20470f9031331b413f64e92fa1de2fbbc091133f23de9

Contents?: true

Size: 1.02 KB

Versions: 13

Compression:

Stored size: 1.02 KB

Contents

module Locomotive
  module Liquid
    module Filters
      module Translate

        # Return the translation described by a key.
        #
        # @param [ String ] key The key of the translation.
        # @param [ String ] locale By default, it uses the value returned by I18n.locale
        # @param [ String ] scope If specified, instead of looking in the translations, it will use I18n instead.
        #
        # @return [ String ] the translated text
        #
        def translate(input, locale = nil, scope = nil)
          locale ||= I18n.locale.to_s

          if scope.blank?
            translation = Locomotive::Translation.where(key: input).first
            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

      end

      ::Liquid::Template.register_filter(Translate)
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
locomotive_cms-2.4.1 lib/locomotive/liquid/filters/translate.rb
locomotive_cms-2.4.0 lib/locomotive/liquid/filters/translate.rb
locomotive_cms-2.3.1 lib/locomotive/liquid/filters/translate.rb
locomotive_cms-2.3.0 lib/locomotive/liquid/filters/translate.rb
locomotive_cms-2.2.3 lib/locomotive/liquid/filters/translate.rb
locomotive_cms-2.2.2 lib/locomotive/liquid/filters/translate.rb
locomotive_cms-2.2.1 lib/locomotive/liquid/filters/translate.rb
locomotive_cms-2.2.0 lib/locomotive/liquid/filters/translate.rb
locomotive_cms-2.1.4 lib/locomotive/liquid/filters/translate.rb
locomotive_cms-2.1.3 lib/locomotive/liquid/filters/translate.rb
locomotive_cms-2.1.2 lib/locomotive/liquid/filters/translate.rb
locomotive_cms-2.1.1 lib/locomotive/liquid/filters/translate.rb
locomotive_cms-2.1.0 lib/locomotive/liquid/filters/translate.rb