4
votes

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

2
did you change the locale in your app's configuration file? - apneadiving
Yes and I have more translations in that yml and I can see that those are working. Here's the entire file:pastebin.com/7qaZaE1z - Baversjo
I'm sure it's this particular file that's being used because when I change translations they're reflected on pages (error messages and such) - Baversjo

2 Answers

7
votes

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).

1
votes

I realized that I had the activerecord entry defined two times in my sv.yml translation file which ment my attributes were overridden at the top of the file. It works now.