Search code examples
ruby-on-railsruby-on-rails-3internationalizationglobalize2

Globalize3 multiple locale form


I have a problem regarding submitting multiple locale translations for model data within the same form. For example:

Post model:

translates :title

How can I post in the 'new' action view which contains the form so that I can submit my title in both :en locale and :fr locale for example?

Thanks.


Solution

  • You can take a look at https://github.com/alvarezrilla/batch_translations

    On Rails 3 and globalize3 I had to change a few things for it to work properly. Change all globalize_translations methods to simply translations, remove the proc.binding params to @template.concat and add to your model

    has_many :translations
    accepts_nested_attributes_for :translations
    

    But it's all working.

    Your form should look something like this:

      <%= form_for(@category) do |f| %>
      <%= render "shared/error_messages", :target => @category %>
      <table class="zebra">
        <tbody>
          <%= f.globalize_fields_for(:fr) do |g| %>
            <tr>
              <td><%= t(:language).humanize %>: <%= t(:french).humanize %></td>
              <td></td>
            </tr>
            <tr>
              <td>
                <%= g.label :name, t(:name).humanize %><br/>
                <%= g.text_field :name %>
              </td>
            </tr>
            <tr>
              <td>
                <%= g.label :description, t(:description).humanize %><br/>
                <%= g.text_area :description, :rows => 40, :cols => 100, :class => "mceEditor" %>
              </td>
            </tr>
          <% end %>
          <%= f.globalize_fields_for(:en) do |g| %>
            <tr>
              <td><%= t(:language).humanize %>: <%= t(:english).humanize %></td>
              <td></td>
            </tr>
            <tr>
              <td>
                <%= g.label :name, t(:name).humanize %><br/>
                <%= g.text_field :name %>
              </td>
            </tr>
            <tr>
              <td>
                <%= g.label :description, t(:description).humanize %><br/>
                <%= g.text_area :description, :rows => 40, :cols => 100, :class => "mceEditor" %>
              </td>
            </tr>
          <% end %>
        </tbody>
      </table>
      <div class="actions">
        <%= f.submit :class=>'button' %> <%= yield :actions %>
      </div>
    <% end %>
    

    If you have more problems, I'll commit a working version of this for Rails 3.