Module: RailsI18nOnair::LiveUi::TranslationHelper
- Defined in:
- lib/rails_i18n_onair/live_ui/translation_helper.rb
Overview
Prepended to ActionView::Helpers::TranslationHelper so that every t() call wraps its output in a carrying data-attributes when Live UI is active.
When Live UI is OFF or no translator is signed in the call falls straight through to the original helper — zero overhead.
Instance Method Summary collapse
-
#t(key, **options) ⇒ Object
Re-alias so that both t() and translate() go through the override.
- #translate(key, **options) ⇒ Object
Instance Method Details
#t(key, **options) ⇒ Object
Re-alias so that both t() and translate() go through the override.
87 88 89 |
# File 'lib/rails_i18n_onair/live_ui/translation_helper.rb', line 87 def t(key, **) translate(key, **) end |
#translate(key, **options) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/rails_i18n_onair/live_ui/translation_helper.rb', line 11 def translate(key, **) # Fast path: skip wrapping unless the middleware flagged this request unless _i18n_onair_live_ui_active? return super end # Allow callers to opt-out: t("key", i18n_onair: false) if .delete(:i18n_onair) == false return super end # Interpolation values may themselves be wrapped translations # (t("greeting", name: t("app_name"))) — strip the inner wrapping so # the outer text interpolates plain strings, not span markup. Unwrapper.() # Resolve lazy keys (.title → controller.action.title) resolved_key = scope_key_by_partial(key) # Include :scope so the editor saves to the correct full key path if [:scope] resolved_key = (Array([:scope]).map(&:to_s) + [resolved_key.to_s]).join(".") end # Strip region/variant: "en_FRA" → "en", "pt-BR" → "pt" # Locale files use language-only keys (en.yml, fr.yml, etc.) locale = ([:locale] || I18n.locale).to_s.split(/[-_]/).first result = super(key, **) # Only wrap plain text (t() may return a Hash subtree or an Array) return result unless result.is_a?(String) # Fetch the raw template (with %{vars} intact) for the editor. # Only needed when interpolation variables are present in options. i18n_reserved = [:scope, :default, :separator, :resolve, :object, :fallback, :format, :cascade, :throw, :raise, :locale, :exception_handler] has_interpolation = (.keys - i18n_reserved).any? raw_template = nil if has_interpolation begin raw_template = I18n.backend.translate(locale, resolved_key, {}) if raw_template.is_a?(Hash) && .key?(:count) # Pluralized entry — point the editor at the plural subkey that # produced this text, so saving edits that form instead of # overwriting the whole {one:, other:} hash. plural_key = _i18n_onair_plural_key(raw_template, [:count], result, .except(*i18n_reserved)) if plural_key resolved_key = "#{resolved_key}.#{plural_key}" raw_template = raw_template[plural_key.to_sym] || raw_template[plural_key.to_s] end end rescue StandardError raw_template = nil end end attrs = { i18n_onair: "true", i18n_key: resolved_key.to_s, i18n_locale: locale.to_s } if raw_template.is_a?(String) && raw_template.include?("%{") attrs[:i18n_raw] = raw_template interp_vars = .except(*i18n_reserved) attrs[:i18n_vars] = interp_vars.to_json if interp_vars.any? end content_tag( :span, result, data: attrs, style: "display:contents" ) end |