使用gem全球化,如何切换语言环境仅输入而不是整个页面?

问题描述 投票:4回答:1

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

ruby-on-rails rails-i18n globalize
1个回答
0
投票

您可以使用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而不是资产管道,则可能需要其他步骤,以使用erb插值添加全局jquery并要求来自gem的js,see more here) 。

在您的模型中:

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>

部分翻译为:

<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>
© www.soinside.com 2019 - 2024. All rights reserved.