Module: Zabon::Helper

Includes:
ActionView::Helpers::TagHelper
Defined in:
lib/zabon/helper.rb

Instance Method Summary collapse

Instance Method Details

#zabon_translate(key, **options) ⇒ Object

can be used as a replacement for ActionView::Helpers::TranslationHelper.translate will use original translate method if locale is not :ja or translation is missing if not will split translation into semantic chunks wrap them into a configurable HTML tag and join again



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/zabon/helper.rb', line 14

def zabon_translate(key, **options)
  orig_translate = options[:orig_translate] || :translate
  translate_options = options.except(:orig_translate)

  locale = (options[:locale] || I18n.locale || :en).to_sym

  return public_send(orig_translate, key, **translate_options) if locale != :ja # if locale is not Japanese we use original method

  return key.map { |k| zabon_translate(k, **options) } if key.is_a?(Array)

  return public_send(orig_translate, key, **translate_options) unless I18n.exists?(key, locale: :ja)

  orig_translation = public_send(orig_translate, key, **translate_options)

  orig_translation = ActionView::Base.full_sanitizer.sanitize(orig_translation, tags: []) if Zabon.config.strip_tags

  Sentry.set_context("zabon", { key: key, locale: locale }) if defined?(Sentry)

  translation = Zabon.split(orig_translation).map do |segment|
    (Zabon.config.tag, segment, Zabon.config.tag_options)
  end.join.html_safe

  block_given? ? yield(translation, key) : translation
end