Ruby on rails 在Rails 4中对视图和表单中的字段使用相同的转换

Ruby on rails 在Rails 4中对视图和表单中的字段使用相同的转换,ruby-on-rails,rails-i18n,Ruby On Rails,Rails I18n,我想对Rails 4中的视图和表单中的字段使用相同的翻译,因此我阅读了文档()并经历了一些尝试,但失败了,然后我找到了这个解决方案: config/locales/en.yml: en: hello: "Hello world" save: "Save" customers: description: "Company full legal name" en: helpers: label: customer: descripti

我想对Rails 4中的视图和表单中的字段使用相同的翻译,因此我阅读了文档()并经历了一些尝试,但失败了,然后我找到了这个解决方案:

config/locales/en.yml:

en:
  hello: "Hello world"
  save: "Save"
  customers:
      description: "Company full legal name"
en:
  helpers:
    label:
      customer:
        description: "Company full legal name"
app/views/customers/_form.html.erb:

...
    <div class="field">
      <%= f.label :description, (I18n.t :description, scope: :customers) %>
      <%= f.text_field :description %>
    </div>
...
...
<p>
  <strong><%=t :description, scope: :customers %></strong>
  <%= @customer.description %>
</p>
...
<div class="field">
  <%= f.label :description %>
  <%= f.text_field :description %>
</div>
在app/views/customers/_form.html.erb中:

...
    <div class="field">
      <%= f.label :description, (I18n.t :description, scope: :customers) %>
      <%= f.text_field :description %>
    </div>
...
...
<p>
  <strong><%=t :description, scope: :customers %></strong>
  <%= @customer.description %>
</p>
...
<div class="field">
  <%= f.label :description %>
  <%= f.text_field :description %>
</div>

但无法为show视图获取该标签


感觉好像我没有从文档中得到翻译系统基础的东西,有人能给我指出正确的方向吗?谢谢

使用i18n需要练习和对运行时上下文的高度关注。

我建议(暂时)安装一个名为gem的gem,它是一个命令行工具,可以帮助您理解YAML中应用程序本地化字符串的必要结构


一旦您知道i18n在哪里查找字符串,根据上下文,您就可以在YAML中将字符串按正确的顺序放入正确的结构中。

这是什么意思?“但是没有办法为show view获取该标签。”您观察到的错误或问题是什么?我不知道如何获取帮助器描述使用i18n需要练习,并且非常关注运行时上下文。我建议(暂时)安装一个名为i18n tasks的gem——它是一个命令行工具,可以帮助您理解YAML中应用程序本地化字符串的必要结构。一旦你知道i18n在哪里寻找字符串,根据上下文,你就可以在YAML中以正确的顺序将字符串放入正确的结构中。谢谢你,Val,这个gem非常有用,你/我们非常欢迎。我很高兴这有帮助。这应该是一个答案吗?如果其他人搜索并找到这个问题,他们更有可能找到gem信息?