views:

348

answers:

1

I use Formtastic. Now I would like to add model translations for some fields. I look at Globalize2 and it seems like what I need. But I have no idea how to integrate it with Formtastic. Does anybody have such experience?

A: 

So it's quite easy. You can use it in the same manner as you don't have a Formtastic.

In migration:

class CreateCategories < ActiveRecord::Migration
  def self.up
    create_table :categories do |t|
      t.timestamps
    end
    Category.create_translation_table! :name => :string
  end
  def self.down
    drop_table :categories
    Category.drop_translation_table!
  end
end

In model:

class Category < ActiveRecord::Base
  attr_accessible :name
  translates :name

  default_scope :include => :globalize_translations

  named_scope :top_categories, {:conditions => {:category_translations => {:locale => I18n.locale}},
                                :order => 'name asc'}
end

One remark: since rails 2.3 you can use default_scope instead of :joins => :globalize_translations. In earlier versions of rails in Find methods and in named_scopes (for example) you should write:

named_scope :top_categories, {:joins => :globalize_translations,
                              :conditions => {:category_translations => {:locale => I18n.locale}},
                              :order => 'name asc'}

In view:

<% semantic_form_for @category do |f| %>
  <% f.inputs do %>
    <%= f.input :locale, :as => :hidden, :value => I18n.locale %>
    <%= f.input :name %>
  <% end %> 
  <%= f.buttons %>
<% end %>

P.S: Globalize2 gem doesn't work for me. So I had to use plugin.

Voldy
**Since Globalize2 version 0.2.0 we can use only:** default_scope :include => :translations
Voldy