Inheriting Rails i18n validation error messages in a subclass

What i understand

Suppose I have a class with convenient validation, for example:

User < ActiveRecord::Base
    validates :username, :format => {/regex/}, :message => :name_format
end

In this case, I can use i18nto make the error message translated by including the following in mine /config/locals/en.yml:

en:
    activerecord:
        errors:
            models:
                user:
                    attributes:
                        username:
                            name_format: 'has the way-wrong format, bro!'

This is normal and generally very convenient.

What I want to know:

My question is: what happens when I have subclasses inheriting from the user:

UserSubclassOne < User
    # extra stuff
end
UserSubclassTwo < User
    # extra stuff
end
...
UserSubclassEnn < User
    # extra stuff
end

Now the problem is that Rails cannot find the translation user_subclass_one.attributes.username.name_format.

He complains:

translation missing:
en.activerecord.errors.models.user_subclass_one.attributes.username.name_format

I hope that Rails will look up the hierarchy UserSubclassOnebefore Userwhen searching for the string in en.yml, and then notice when it gets a β€œhit”, but (if I didn’t do something terribly wrong), apparently this does not happen.

en.yml.en.errors.models User, user_subclass_one, user_subclass_two .., Rails , .

, ?

:

User gem MyGem, Rails MyEngine, Rails MyApp, UserSubclassOne,..., UserSubclassEnn, , , MyGem::User, en.yml, - , , .

/:

, , . , MyApp ( UserSubclassOne) MyGem ( User). , User MyGem ( ), User :

User < ActiveRecord::Base

MyGem::User < ActiveRecord::Base

.

i18n , my_gem/user, User, my_gem.user, my_gem: user ..

, en.yml : /config/locals/en.yml:

en:
    activerecord:
        errors:
            models:
                my_gem/user:
                    attributes:
                        username:
                            name_format: 'has the way-wrong format, bro!'

!

+5
2

, , . , MyApp ( UserSubclassOne) MyGem ( User). , User MyGem ( ), User :

User < ActiveRecord::Base

MyGem::User < ActiveRecord::Base

.

i18n , my_gem/user, User, my_gem.user, my_gem: user ..

, en.yml : /config/locals/en.yml:

en:
    activerecord:
        errors:
            models:
                my_gem/user:
                    attributes:
                        username:
                            name_format: 'has the way-wrong format, bro!'

!

+6

Rails Guides i18n (5.1.1) , :

:

class User < ActiveRecord::Base
  validates :name, :presence => true
end

<... ... >

, .

, , :

class Admin < User
  validates :name, :presence => true
end

:

activerecord.errors.models.admin.attributes.name.blank
activerecord.errors.models.admin.blank
activerecord.errors.models.user.attributes.name.blank
activerecord.errors.models.user.blank
activerecord.errors.messages.blank
errors.attributes.name.blank
errors.messages.blank

, , .

, , :

//user.rb

User < ActiveRecord::Base
  validates :username, :format => {/regex/}, :message => :name_format
end

//user_subclass.rb

UserSubclass < User
  validates :username, :format => {/regex/}, :message => :name_format
end

config/locales/en.yml :

en:
  activerecord:
    errors:
      models:
        user:
          attributes:
            username:
              name_format: 'has the way-wrong format, bro!'

, UserSubClass, :

activerecord.errors.models.user_sublcass.attributes.username.name_format # fail
activerecord.errors.models.user_sublcass.name_format                     # fail
activerecord.errors.models.user.attributes.username.name_format          # success
activerecord.errors.models.user.name_format
# ...

, yaml , , , , , , , .

+2

All Articles