Sha256: 06667849f0a94c3336cdfd53a33033df0dfe932d03340ada2466f1a5eeaabbde

Contents?: true

Size: 1.2 KB

Versions: 54

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

module ActiveSupport
  module HtmlSafeTranslation # :nodoc:
    extend self

    def translate(key, **options)
      if html_safe_translation_key?(key)
        html_safe_options = html_escape_translation_options(options)
        translation = I18n.translate(key, **html_safe_options)
        html_safe_translation(translation)
      else
        I18n.translate(key, **options)
      end
    end

    private
      def html_safe_translation_key?(key)
        /(?:_|\b)html\z/.match?(key)
      end

      def html_escape_translation_options(options)
        options.each do |name, value|
          unless i18n_option?(name) || (name == :count && value.is_a?(Numeric))
            options[name] = ERB::Util.html_escape(value.to_s)
          end
        end
      end

      def i18n_option?(name)
        (@i18n_option_names ||= I18n::RESERVED_KEYS.to_set).include?(name)
      end


      def html_safe_translation(translation)
        if translation.respond_to?(:map)
          translation.map { |element| element.respond_to?(:html_safe) ? element.html_safe : element }
        else
          translation.respond_to?(:html_safe) ? translation.html_safe : translation
        end
      end
  end
end

Version data entries

54 entries across 51 versions & 8 rubygems

Version Path
activesupport-7.0.8.7 lib/active_support/html_safe_translation.rb
activesupport-7.0.8.6 lib/active_support/html_safe_translation.rb
activesupport-7.0.8.5 lib/active_support/html_safe_translation.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/activesupport-7.0.8.4/lib/active_support/html_safe_translation.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/activesupport-7.0.5.1/lib/active_support/html_safe_translation.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/activesupport-7.0.5.1/lib/active_support/html_safe_translation.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/activesupport-7.0.5.1/lib/active_support/html_safe_translation.rb
activesupport-7.0.8.4 lib/active_support/html_safe_translation.rb
activesupport-7.1.3.1 lib/active_support/html_safe_translation.rb
activesupport-7.0.8.1 lib/active_support/html_safe_translation.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/activesupport-7.1.3/lib/active_support/html_safe_translation.rb
activesupport-7.1.3 lib/active_support/html_safe_translation.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activesupport-7.0.3.1/lib/active_support/html_safe_translation.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activesupport-7.0.2.3/lib/active_support/html_safe_translation.rb
activesupport-7.1.2 lib/active_support/html_safe_translation.rb
activesupport-7.1.1 lib/active_support/html_safe_translation.rb
activesupport-7.1.0 lib/active_support/html_safe_translation.rb
activesupport-7.1.0.rc2 lib/active_support/html_safe_translation.rb
activesupport-7.1.0.rc1 lib/active_support/html_safe_translation.rb
activesupport-7.1.0.beta1 lib/active_support/html_safe_translation.rb