0

我使用 Formtastic。现在我想为某些字段添加模型翻译。我看着 Globalize2,它似乎是我需要的。但我不知道如何将它与 Formtastic 集成。有人有这样的经历吗?

4

1 回答 1

0

所以这很容易。您可以像没有 Formtastic 一样使用它。

在迁移中:

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

在模型中:

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

一句话:从 Rails 2.3 开始,您可以使用default_scope而不是:joins => :globalize_translations。在 Find 方法和 named_scopes 的早期版本中,您应该编写:

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

鉴于:

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

PS:Globalize2 gem 对我不起作用。所以我不得不使用插件。

于 2009-12-24T09:04:23.773 回答