Sha256: 2a58b807d055106f16ef2f86f9f579abfe320ae17e47a0afd4eadb1aa3cfefc7

Contents?: true

Size: 1.1 KB

Versions: 6

Compression:

Stored size: 1.1 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 = @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

      end

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

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
locomotive_cms-2.5.7 lib/locomotive/liquid/filters/translate.rb
locomotivecms-3.0.0.pre.alpha.2 lib/locomotive/previous_liquid/filters/translate.rb
locomotivecms-3.0.0.pre.alpha lib/locomotive/previous_liquid/filters/translate.rb
locomotive_cms-2.5.6 lib/locomotive/liquid/filters/translate.rb
locomotive_cms-2.5.6.rc2 lib/locomotive/liquid/filters/translate.rb
locomotive_cms-2.5.6.rc1 lib/locomotive/liquid/filters/translate.rb