简单表格集合翻译 [英] simple form collection translation

查看:43
本文介绍了简单表格集合翻译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用翻译示例此处翻译:male, :female 选项使用 Ruby I18n,形式为:

Using the translation example here that translates the :male, :female options using Ruby I18n, the form will be:

查看:_form.html.erb

View: _form.html.erb

<%= f.input :gender, collection: [:male, :female] %>

生成的 HTML:

<option>Male</option>
<option>Female</option>

因此,在提交时,表单会使用翻译后的值.

So when submitting, the form uses the translated values.

如何更改视图代码以提交 ["male", "female"] 值而不是 ["Male", "Female"]?

How can I change the view code to submit ["male", "female"] values instead of ["Male", "Female"]?

我目前最好的一次投篮非常难看:

My best shot so far is pretty ugly:

<%= f.input :gender, collection: [:male, :female].map{|v| [t("simple_form.options.user.gender.#{v.to_s}", v] %>

推荐答案

在您看来,您应该使用

<%= f.input :gender, collection: [:male, :female] %> 

在你的 yaml 文件中,你应该有

In your yaml file, you should have

simple_form:
  options:
    user: (or defaults:)
      gender:
        male: Male
        female: Female

这应该自动翻译成

<option value="male">
  Male
</option>

返回给控制器的是值,而不是翻译后的词.

It is the values that will go back to the controller, not the translated words.

如果不是,那就是有问题...

If not, there is something wrong...

这篇关于简单表格集合翻译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆