File: translation.rb

package info (click to toggle)
ruby-noticed 3.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 324 kB
  • sloc: ruby: 1,186; makefile: 4
file content (31 lines) | stat: -rw-r--r-- 757 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
module Noticed
  module Translation
    extend ActiveSupport::Concern

    # Returns the +i18n_scope+ for the class. Overwrite if you want custom lookup.
    def i18n_scope
      :notifiers
    end

    def class_scope
      self.class.name.underscore.tr("/", ".")
    end

    def translate(key, **options)
      if defined?(::ActiveSupport::HtmlSafeTranslation)
        ActiveSupport::HtmlSafeTranslation.translate scope_translation_key(key), **options
      else
        I18n.translate scope_translation_key(key), **options
      end
    end
    alias_method :t, :translate

    def scope_translation_key(key)
      if key.to_s.start_with?(".")
        [i18n_scope, class_scope].compact.join(".") + key
      else
        key
      end
    end
  end
end