【问题标题】:Rails - Translate model name in form actionsRails - 在表单操作中翻译模型名称
【发布时间】:2012-10-25 06:27:56
【问题描述】:

我想用 i18n 系统翻译一个 rails 表单。

我的模型属性翻译正确,但是当我想翻译提交动作时,模型名称没有翻译。

这是我的 fr.yml 语言环境文件

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

我的 _form.html.erb 是

<%= 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 %>

在我的表单上,我希望更新按钮是“Mise à jour Personnage”,但我仍然有“Mise à jour Character”。

感谢您的帮助!

【问题讨论】:

    标签: ruby-on-rails internationalization translation locale


    【解决方案1】:

    看起来你有一个model,你应该有models

    将您的 fr.yml 更改为如下所示:

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

    有关详细信息,请参阅Translations for Active Record Models 上的 rails i18n 文档部分。

    【讨论】:

    • 就是这样!非常感谢您的帮助!
    • 因为我遇到了这个问题:如果您的模型直接使用ActiveModel 而不是ActiveRecord(可能是因为您使用了数据映射器),请改用activemodel.models.character
    • 对我不起作用,按钮仍标记为(在我的情况下为 pt-br)“Criar 类别”而不是“Criar 类别”。
    【解决方案2】:

    这是在 form_for 标记中使用时提交按钮的默认行为...它将显示更新字符而不是“更新 #{Model.name}”...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-09
      • 1970-01-01
      • 2011-02-25
      • 1970-01-01
      • 2012-07-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多