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
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.