diff options
-rw-r--r-- | Gemfile.lock | 6 | ||||
-rw-r--r-- | activemodel/CHANGELOG.md | 24 | ||||
-rw-r--r-- | activemodel/lib/active_model/errors.rb | 23 | ||||
-rw-r--r-- | activerecord/test/cases/validations/i18n_generate_message_validation_test.rb | 20 | ||||
-rw-r--r-- | activesupport/CHANGELOG.md | 6 | ||||
-rw-r--r-- | activesupport/activesupport.gemspec | 2 | ||||
-rw-r--r-- | railties/lib/rails/commands/encrypted/USAGE | 2 |
7 files changed, 65 insertions, 18 deletions
diff --git a/Gemfile.lock b/Gemfile.lock index c778569e72..cc2bb76150 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -70,7 +70,7 @@ PATH i18n (>= 0.7, < 2) minitest (~> 5.1) tzinfo (~> 1.1) - zeitwerk (~> 1.3, >= 1.3.2) + zeitwerk (~> 1.3, >= 1.3.3) rails (6.0.0.beta2) actioncable (= 6.0.0.beta2) actionmailbox (= 6.0.0.beta2) @@ -277,7 +277,7 @@ GEM hiredis (0.6.3-java) http_parser.rb (0.6.0) httpclient (2.8.3) - i18n (1.5.3) + i18n (1.6.0) concurrent-ruby (~> 1.0) image_processing (1.7.1) mini_magick (~> 4.0) @@ -517,7 +517,7 @@ GEM websocket-extensions (0.1.3) xpath (3.2.0) nokogiri (~> 1.8) - zeitwerk (1.3.2) + zeitwerk (1.3.3) PLATFORMS java 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 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 # * <tt>errors.messages.blank</tt> 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 diff --git a/activerecord/test/cases/validations/i18n_generate_message_validation_test.rb b/activerecord/test/cases/validations/i18n_generate_message_validation_test.rb index 703c24b340..993c201f03 100644 --- a/activerecord/test/cases/validations/i18n_generate_message_validation_test.rb +++ b/activerecord/test/cases/validations/i18n_generate_message_validation_test.rb @@ -4,16 +4,20 @@ require "cases/helper" require "models/topic" class I18nGenerateMessageValidationTest < ActiveRecord::TestCase + class Backend < I18n::Backend::Simple + include I18n::Backend::Fallbacks + end + def setup Topic.clear_validators! @topic = Topic.new - I18n.backend = I18n::Backend::Simple.new + I18n.backend = Backend.new end def reset_i18n_load_path @old_load_path, @old_backend = I18n.load_path.dup, I18n.backend I18n.load_path.clear - I18n.backend = I18n::Backend::Simple.new + I18n.backend = Backend.new yield ensure I18n.load_path.replace @old_load_path @@ -83,4 +87,16 @@ class I18nGenerateMessageValidationTest < ActiveRecord::TestCase assert_equal "Custom taken message", @topic.errors.generate_message(:title, :taken, value: "title") end end + + test "activerecord attributes scope falls back to parent locale before it falls back to the :errors namespace" do + reset_i18n_load_path do + I18n.backend.store_translations "en", activerecord: { errors: { models: { topic: { attributes: { title: { taken: "custom en message" } } } } } } + I18n.backend.store_translations "en-US", errors: { messages: { taken: "generic en-US fallback" } } + + I18n.with_locale "en-US" do + assert_equal "custom en message", @topic.errors.generate_message(:title, :taken, value: "title") + assert_equal "generic en-US fallback", @topic.errors.generate_message(:heading, :taken, value: "heading") + end + end + end end diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index 14d0ca047a..4da6421ea4 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,4 +1,4 @@ -* Add support for supplying `locale` to `transliterate` and `parameterize` +* Add support for supplying `locale` to `transliterate` and `parameterize`. I18n.backend.store_translations(:de, i18n: { transliterate: { rule: { "ΓΌ" => "ue" } } }) @@ -20,12 +20,13 @@ *DHH* * Added Array#including and Enumerable#including to conveniently enlarge a collection with more members using a method rather than an operator: - + [ 1, 2, 3 ].including(4, 5) => [ 1, 2, 3, 4, 5 ] post.authors.including(Current.person) => All the authors plus the current person! *DHH* + ## Rails 6.0.0.beta2 (February 25, 2019) ## * New autoloading based on [Zeitwerk](https://github.com/fxn/zeitwerk). @@ -54,6 +55,7 @@ *Guillermo Iguaran* + ## Rails 6.0.0.beta1 (January 18, 2019) ## * Remove deprecated `Module#reachable?` method. diff --git a/activesupport/activesupport.gemspec b/activesupport/activesupport.gemspec index 2781d4e6f7..8409a0c420 100644 --- a/activesupport/activesupport.gemspec +++ b/activesupport/activesupport.gemspec @@ -34,5 +34,5 @@ Gem::Specification.new do |s| s.add_dependency "tzinfo", "~> 1.1" s.add_dependency "minitest", "~> 5.1" s.add_dependency "concurrent-ruby", "~> 1.0", ">= 1.0.2" - s.add_dependency "zeitwerk", "~> 1.3", ">= 1.3.2" + s.add_dependency "zeitwerk", "~> 1.3", ">= 1.3.3" end diff --git a/railties/lib/rails/commands/encrypted/USAGE b/railties/lib/rails/commands/encrypted/USAGE index 7ec3ebb5d7..253eec2378 100644 --- a/railties/lib/rails/commands/encrypted/USAGE +++ b/railties/lib/rails/commands/encrypted/USAGE @@ -6,7 +6,7 @@ See the `Rails.application.encrypted` documentation for using them in your app. === Encryption Keys By default, Rails looks for the encryption key in `config/master.key` or -`ENV["RAILS_MASTER_KEY"]`, but that lookup can be overriden with `--key`: +`ENV["RAILS_MASTER_KEY"]`, but that lookup can be overridden with `--key`: rails encrypted:edit config/encrypted_file.yml.enc --key config/encrypted_file.key |