使用globalize宝石,如何仅为输入切换语言环境而不是整个页面?

10
上下文:对于一个自行车租赁的Ruby on Rails应用程序,我正在使用gem globalize来处理不同语言中的输入: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

1个回答

5

你可以使用Globalize.with_locale来临时设置语言环境,它对视图也有效:

<% Globalize.with_locale(some_other_locale) do %>
  in this part of the page locale will be <%= locale.inspect %>
<% end %>

但对于您的情况,更用户友好的方法是使表单动态化,以便用户可以添加他们喜欢的多种语言的翻译。

全球化翻译只是一个附加的表/模型YourModel::Translation,其中包含区域设置和已翻译字段,因此您可以像处理任何其他嵌套表单一样直接使用它们。

gem cocoon添加到您的项目中,这将处理动态表单(如果您正在使用webpacker而不是asset pipeline-可能需要额外的步骤,使用erb插值从gem添加全局jquery和require js,在此处查看更多信息)。

在您的模型中:

translates :description #, ...
accepts_nested_attributes_for :translations, allow_destroy: true

在控制器中:

def your_some_params
  params.require(:your_model_name).permit(
        ...
        translations_attributes: [
          :id, :_destroy,
          :locale,
          :description,
        ]
      )
end

表单中的:

  <div id='translations'>
    <%= form.fields_for :translations do |t| %>
      <%= render 'translation_fields', f: t %>
    <% end %>

    <div class='links'>
      <%= link_to_add_association 'add translation', form, :translations  %>
    </div>
  </div>

针对“partial”的翻译:

<div class='nested-fields'>
  <%= f.hidden_field  :id %>
  <%= f.select :locale, I18n.available_locales %>
  <%= f.text_area :description %>

  <%= link_to_remove_association "remove this translation", f %>
</div>

谢谢Vasfed!! 真的很有用。 - techquestion

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接