Module: Servitium::I18n

Included in:
Context, Service
Defined in:
lib/servitium/i18n.rb

Instance Method Summary collapse

Instance Method Details

#t(key, passed_options = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/servitium/i18n.rb', line 5

def t(key, passed_options = {})
  @service_scope ||= nil

  unless @service_scope.present?
    parts = (is_a?(Class) ? self : self.class).to_s.underscore.tr("/", ".").split(".")
    parts[-1] = "#{parts.last.gsub("_service", "").pluralize}.service" if parts.last.end_with?("_service")
    parts[-1] = "#{parts.last.gsub("_context", "").pluralize}.context" if parts.last.end_with?("_context")
    @service_scope = parts.compact.join(".")
  end

  options = {scope: @service_scope}
  options[:default] = ::I18n.t(key) unless key.start_with?(".")

  ::I18n.t(key, **options.merge(passed_options))
end