Translated attributes in Rails error messages (Rails 2.3.2, I18N)

后端 未结 2 1844
遇见更好的自我
遇见更好的自我 2021-02-06 07:51

I have defined translated attributes and model names in the translation file and Modelname.human_attribute_name(...) returns the correctly translated attribute name, but the att

2条回答
  •  天命终不由人
    2021-02-06 08:10

    From the Guide on the subject, you will need to set up the localization file properly with your model names and attribute names:

    en: 
      activerecord: 
        models: 
          user: Dude 
        attributes: 
          user: 
            login: "Handle"
    

    Since this is YAML, make sure all your "tabs" are actually two soft spaces. Then you can get them out with User.human_name and User.human_attribute_name(:login).

    I didn't take that for gospel -- there could have been a bug. I tested it, and it works fine. I made a model named Model with an attribute title. Here is a snippet of my fr.yml file in config/locales:

    fr:
      activerecord:
        models:
          model: "Sumfink"
        attributes:
          model:
            title: "Tiltile"
    

    Here's the relevant view code:

    New <%= Model.human_name %>

    <% form_for(@model) do |f| %> <%= f.error_messages %> <%= Model.human_attribute_name("title") %>
    <%= f.text_field :title %> <%= f.error_message_on :title %> ...

    And a screenshot of the properly translated output: http://screencast.com/t/et5FhVe1Gp

提交回复
热议问题