2

I am using Trailblazer 2.1 (and dry-validation (0.11.1) consequently) in a Rails 5 project.

I have 2 different language locales bound to the user profile.

When I dynamically change the user's locale - dry-validation does not notice it - and keeps yielding the validation errors with a former locale.

What's wrong? Is this or bug or a feature? Why doesn't dry-validation respond to the locale change? How to fix it?

Here's my locale switching code:

class ApplicationController < ActionController::Base

  before_action :switch_locale

  def switch_locale

    I18n.locale = current_user.locale

  end

end
prograils
  • 2,248
  • 1
  • 28
  • 45

1 Answers1

1

I acidentally found the solution. It's simply a bug inside dry-validation. I found and successfully applied a work-around to this problem:

config/initializers/dry_validation.rb:

Rails.application.configure do
  Dry::Validation::Schema.configure do |config|
    config.messages = :i18n
  end

  Dry::Validation::Schema::Form.configure do |config|
    config.messages = :i18n
  end
end

# https://github.com/dry-rb/dry-validation/issues/368
# Monkey patch MessageCompiler to make the failing example pass:

module PatchMessageCompiler
  def default_lookup_options
    { locale: messages.default_locale }
  end
end

Dry::Validation::MessageCompiler.prepend PatchMessageCompiler
prograils
  • 2,248
  • 1
  • 28
  • 45