上下文:对于一个自行车租赁的Ruby on Rails应用程序,我正在使用gem globalize来处理不同语言中的输入:description。
当前状态:全球化实现起作用了,根据我的语言环境,我能够以特定语言存储:description。:description的输入是基于整个网页的语言环境进行处理的。这意味着为了以正确的语言存储:description,必须更改页面上的所有内容的语言。
或者,我也可以显示所有可用的语言环境,并为每个语言环境显示描述(请参见下面注释掉的代码)。
问题:我正在寻找一种让用户仅选择:description的语言并将其保存在正确的语言中而无需更改整个网页语言的方法。
代码
当前状态:全球化实现起作用了,根据我的语言环境,我能够以特定语言存储:description。:description的输入是基于整个网页的语言环境进行处理的。这意味着为了以正确的语言存储:description,必须更改页面上的所有内容的语言。
或者,我也可以显示所有可用的语言环境,并为每个语言环境显示描述(请参见下面注释掉的代码)。
问题:我正在寻找一种让用户仅选择:description的语言并将其保存在正确的语言中而无需更改整个网页语言的方法。
代码
<div class="row">
<%# I18n.available_locales.each do |locale| %>
<!-- <h1><%#= locale %></h1> -->
<%= f.globalize_fields_for locale do |ff| %>
<div class="col-10">
<div class="form-group">
<label class="form-control-label text required" for="accommodation_category_description">Description</label>
<div><%= ff.text_area :description, :rows =>"5", :cols =>"30", class:"form-control is-valid text required" %></div>
</div>
</div>
<% end %>
<%# end %>
</div>
</div>
initializers/globalization.rb
module ActionView
module Helpers
class FormBuilder
#
# Helper that renders translations fields
# on a per-locale basis, so you can use them separately
# in the same form and still saving them all at once
# in the same request.
def globalize_fields_for(locale, *args, &proc)
raise ArgumentError, "Missing block" unless block_given?
@index = @index ? @index + 1 : 1
object_name = "#{@object_name}[translations_attributes][#{@index}]"
object = @object.translations.find_by_locale locale.to_s
@template.concat @template.hidden_field_tag("#{object_name}[id]", object ? object.id : "")
@template.concat @template.hidden_field_tag("#{object_name}[locale]", locale)
@template.fields_for(object_name, object, *args, &proc)
end
end
end
end