Class: ReeI18n::T

Inherits:
Object
  • Object
show all
Includes:
Ree::FnDSL
Defined in:
lib/ree_lib/packages/ree_i18n/package/ree_i18n/functions/t.rb

Constant Summary collapse

DEFAULT_BY_LOCALE =
Object.new
RESERVED_KEYS =
Set.new([
  :throw, :raise, :locale, :scope, :default, :deep_interpolation,
  :count, :exception_handler, :default_by_locale
])

Instance Method Summary collapse

Instance Method Details

#call(key = nil, context = {}, **options) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/ree_lib/packages/ree_i18n/package/ree_i18n/functions/t.rb', line 59

def call(key = nil, context = {}, **options)
  context.each do |k, _|
    if RESERVED_KEYS.include?(k)
      raise ArgumentError, "translation context contains reserved key :#{k}"
    end
  end

  opts = context.merge(options)
  default_by_locale = opts.delete(:default_by_locale)

  if default_by_locale && !opts[:default]
    opts[:default] = DEFAULT_BY_LOCALE
  end

  opts[:throw] ||= false
  opts[:raise] ||= false
  opts[:locale] ||= nil

  result = I18n.t(key, **opts)

  if result == DEFAULT_BY_LOCALE
    result = call(
      key, context,
      **except(
        options.merge(locale: default_by_locale), [:default_by_locale]
      )
    )
  end

  result
end