Forked from tomash/migrate_model_translations_from_globalize1.rb
Created
February 18, 2013 16:17
-
-
Save crohr/4978530 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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