Skip to content

Instantly share code, notes, and snippets.

@robyurkowski
Created March 2, 2012 16:04
Show Gist options
  • Save robyurkowski/3649d08ab3c84b24ab52 to your computer and use it in GitHub Desktop.
Save robyurkowski/3649d08ab3c84b24ab52 to your computer and use it in GitHub Desktop.
Translating a Refinery 1.0.9 Engine
# Step 1: Migration.
# db/create_<model>_translations
class Create<Model>Translations < ActiveRecord::Migration
def self.up
<Model>.create_translation_table!({
:title => :string,
:body => :text
}, :migrate_data => true)
drop_column :<model>, :title
drop_column :<model>, :body
end
def self.down
add_column :<model>, :title, :string
add_column :<model>, :body, :text
<Model>.drop_translation_table! :migrate_data => true
end
end
# Don't forget to run `rake db:migrate`!
# Step 2: Add required translation stuff inside your model.
# app/models/<model>.rb
class <Model> < ActiveRecord::Base
translates :title, :body
end

Override view=admin/pages/_locale_picker; copy it into app/views/shared/admin/ (may need to mkdir -p here)

# Override view=admin/<model>/_form and edit it. After the following:
<%= form_for [:admin, @<model>], url_opts do |f| %>
<%= render :partial => "/shared/admin/error_messages",
:locals => {
:object => @<model>,
:include_object_name => true
} %>
# Insert...
<%= render :partial => "/shared/admin/locale_picker",
:locals => {
:current_locale => Thread.current[:globalize_locale]
} if ::Refinery.i18n_enabled? %>
# Override view=admin/<model>/_<model> and edit it. Change the top bit to look something like this:
<span class='title'>
<%= <model>.name %>
<% if ::Refinery.i18n_enabled? and ::Refinery::I18n.frontend_locales.many? and (locales = <model>.translations.map(&:locale)).present? %>
<span class='preview'>
<% ([<model>.translation.try(:locale)] | locales).each do |locale| %>
<%= link_to refinery_icon_tag("flags/#{locale}.png", :size => '16x11'), edit_admin_<model>_path(<model>, :switch_locale => locale), :class => 'locale' %>
<% end %>
</span>
<% end %>
</span>
@cwise
Copy link

cwise commented May 8, 2012

Some notes for Refinery 2.0.x

Step 1 - drop_column isn't valid - should be remove_column

Step 2 - model needs:

  class Translation
    attr_accessible :locale
  end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment