Zach asks:

Any Help?

Jan says:

I use this way: adding collumn

class AddLocaleToProduct < ActiveRecord::Migration
  def self.up
    add_column :products, :locale, :string
  end

  def self.down
    remove_column :products, :locale
  end
end
in product.rb

  def self.find_products_for_sale
    find(:all, :order => "title", :conditions => {:locale => I18n.locale})
  end

    validates_presence_of :title, :description, :locale
    validates_numericality_of :price
adding this to new and edit.html.erb

  <p>
    <%= f.label :locale %><br />
    <%= f.text_field :locale %>
  </p>
to show.html.erb

<p>
  <b>Locale:</b>
  <%=h @product.locale %>
</p>

Jamie says:

Instead of a text field I used a select list like this

adding this to new and edit.html.erb

  <p>
    <%= f.label :locale %><br />
    <%= f.select :locale, LANGUAGES %>
  </p>

Patrick says:

I actually in Rails 2.3.8 had to do this:

  def self.find_products_for_sale
    find(:all, :order => "title", :conditions => {:locale => "#{I18n.locale}"})
  end 
when i did it without #{} it got like this: -:en

Dave says:

I let the select check the current locale in views/product/new.html.erb:


  <p>
    <%= f.label :locale %><br />
    <%= f.select :locale, LANGUAGES, :selected => I18n.locale  %>
  </p>

And instead showing the locale on views/product/index.html.erb for each product I only list the ones that correspond to that locale. In products_controller.rb:


...
  def index
    @products = Product.find(:all, :conditions => {:locale => I18n.locale})
...

And in views/product/index.html.erb:



...
  <h1>Listing products for <%= LANGUAGES.index(I18n.locale) %></h1>
...

Andriy says:

1.adding collumn:

rails generate migration add_title_ua_to_product \ title_es:string

rake db:migrate

rails generate migration add_description_ua_to_product \ description_es:text

rake db:migrate

2. add in views/products/index.html.erb


<td class="list_description" >
      <dl>
       <dt><%= product.title %></dt>
       <dd><%= truncate(strip_tags(product.description), :length => 140) %></dd>
       </dl>
       </td>

       <td class="list_description" > 
      <dl>
       <dt><%= product.title_es %></dt>
       <dd><%= truncate(strip_tags(product.description_es), :length => 140) %></dd>
       </dl>
       </td>    

         <td class="list_price" >
         <dl>

3. add in views/products/_form.html.erb



            <% @product.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>
<table>
  <div class="field">
    <td><dl>
    <%= f.label :title %><br />
    <%= f.text_field :title %>
    </dl></td>
    <td><dl>
    <%= f.label :title_es %><br />
    <%= f.text_field :title_es %>
    </dl></td>
  </div>
  </table>
  <table>
  <div class="field">
    <td><dl>
    <%= f.label :description %><br />
    <%= f.text_area :description %>
    </dl></td>
    <td><dl>
    <%= f.label :description_es %><br />
    <%= f.text_area :description_es %>
    </dl></td>
  </div>
  </table>
  <div class="field">
    <%= f.label :image_url %><br />

3. add in views/store/index.html.erb



  <% if I18n.locale.to_s == 'en' %>
  <h3><%= product.title %></h3>
  <% end %>
  <% if I18n.locale.to_s == 'es' %>
  <h3><%= product.title_ua %></h3>
  <% end %>
  <% if I18n.locale.to_s == 'en' %>
  <%=sanitize product.description %>
 <% end %>
  <% if I18n.locale.to_s == 'es' %>
  <%=sanitize product.description_es %>
  <% end %>
  <div class="price_line" >

Diego says:

I’ve used the tip from and used the following to filter only the products for a certain locale

In products_controller.rb

def index
  @products = Product.where(locale: I18n.locale)                                                                                    
  respond_to do |format|
     format.html # index.html.erb
     format.json { render json: @products }
   end
end

This way you don’t need to add another method in the Product model.

If you want to add the filter in the store index change the Product.order(:title) line to the following


 @products = Product.where(locale: I18n.locale).order(:title)