Module: TextHelpers::Translation

Defined in:
lib/text_helpers/translation.rb

Instance Method Summary collapse

Instance Method Details

#html(key, options = {}) ⇒ Object

Public: Get an HTML representation of the rendered markdown for the passed I18n key.

key - The desired I18n lookup key. options - A Hash of options to pass through to the lookup.

:inline - A special option that will remove the enclosing <p>
          tags when set to true.

Returns a String containing the localized text rendered via Markdown



35
36
37
38
39
40
41
42
# File 'lib/text_helpers/translation.rb', line 35

def html(key, options = {})
  rendered = GitHub::Markdown.render(text(key, options))
  if options[:inline]
    rendered.gsub(/<\/?p>/, '').html_safe
  else
    rendered.html_safe
  end
end

#text(key, options = {}) ⇒ Object

Public: Get the I18n localized text for the passed key.

key - The desired I18n lookup key. options - A Hash of options to pass through to the lookup.

Returns a String resulting from the I18n lookup.



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/text_helpers/translation.rb', line 15

def text(key, options = {})
  text = I18n.t(key, {
    scope: self.translation_scope,
    default: "!#{key}!"
  }.merge(options))

  # Interpolate any keypaths (e.g., `!some.lookup.key!`) found in the text.
  text.strip.gsub(/!([\w._]+)!/) do |match|
    I18n.t($1)
  end.html_safe
end