继承Rails i18n子类中的验证错误消息 [英] Inheriting Rails i18n validation error messages in the subclass
问题描述
我的理解
假设我的课程有一个方便的验证,如:
用户< ActiveRecord :: Base
验证:username,:format => {/ regex /},:message => :name_format
end
在这种情况下,我可以使用 i18n
通过在我的 /config/locals/en.yml
中包含以下内容来使错误消息可翻译:
zh:
activerecord:
错误:
型号:
用户:
属性:
用户名:
name_format:'格式错误,兄弟!'
这很好,通常非常方便。
我想知道的是什么:
我的问题是:会发生什么当我有从User继承的子类时:
UserSubclassOne<用户
#额外的东西
结束
UserSubclassTwo<用户
#额外的东西
结束
...
UserSubclassEnn<用户
#额外的东西
结束
现在问题是Rails可以' 。吨找到翻译 user_subclass_one.attributes.username.name_format
有抱怨:
'pre>
翻译丢失:
en.activerecord.errors.models.user_subclass_one.attributes.username.name_format
我希望Rails会将 UserSubclassOne
的层次结构查找到用户
在 en.yml
中搜索字符串,然后注意它何时被点击,但(除非我做了什么可怕的错误)显然这不会发生。
一个明显的解决方案是复制 en.yml.en.errors.models
中的数据用户
, user_subclass_one
, user_subclass_two
等,但我的Rails的常识告诉我这是非常错误的。
任何想法,伙计们?
潜在并发症:
用户
在Rails引擎中包含的gem MyGem
中定义 MyEngine
被包括在全on Rails应用程序 MyApp的
定义 UserSubclassOne
,..., UserSubclassEnn
。我不认为这应该重要,因为验证是在 MyGem :: User
中运行的,这是 en.yml
文件存在 - 只是想让人们知道以防万一。
终极问题/解决方案:
事实证明问题是命名空间。回想一下 MyApp
(定义 UserSubclassOne
)使用 MyGem
(它定义了用户
)。事实证明用户
实际上在命名空间 MyGem
(这不一定总是如此),所以完整的声明用户
开头的行不是:
用户< ActiveRecord :: Base
而是
MyGem :: User< ActiveRecord :: Base
。
i18n gem查找类层次结构,它注意到这个命名空间并搜索 my_gem / user
,而不是简单地 user
, my_gem.user
, my_gem:user
等。
<因此,我必须将
en.yml
文件更改为: /config/locals/en.yml
: zh:
activerecord:
错误:
型号:
my_gem / user:
属性:
用户名:
name_format:'格式错误,兄弟!'
和bingo!
所以事实证明问题是命名空间。回想一下 MyApp
(定义 UserSubclassOne
)使用 MyGem
(它定义了用户
)。事实证明用户
实际上在命名空间 MyGem
(这不一定总是如此),所以完整的声明用户
开头的行不是:
用户< ActiveRecord :: Base
而是
MyGem :: User< ActiveRecord :: Base
。
i18n gem查找类层次结构,它注意到这个命名空间并搜索 my_gem / user
,而不是简单地 user
, my_gem.user
, my_gem:user
等。
<因此,我必须将
en.yml
文件更改为: /config/locals/en.yml
: zh:
activerecord:
错误:
型号:
my_gem / user:
属性:
用户名:
name_format:'格式错误,兄弟!'
和bingo!
What I understand
Suppose I have a class with a handy validation like:
User < ActiveRecord::Base
validates :username, :format => {/regex/}, :message => :name_format
end
In this case, I can use i18n
to make the error message translatable, by including the following in my /config/locals/en.yml
:
en:
activerecord:
errors:
models:
user:
attributes:
username:
name_format: 'has the way-wrong format, bro!'
This is fine and generally really handy.
What I want to know:
My question is: What happens when I have subclasses that inherit from User:
UserSubclassOne < User
# extra stuff
end
UserSubclassTwo < User
# extra stuff
end
...
UserSubclassEnn < User
# extra stuff
end
Now the problem is that Rails can't find the translation user_subclass_one.attributes.username.name_format
.
It complains:
translation missing:
en.activerecord.errors.models.user_subclass_one.attributes.username.name_format
I'd hope that Rails would look up the hierarchy of UserSubclassOne
to User
when searching for a string in en.yml
and then notice when it gets a 'hit', but (unless I've done something horribly wrong) apparently that doesn't happen.
An obvious solution is to duplicate the data in en.yml.en.errors.models
for user
, user_subclass_one
, user_subclass_two
, etc, but my Rails-sense tells me that this is deeply wrong.
Any ideas, folks?
Potential Complication:
User
is defined in a gem MyGem
that is included in a Rails engine MyEngine
that is included in the full-on Rails app MyApp
that defines UserSubclassOne
, ..., UserSubclassEnn
. I don't think this should matter though, since the validations are running in MyGem::User
, which is where the en.yml
file lives -- just wanted to let people know in case it does.
Ultimate problem/solution:
So it turns out that the problem was namespacing. Recall that MyApp
(which defines UserSubclassOne
) uses MyGem
(which defines User
). It turns out User
is actually in the namespace MyGem
(this is not necessarily always the case), so the full declaration line at the beginning of User
is not:
User < ActiveRecord::Base
but rather
MyGem::User < ActiveRecord::Base
.
When the i18n gem looks up the class hierarchy, it notices this namespace and searches for my_gem/user
, rather than simply user
, my_gem.user
, my_gem: user
, etc.
Thus I had to change my en.yml
file to:
/config/locals/en.yml
:
en:
activerecord:
errors:
models:
my_gem/user:
attributes:
username:
name_format: 'has the way-wrong format, bro!'
and bingo!
So it turns out that the problem was namespacing. Recall that MyApp
(which defines UserSubclassOne
) uses MyGem
(which defines User
). It turns out User
is actually in the namespace MyGem
(this is not necessarily always the case), so the full declaration line at the beginning of User
is not:
User < ActiveRecord::Base
but rather
MyGem::User < ActiveRecord::Base
.
When the i18n gem looks up the class hierarchy, it notices this namespace and searches for my_gem/user
, rather than simply user
, my_gem.user
, my_gem: user
, etc.
Thus I had to change my en.yml
file to:
/config/locals/en.yml
:
en:
activerecord:
errors:
models:
my_gem/user:
attributes:
username:
name_format: 'has the way-wrong format, bro!'
and bingo!
这篇关于继承Rails i18n子类中的验证错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!