更改 en.yml 文件中的属性名称不起作用 [英] Changing attributes name in en.yml file is not working

查看:32
本文介绍了更改 en.yml 文件中的属性名称不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 ruby​​ on rails 项目中更改了 en.yml 文件中的属性名称.按钮工作正常.但是字段属性没有改变.

I changed the attributes name in en.yml file in ruby on rails project. The buttons are working fine. But the field attributes is not changing.

这是我的模型,

class Enr::AffordableWarmth < ActiveRecord::Base
  self.table_name = "AffordableWarmth"
  self.primary_key = 'Record_No'

  validates_presence_of :No_Bedrooms, :No_Bedspaces, :Max_Annual_Energy_Cost
  validates :No_Bedrooms, uniqueness: { scope: :No_Bedspaces, 
    message: "already exists!" }
  validates_numericality_of :No_Bedrooms, :No_Bedspaces, :Max_Annual_Energy_Cost
end

en-GB.yml 文件

en-GB:
  helpers:
    submit:
      enr_rds_dea:
        create: 'Create new user'
        update: 'Update'

      enr_affordable_warmth:
        create: 'Create'
        update: 'Update'

  activerecord:
      models:
        AffordableWarmth:
      attributes:
        AffordableWarmth:
          No_Bedrooms: "Number of Bedrooms"

尽管如此,在 rails 控制台和它显示的表单中,No Bedrooms could not be blank".来自活动记录的代码位不起作用.在一些代码工作正常之前.

Still, in the rails console and in the form it displays 'No Bedrooms could not be blank'. Bit of code is not working from the activerecord. Before bit of code is working fine.

推荐答案

lib/active_model/errors.rb,我觉得关键应该是'enr/affordable_warmth'而不是AffordableWarmth,但是您可能需要以某种方式指定Enr"命名空间.

Looking at lib/active_model/errors.rb, I think the key should be 'enr/affordable_warmth' instead of AffordableWarmth, but you probably need to specify the "Enr" namespace in some way.

如果不行,用bundle show activemodel找到安装好的gem源,然后在lib/active_model/errors.rb中调试一下,准确找出它期望什么 i18n 密钥.

If it doesn't work, locate the installed gem source with bundle show activemodel, then do some debugging in lib/active_model/errors.rb to find out exactly what i18n keys it expects.

更新:

调试方法如下:

我搜索 I18n,它执行翻译,所以我找到方法 generate_message,它生成整个错误消息,并将翻译后的属性名称作为参数(参见 :attribute 键),这将我引向方法 human_attribute_name,并且我在 lib/active_model/translation.rb 中找到了该方法.

I search for I18n, which does the translaton, so I find the method generate_message, which generates the whole error message, and it takes the translated attribute name as a paramater (see :attribute key), which leads me to the method human_attribute_name, and I find that in lib/active_model/translation.rb.

我添加一行:

<代码>puts "human_attribute_name(#{attribute.inspect}, #{options.inspect}): defaults: #{defaults.inspect}"

就在 I18n.translate 之前,然后启动我的控制台.

just before the I18n.translate, and start my console.

对于模型 Foo::User 我得到:

<代码>human_attribute_name("account_type", {}):默认值:[:"activerecord.attributes.foo/user.account_type", :"attributes.account_type", "帐户类型"]

所以这应该适合你:

  activerecord:
      attributes:
        "enr/affordable_warmth":
          no_bedrooms: "Number of Bedrooms"

这篇关于更改 en.yml 文件中的属性名称不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆