Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails - Translate model name in form actions

I want to translate a rails form using i18n system.

My model attributes are translated correctly, but when I want to translate submit actions, the model name is not translated.

Here is my fr.yml locale file

fr:
  activerecord:
    model:
      character:
        one: "Personnage"
        other: "Personnages"
    attributes:
      character:
        name: "Nom"
        title: "Titre"
        biography: "Biographie"
  helpers:
    submit:
      update: "Mise à jour %{model}"

My _form.html.erb is

<%= form_for(@character) do |f| %>
  <% if @character.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@character.errors.count, "error") %> prohibited this character from being saved:</h2>

      <ul>
      <% @character.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :biography %><br />
    <%= f.text_area :biography, :rows => 8%>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

On my form, I expected the update button to be "Mise à jour Personnage", but I still have "Mise à jour Character".

Thanks for your help !

like image 442
John Smith Avatar asked Nov 05 '12 12:11

John Smith


2 Answers

Looks like you have a model where you should have models.

Change your fr.yml to look like this:

fr:
  activerecord:
    models:
      character:
        one: "Personnage"
        other: "Personnages"
    ...

See the rails i18n documentation section on Translations for Active Record Models for details.

like image 120
Chris Salzberg Avatar answered Oct 28 '22 08:10

Chris Salzberg


This is the default behavior of the submit button when used in the form_for tag...it will show the Update Character instead of "Update #{Model.name}"...

like image 45
Aditya Kapoor Avatar answered Oct 28 '22 07:10

Aditya Kapoor