Ruby-on-rails 表单标签的 Rails i18n 和 yml 结构

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3516478/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:04:07  来源:igfitidea点击:

Rails i18n and yml structure for form labels

ruby-on-railsinternationalizationyaml

提问by rhardih

According to the ActionView documentation. Quote:

根据ActionView 文档。引用:

The text of label will default to the attribute name unless a translation is found in the current I18n locale (through views.labels.<modelname>.<attribute>) or you specify it explicitly.

标签文本将默认为属性名称,除非在当前 I18n 语言环境中找到翻译(通过 views.labels.<modelname>.<attribute>)或者您明确指定它。

I have a "user" model and a registration form. Here's a snippet of the relevant part:

我有一个“用户”模型和一个注册表单。这是相关部分的片段:

<% form_for(@user) do |f| %>
    ...
    <p>
    <%= f.label :username %>
    <%= f.text_field :username, :class => 'full_width' %>
    </p>
    ...
<% end %>

Dots hide unimportant code.

点隐藏了不重要的代码。

As I understand the documentation, if I provide a translation in my locale file, in this case :dk, my dk.yml looking like so:

据我了解文档,如果我在我的语言环境文件中提供翻译,在这种情况下:dk,我的 dk.yml 看起来像这样:

dk:
    views:
        labels:
            user:
                username:
                    "blahblah"

Rails should translate the label text and insert "blahblah" instead of "Username".

Rails 应该翻译标签文本并插入“blahblah”而不是“用户名”。

This is not happening, so I must have missed something. Any help appreciated.

这不会发生,所以我一定错过了什么。任何帮助表示赞赏。

回答by Voldy

In Rails 3.1 that is a little bit changed.

在 Rails 3.1 中,这有点改变。

<% form_for @post do |f| %>
  <%= f.label :title %>
  <%= f.text_field :title %>
  <%= f.submit %>
<% end %>

en:
  helpers:
    label:
      post:
        title: 'Customized title'

回答by rhardih

I think I found another solution here.

我想我在这里找到了另一个解决方案。

My app was version 2.3.5. I've now changed it to 2.3.8 and <%= f.label :username %>now uses the translation in:

我的应用程序是 2.3.5 版。我现在已将其更改为 2.3.8,<%= f.label :username %>现在使用以下翻译:

dk:
  activerecord:
    attributes:
      user:
        username:

I found the hint in this ticket:

我在这张票中找到了提示:

https://rails.lighthouseapp.com/projects/8994/tickets/745-form-label-should-use-i18n

https://rails.lighthouseapp.com/projects/8994/tickets/745-form-label-should-use-i18n

回答by Nicolas Buduroi

That's because the labelmethod you are calling is not the one from ActionView::Helpers::FormHelperbut is in fact the label_tagmethod from ActionView::Helpers::FormTagHelper. The form_formethod is rewriting the code in the given block by adding _tagto the used form helpers. So you're not looking at the documentation for the right method!

那是因为label您调用的方法不是 fromActionView::Helpers::FormHelperlabel_tag方法,但实际上是from的方法ActionView::Helpers::FormTagHelper。该form_for方法通过添加_tag到使用的表单助手来重写给定块中的代码。所以你不是在看正确方法的文档!

I've not yet used that method, as sometimes the label for a field can be different from multiple forms using the same model, so I've written my own helper.

我还没有使用过这种方法,因为有时一个字段的标签可能与使用相同模型的多个表单不同,所以我编写了自己的助手。