Module: Bridgetown::Foundation::Packages::SafeTranslations

Extended by:
Inclusive::Public
Defined in:
bridgetown-foundation/lib/bridgetown/foundation/packages/safe_translations.rb

Overview

NOTE: this is tested by test/test_ruby_helpers.rb in bridgetown-core

This is loosely based on the HtmlSafeTranslation module from ActiveSupport, but you can actually use it for any kind of safety use case in a translation setting because its decoupled from any specific escaping or safety mechanisms.

Instance Method Summary collapse

Instance Method Details

#escape_translation_options(options, escaper) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'bridgetown-foundation/lib/bridgetown/foundation/packages/safe_translations.rb', line 30

def escape_translation_options(options, escaper)
  @reserved_i18n_keys ||= I18n::RESERVED_KEYS.to_set

  options.to_h do |name, value|
    unless @reserved_i18n_keys.include?(name) || (name == :count && value.is_a?(Numeric))
      next [name, escaper.(value)]
    end

    [name, value]
  end
end

#safe_translation(translation, safety_method) ⇒ Object



42
43
44
45
46
47
48
# File 'bridgetown-foundation/lib/bridgetown/foundation/packages/safe_translations.rb', line 42

def safe_translation(translation, safety_method)
  @safe_value ||= -> { _1.respond_to?(safety_method) ? _1.send(safety_method) : _1 }

  return translation.map { @safe_value.(_1) } if translation.respond_to?(:map)

  @safe_value.(translation)
end

#translate(key, escaper, safety_method = :html_safe, **options) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'bridgetown-foundation/lib/bridgetown/foundation/packages/safe_translations.rb', line 13

def translate(key, escaper, safety_method = :html_safe, **options)
  safe_options = escape_translation_options(options, escaper)

  i18n_error = false

  exception_handler = ->(*args) do
    i18n_error = true
    I18n.exception_handler.(*args)
  end

  I18n.translate(key, **safe_options, exception_handler:).then do |translation|
    i18n_error ? translation : safe_translation(translation, safety_method)
  end
end