0

Я использовал globalize2 для добавления i18n на старый сайт. На испанском языке уже много контента, однако он не хранится в таблицах globalize2. Есть ли способ конвертировать этот контент в globalize2 с миграцией в рельсы?Globalize2 и миграции

Проблема в том, что я не могу получить доступ к сохраненного контента:

>> Panel.first 
=> #<Panel id: 1, name: "RT", description: "asd", proje.... 
>> Panel.first.name 
=> nil 
>> I18n.locale = nil 
=> nil 
>> Panel.first.name 
=> nil 

Любые идеи?

ответ

1

Я уверен, что вы решили это так или иначе, но здесь. Вы должны иметь возможность использовать метод read_attribute, чтобы выкопать то, что вы ищете.

Я просто использовал следующее, чтобы перенести содержимое из основной таблицы в таблицу переводов globalize2.

  1. Добавить подходящую линию translates в свою модель.
  2. Поместите следующий в config/initializers/globalize2_data_migration.rb:

    require 'globalize' 
    module Globalize 
        module ActiveRecord 
        module Migration 
    
         def move_data_to_translation_table 
         klass = self.class_name.constantize 
         return unless klass.count > 0 
         translated_attribute_columns = klass.first.translated_attributes.keys 
         klass.all.each do |p| 
          attribs = {} 
          translated_attribute_columns.each { |c| attribs[c] = p.read_attribute(c) } 
          p.update_attributes(attribs) 
         end 
         end 
    
         def move_data_to_model_table 
         # Find all of the translated attributes for all records in the model. 
         klass = self.class_name.constantize 
         return unless klass.count > 0 
         all_translated_attributes = klass.all.collect{|m| m.attributes} 
         all_translated_attributes.each do |translated_record| 
          # Create a hash containing the translated column names and their values. 
          translated_attribute_names.inject(fields_to_update={}) do |f, name| 
          f.update({name.to_sym => translated_record[name.to_s]}) 
          end 
    
          # Now, update the actual model's record with the hash. 
          klass.update_all(fields_to_update, {:id => translated_record['id']}) 
         end 
         end 
        end 
        end 
    end 
    
  3. Создано миграции со следующим:

    class TranslateAndMigratePages < ActiveRecord::Migration 
        def self.up 
        Page.create_translation_table!({ 
         :title => :string, 
         :custom_title => :string, 
         :meta_keywords => :string, 
         :meta_description => :text, 
         :browser_title => :string 
        }) 
    
        say_with_time('Migrating Page data to translation tables') do 
         Page.move_data_to_translation_table 
        end 
        end 
    
        def self.down 
        say_with_time('Moving Page translated values into main table') do 
         Page.move_data_to_model_table 
        end 
        Page.drop_translation_table! 
        end 
    end 
    

заимствует из Globalize 3 и refinerycms.

+0

Ничего себе, забыл об этом, мы даже перестали использовать глобализацию некоторое время назад! – diegogs