ActiveRecord翻译嵌套属性

时间:2018-01-16 18:33:30

标签: ruby-on-rails ruby activerecord internationalization

我有一个看起来像这样的模型:

class Search
  validate :customers_are_of_age

  def customers
    [customer1, customer2]
  end

  def customers_are_of_age
    unless customers.all?(&:of_age)
      errors.add(:customers, :not_of_age)
    end
  end
end

这是一个人为的例子,但突出了我所面临的问题。现在我的翻译文件如下:

en:
  activemodel:
    errors:
      models: 
        search: 
          customers:
            not_of_age: "not of age"
de:
  activemodel:
    errors:
      models: 
        search: 
          customers:
            not_of_age: "nicht im Alter"

这个工作大多没问题,因为如果我有一个未通过验证的搜索对象,我会收到消息"不是年龄的客户"这就是我想要的。但是在德语中,我收到了消息"客户nicht im Alter"。

我如何翻译"客户"键?我尝试过以下无效:

activemodel:
    errors:
      attributes:
        search/customers: "Customers"
      models: 
        search: 
          customers:
            not_of_age: "not of age"

我也尝试了这个,但它也不起作用:

activemodel:
      models: 
        search: 
          customers:
            one: "Customer"
            many: "Customers"
            not_of_age: "not of age"

1 个答案:

答案 0 :(得分:0)

您看到“Customers nicht im Alter”的原因是因为未翻译模型名称(对于客户模型)。您只为错误添加了翻译。 您可以在此处找到更多信息:http://guides.rubyonrails.org/i18n.html#translations-for-active-record-models

<div class="upload_container">
            <h1>- UPLOAD A FILE -</h1>
            <p>  Here you can upload files that you want to share </p>
        <%= form_for :upload, url: upload_uploadFile_path do |f| %>
            <%= f.file_field :file, :class=> "btn-uploadoldschool" %>
            <br/>
            <br/>
            <br/>
            <%= f.submit :UPLOAD!, :class=> "btn-uploadoldschool" %>
        <% end %>
        <% if user_signed_in? %>
            <% if current_user.has_role? :admin %>
                <%= link_to upload_admin_url do %>  

                <% end %>
            <% end %>
        <% end %>        

</div>