zilverline / sequent

CQRS & event sourcing framework for Ruby
https://sequent.io
MIT License
541 stars 58 forks source link

Translate custom errors by error_locale_resolver #388

Closed mvandiepen closed 1 year ago

mvandiepen commented 1 year ago

command.valid? fills the error messages. So wrapping that with I18n.with_locale ensures those messages are translated according to the error_locale_resolver.

Previously tried to ensure this through a command_middleware, like:

class EnsureCommandErrorLocale
  def self.call(command, &block)
    I18n.with_locale(Sequent.configuration.error_locale_resolver&.call || I18n.locale) do
      command.valid?
    end

    block.call
  end
end

But the command.valid? called in Sequent::Core::CommandService#process_command will override the messages again, reverting it back to the default I18n.locale. Hence this PR.