Skip to content

Instantly share code, notes, and snippets.

@robyurkowski

robyurkowski/step_1.rb Secret

Created Mar 2, 2012
Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@cwise 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