mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Fall back to parent locale before it falls back to the :errors namespace
This commit is contained in:
parent
d333d85254
commit
2176f4b30c
2 changed files with 32 additions and 11 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue