Class: Dry::Validation::Rust::YamlBackend

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

Overview

Resolves messages from YAML translation files listed in MessageConfig#load_paths.

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ YamlBackend

Returns a new instance of YamlBackend.



48
49
50
51
52
53
# File 'lib/dry/validation/rust/message_backend.rb', line 48

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

Instance Method Details

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



55
56
57
58
59
# File 'lib/dry/validation/rust/message_backend.rb', line 55

def message(code:, predicate:, args:, type:, fallback:)
  key = predicate ? "#{predicate.to_s.delete_suffix('?')}?" : code.to_s
  template = @translations[[@default_locale, *@top_namespace.split('.'), 'errors', key].join('.')]
  template ? interpolate(template, tokens_for(args, type)) : fallback
end