Module: ViewComponent::Translatable

Extended by:
ActiveSupport::Concern
Included in:
Base
Defined in:
lib/view_component/translatable.rb

Defined Under Namespace

Classes: I18nBackend

Instance Method Summary collapse

Instance Method Details

#__vc_i18n_scopeObject



124
125
126
# File 'lib/view_component/translatable.rb', line 124

def __vc_i18n_scope
  self.class.__vc_i18n_scope
end

#translate(key = nil, **options) ⇒ Object Also known as: t



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/view_component/translatable.rb', line 93

def translate(key = nil, **options)
  raise ViewComponent::TranslateCalledBeforeRenderError if view_context.nil?

  return @view_context.translate(key, **options) unless __vc_i18n_backend
  return key.map { |k| translate(k, **options) } if key.is_a?(Array)

  locale = options.delete(:locale) || ::I18n.locale
  key = self.class.__vc_i18n_key(key, options.delete(:scope))
  as_html = HTML_SAFE_TRANSLATION_KEY.match?(key)

  __vc_html_escape_translation_options!(options) if as_html

  if key.start_with?(__vc_i18n_scope + ".")
    translated =
      catch(:exception) do
        __vc_i18n_backend.translate(locale, key, options)
      end

    # Fallback to the global translations
    if translated.is_a? ::I18n::MissingTranslation
      return @view_context.translate(key, locale: locale, **options)
    end

    translated = __vc_html_safe_translation(translated) if as_html
    translated
  else
    @view_context.translate(key, locale: locale, **options)
  end
end