Class: Dry::Validation::Rust::I18nBackend
- Inherits:
-
MessageBackend
- Object
- MessageBackend
- Dry::Validation::Rust::I18nBackend
- Defined in:
- lib/dry/validation/rust/message_backend.rb
Overview
Resolves messages through the optional i18n gem.
Instance Method Summary collapse
-
#initialize(config) ⇒ I18nBackend
constructor
A new instance of I18nBackend.
- #message(code:, predicate:, args:, type:, fallback:) ⇒ Object
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 (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 |