【问题标题】:Using i18n with Bootstrap-Rails and Simple_Form将 i18n 与 Bootstrap-Rails 和 Simple_Form 一起使用
【发布时间】:2014-10-03 19:18:09
【问题描述】:

我有一个名为 Variable 的模型,其名称和描述为变量。其索引视图如下:

<%- model_class = Variable -%>
<div class="page-header">
  <h1><%=t '.title', :default => model_class.model_name.human.pluralize.titleize %></h1>
</div>
<table class="table table-striped">
  <thead>
    <tr>
      <th><%= model_class.human_attribute_name(:id) %></th>
      <th><%= model_class.human_attribute_name(:name) %></th>
      <th><%= model_class.human_attribute_name(:description) %></th>
      <th><%= model_class.human_attribute_name(:created_at) %></th>
      <th><%=t '.actions', :default => t("helpers.actions") %></th>
    </tr>
  </thead>
  <tbody>
    <% @variables.each do |variable| %>
      <tr>
        <td><%= link_to variable.id, variable_path(variable) %></td>
        <td><%= variable.name %></td>
        <td><%= variable.description %></td>
        <td><%=l variable.created_at %></td>
        <td>
          <%= link_to t('.edit', :default => t("helpers.links.edit")),
                      edit_variable_path(variable), :class => 'btn btn-default btn-xs' %>
          <%= link_to t('.destroy', :default => t("helpers.links.destroy")),
                      variable_path(variable),
                      :method => :delete,
                      :data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) },
                      :class => 'btn btn-xs btn-danger' %>
        </td>
      </tr>
    <% end %>
  </tbody>
</table>

<%= link_to t('.new', :default => t("helpers.links.new")),
            new_variable_path,
            :class => 'btn btn-primary' %>

所以,它是一个标准的脚手架 + Twitter_Bootstrap,由 Simple_form 代码生成。

我想把它翻译成 pt-BR 和 es,但我真的不明白怎么做,因为它使用了 model_class.human_attribute_name(:name)。 simple_form 网站说(假设我要翻译成 en):

en:
  simple_form:
    options:
      user:
        gender:
          male: 'Male'
          female: 'Female

当我将“选项”更改为“属性”,将“用户”更改为“变量”等等时,这似乎不起作用......说实话,我什至不知道是否应该放置翻译在 simple_form.pt_BR.yml 或新的 pt-BR 文件中。

任何帮助,在类似情况下使用的 i18n 示例将不胜感激!

谢谢!

【问题讨论】:

    标签: ruby-on-rails twitter-bootstrap internationalization simple-form rails-i18n


    【解决方案1】:

    您可以将一个文件用于所有翻译,也可以拆分为多个文件,没关系。

    我可以建议您为自己的翻译准备一个语言环境文件,并且不要修改 simple_form(如果您使用其他 gem,则不要修改其他语言环境文件)。这样,如果您更新 simple_form gem,您可以使用新的翻译文件而不会丢失您自己的翻译。

    您的语言环境文件应如下所示:

    es:
      simple_form:
        labels:
          variable:
            name: "Nombre"
            description: "Descripción"
    

    一些精度:

    • 模型名称为单数形式
    • 您应该使用“标签”而不是“选项”。 'Options' 用于翻译集合助手中的选项

    希望对您有所帮助。

    【讨论】:

    • 我无法使用 simple_form 的标签,但我按照您的建议创建了另一个语言环境文件。以下代码创建了我需要的默认值:`pt-BR: attributes: name: "Nome" description: "Descrição" id: "Código" ``
    猜你喜欢
    • 2013-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-04
    • 1970-01-01
    • 2013-09-26
    • 1970-01-01
    相关资源
    最近更新 更多