Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3.1 translate model attribute name

I'm trying to customize (translate) an active record attribute name in rails 3.1 ("first_name"). Here's beginning of my locale file (config/locales/sv.yml):

"sv":
  activerecord:
    models:
      employee: "Anställd"
    attributes:
      employee:
        first_name: "Förnamn"

I'm sure this file is used by rails because changing translations further down in the file works. Here's the form field erb code that should say "Förnamn" not "First name":

  <div class="field">
    <%= f.label :first_name %><br />
    <%= f.text_field :first_name %>
  </div>

Running Employee.human_attribute_name(:first_name) in rails console returns "First name". Thank you very much

like image 791
Baversjo Avatar asked Feb 24 '23 14:02

Baversjo


1 Answers

In Rails 3.1 you can do it this way as well:

<% form_for @post do |f| %>
  <%= f.label :title %>
  <%= f.text_field :title %>
  <%= f.submit %>
<% end %>

en:
  helpers:
    label:
      post:
        title: 'Customized title'

This approach is ORM agnostic and works fine for active model (with mongoid for instance).

like image 99
Voldy Avatar answered Mar 04 '23 23:03

Voldy