From 2176f4b30c9f4ea57a0fd4182808976b64202ab0 Mon Sep 17 00:00:00 2001 From: Hugo Vacher Date: Wed, 27 Feb 2019 09:42:02 -0500 Subject: Fall back to parent locale before it falls back to the :errors namespace --- activemodel/lib/active_model/errors.rb | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) (limited to 'activemodel') diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb index 9fd6f2d89c..d7e682d406 100644 --- a/activemodel/lib/active_model/errors.rb +++ b/activemodel/lib/active_model/errors.rb @@ -479,6 +479,14 @@ module ActiveModel # * errors.messages.blank def generate_message(attribute, type = :invalid, options = {}) type = options.delete(:message) if options[:message].is_a?(Symbol) + value = (attribute != :base ? @base.send(:read_attribute_for_validation, attribute) : nil) + + options = { + model: @base.model_name.human, + attribute: @base.class.human_attribute_name(attribute), + value: value, + object: @base + }.merge!(options) if @base.class.respond_to?(:i18n_scope) i18n_scope = @base.class.i18n_scope.to_s @@ -487,6 +495,11 @@ module ActiveModel :"#{i18n_scope}.errors.models.#{klass.model_name.i18n_key}.#{type}" ] end defaults << :"#{i18n_scope}.errors.messages.#{type}" + + catch(:exception) do + translation = I18n.translate(defaults.first, options.merge(default: defaults.drop(1), throw: true)) + return translation unless translation.nil? + end unless options[:message] else defaults = [] end @@ -496,15 +509,7 @@ module ActiveModel key = defaults.shift defaults = options.delete(:message) if options[:message] - value = (attribute != :base ? @base.send(:read_attribute_for_validation, attribute) : nil) - - options = { - default: defaults, - model: @base.model_name.human, - attribute: @base.class.human_attribute_name(attribute), - value: value, - object: @base - }.merge!(options) + options[:default] = defaults I18n.translate(key, options) end -- cgit v1.2.3 From 9ccc5e104287e45b21e4129991b1e507aa1a7dff Mon Sep 17 00:00:00 2001 From: Hugo Vacher Date: Mon, 11 Mar 2019 13:42:38 -0400 Subject: Update Changelog with new locale fallback behavior on validation --- activemodel/CHANGELOG.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'activemodel') diff --git a/activemodel/CHANGELOG.md b/activemodel/CHANGELOG.md index a644c63992..d7586f60ed 100644 --- a/activemodel/CHANGELOG.md +++ b/activemodel/CHANGELOG.md @@ -1,3 +1,27 @@ + +* Changed how validation error translation strings are fetched: The new behaviour + will first try the more specific keys, including doing locale fallback, then try + the less specific ones. + + For example this is the order keys will now be tried for a `blank` error on a + `product`'s `title` attribute with current locale set to `en-US`: + + en-US.activerecord.errors.models.product.attributes.title.blank + en-US.activerecord.errors.models.product.blank + en-US.activerecord.errors.messages.blank + + en.activerecord.errors.models.product.attributes.title.blank + en.activerecord.errors.models.product.blank + en.activerecord.errors.messages.blank + + en-US.errors.attributes.title.blank + en-US.errors.messages.blank + + en.errors.attributes.title.blank + en.errors.messages.blank + + *Hugo Vacher* + ## Rails 6.0.0.beta2 (February 25, 2019) ## * Fix date value when casting a multiparameter date hash to not convert -- cgit v1.2.3