Create a gist now

Instantly share code, notes, and snippets.

Script to migrate from Globalize1 to Globalize2 model translations
require 'config/environment.rb'
class GlobalizeCountry < ActiveRecord::Base
end
class GlobalizeLanguage < ActiveRecord::Base
end
class GlobalizeTranslation < ActiveRecord::Base
belongs_to :language, :class_name => "GlobalizeLanguage"
end
class ModelTranslation < GlobalizeTranslation
end
class ViewTranslation < GlobalizeTranslation
end
translated = {"Category" => ["name","description"],
"Product" => ["title","description"],
"News" => ["title", "content"]}
#first the default:
def_lang = GlobalizeLanguage.find_by_iso_639_1(I18n.default_locale.to_s)
translated.each do |classname, fields|
puts "Migrating #{classname} for language #{def_lang.english_name}"
klass = Kernel.const_get(classname)
klass.find(:all).each do |instance|
fields.each do |field|
assign_method = (field + "=").to_sym
instance.send(assign_method, instance.attributes[field]) if instance.send(field.to_sym).nil?
end
instance.save
end
end
#model translations based on globalize translations
#how many different languages are really there (in model translations)?
glob_lang_ids = ModelTranslation.find(:all, :group => "language_id").collect{|mt| mt.language_id}
glob_lang_ids.each do |glob_lang_id|
glob_lang = GlobalizeLanguage.find(glob_lang_id)
I18n.locale = glob_lang.iso_639_1.to_sym
translated.each do |classname, fields|
klass = Kernel.const_get(classname)
puts "Migrating #{classname} with tablename #{klass.table_name} for language #{glob_lang.english_name}"
puts "Locale for migration: #{I18n.locale.to_s}"
klass.find(:all).each do |instance|
fields.each do |field|
mt = ModelTranslation.find_by_language_id_and_table_name_and_item_id_and_facet(glob_lang_id, klass.table_name, instance.id, field)
next if mt.nil?
assign_method = (field + "=").to_sym
instance.send(assign_method, mt.text)
end
instance.save
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment