Class: Dry::Validation::Rust::I18nBackend

Inherits:
MessageBackend show all
Defined in:
lib/dry/validation/rust/message_backend.rb

Overview

Resolves messages through the optional i18n gem.

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ I18nBackend

Returns a new instance of I18nBackend.



86
87
88
89
90
91
# File 'lib/dry/validation/rust/message_backend.rb', line 86

def initialize(config)
  super
  @default_locale = config.default_locale.to_sym
  @top_namespace = config.top_namespace.to_s
  load_i18n(config.load_paths)
end

Instance Method Details

#message(code:, predicate:, args:, type:, fallback:) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/dry/validation/rust/message_backend.rb', line 93

def message(code:, predicate:, args:, type:, fallback:)
  key = predicate ? "#{predicate.to_s.delete_suffix('?')}?" : code.to_s
  translation_key = [*@top_namespace.split('.'), 'errors', key].join('.')
  return fallback unless ::I18n.exists?(translation_key, @default_locale)

  ::I18n.t(translation_key, locale: @default_locale, **tokens_for(args, type))
end