Module: RailsI18nOnair::LiveUi::Unwrapper

Defined in:
lib/rails_i18n_onair/live_ui/unwrapper.rb

Overview

Strips Live UI wrapping (editable tags and ⟦i18n⟧ markers) from a string, restoring the plain translated text.

Needed when a wrapped translation is passed as an interpolation value to another t() call — e.g. t("greeting", name: t("app_name")) — where the wrapper markup would otherwise end up escaped inside the outer text.

Constant Summary collapse

SPAN_PATTERN =
%r{<span data-i18n-onair="true"[^>]*>(.*?)</span>}m.freeze
MARKER_PATTERN =
/⟦\/?i18n[^⟧]*⟧/.freeze

Class Method Summary collapse

Class Method Details

.unwrap(value) ⇒ Object

Returns a plain String with any Live UI wrapping removed. Non-strings and strings without wrapping pass through untouched.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rails_i18n_onair/live_ui/unwrapper.rb', line 19

def unwrap(value)
  return value unless value.is_a?(String)
  return value unless value.include?("data-i18n-onair") || value.include?("")

  cleaned = value.to_str
  # Nested spans resolve one layer per pass; a few passes cover any
  # realistic nesting depth.
  3.times do
    replaced = cleaned.gsub(SPAN_PATTERN) { CGI.unescapeHTML($1) }
    break if replaced == cleaned

    cleaned = replaced
  end
  cleaned.gsub(MARKER_PATTERN, "")
end

.unwrap_options!(options) ⇒ Object

Unwraps every String inside t() options (interpolation variables, string defaults, arrays of defaults). Mutates and returns the hash.



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rails_i18n_onair/live_ui/unwrapper.rb', line 37

def unwrap_options!(options)
  options.each do |key, val|
    case val
    when String
      options[key] = unwrap(val)
    when Array
      options[key] = val.map { |v| v.is_a?(String) ? unwrap(v) : v }
    end
  end
  options
end