简体   繁体   English

全球化2和迁移

[英]Globalize2 and migrations

I have used globalize2 to add i18n to an old site. 我已使用globalize2将i18n添加到旧站点。 There is already a lot of content in spanish, however it isn't stored in globalize2 tables. 西班牙文已经有很多内容,但是并未存储在globalize2表中。 Is there a way to convert this content to globalize2 with a migration in rails? 有没有办法通过Rails的迁移将此内容转换为globalize2?

The problem is I can't access the stored content: 问题是我无法访问存储的内容:

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

Any ideas? 有任何想法吗?

I'm sure you solved this one way or another but here goes. 我确定您已经通过一种或另一种方法解决了问题,但是可以解决了。 You should be able to use the read_attribute method to dig out what you're looking for. 您应该能够使用read_attribute方法来挖掘所需的内容。

I just used the following to migrate content from the main table into a globalize2 translations table. 我只是使用以下内容将内容从主表迁移到globalize2 translations表中。

  1. Add the appropriate translates line to your model. 将适当的translates线添加到模型中。
  2. Place the following in config/initializers/globalize2_data_migration.rb : 将以下内容放在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. Created a migration with the following: 使用以下内容创建了迁移:

     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 

Borrows heavily from Globalize 3 and refinerycms. 从Globalize 3和refinerycms大量借用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM