我正在使用Ruby on Rails 3.1,我想知道如何正确处理与“资源资源”相关的国际化。那就是......
...在我的config/routes.rb
文件中:
resources :users do
resource :account
end
...在我的app/models/users/account.rb
文件中:
class Users::Account < ActiveRecord::Base
validates :firstname,
:presence => true
...
end
...在我的config/locales/models/user/account/en.yml
文件中:
en:
activerecord:
errors:
messages:
presence: "custom presence message - english"
...在我的config/locales/models/user/account/it.yml
文件中:
it:
activerecord:
errors:
messages:
presence: "custom presence message - italian"
以上代码未在前端显示“custom presence message
”(它仍会显示默认的RoR状态消息:can not be blank
)。此外,如果在我的app/models/users/account.rb
文件中,我使用:
class Users::Account < ActiveRecord::Base
validates :firstname,
:presence => { :message => t(:presence) } # Here I try to use the i18n helper method
...
end
我收到以下错误:
NoMethodError (undefined method `t' for #<Class:0x000001075bbc80>)
为什么我会获得NoMethodError
?
问题与我在目录中组织我的语言环境文件的方式有关吗?此时(如official RoR guide中所述)我的文件系统是:
config/locales/defaults/en.yml
config/locales/defaults/it.yml
config/locales/models/user/en.yml
config/locales/models/user/it.yml
config/locales/models/user/account/en.yml
config/locales/models/user/account/it.yml
简而言之,我希望仅在验证“{资源资源”类型custom presence message
时显示“Users::Account
”。 我该怎么办?那吗
我还尝试在config/locales/models/user/account/en.yml
文件中说明以下代码
en:
activerecord:
errors:
models:
user:
account:
attributes:
firstname:
blank: "custom presence message - english"
但它不起作用。无论如何,以下工作但我需要不同属性的不同翻译(正如我在前面的代码示例中所述):
en:
activerecord:
errors:
messages:
blank: "custom presence message - english"
答案 0 :(得分:2)
好的,由于这个原因:https://github.com/rails/rails/issues/1402最后评论嵌套模型查找被删除
所以尝试类似
的东西activerecord:
errors:
models:
users:
account:
attributes:
first_name:
blank: "You should fill up first name field to complete that"
并更改内部:消息哈希到
I18n.t(:"activerecord.errors.models.users.account.attributes.first_name.blank")
并尝试避免嵌套模型; - )
更新
经过一些调整后,这将有效: activerecord:
errors:
models:
"users/account":
attributes:
first_name:
blank: "You should fill up first name field to complete that"