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

tec*_*ion 10 ruby-on-rails rails-i18n globalize

上下文:对于用于自行车租赁的 Ruby on Rails 应用程序,我使用 gem globalize 来处理:description不同语言的输入。

当前状态: globalize 实现有效,这取决于我能够以description特定语言存储的语言环境。输入 for: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>
Run Code Online (Sandbox Code Playgroud)

初始化程序/全球化.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

Run Code Online (Sandbox Code Playgroud)

Vas*_*fed 5

您可以使用Globalize.with_locale临时设置语言环境,这也适用于视图:

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

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

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

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

在您的模型中:

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

在控制器中:

translates :description #, ...
accepts_nested_attributes_for :translations, allow_destroy: true
Run Code Online (Sandbox Code Playgroud)

通知:

  <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>
Run Code Online (Sandbox Code Playgroud)

部分翻译,如:

<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>
Run Code Online (Sandbox Code Playgroud)