全球化2和迁移

发布于 2024-09-04 16:24:35 字数 369 浏览 5 评论 0原文

我已经使用 globalize2 将 i18n 添加到旧站点。已经有很多西班牙语内容,但是它没有存储在 globalize2 表中。有没有办法通过 Rails 迁移将此内容转换为 Globalize2?

问题是我无法访问存储的内容:

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

有什么想法吗?

I have used globalize2 to add i18n to an old site. There is already a lot of content in spanish, however it isn't stored in globalize2 tables. Is there a way to convert this content to globalize2 with a migration in rails?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜嗑 2024-09-11 16:24:35

我确信您已经以​​某种方式解决了这个问题,但这里是。您应该能够使用 read_attribute 方法来挖掘您要查找的内容。

我只是使用以下命令将内容从主表迁移到 globalize2 翻译表中。

  1. 将适当的 translates 行添加到您的模型中。
  2. 将以下内容放入config/initializers/globalize2_data_migration.rb

    需要“全球化”
    模块全球化
      活动记录模块
        模块迁移
    
          def move_data_to_translation_table
            klass = self.class_name.constantize
            除非 klass.count > 则返回0
            翻译的属性列 = klass.first.translated_attributes.keys
            klass.all.each 做 |p|
              属性 = {}
              翻译的属性列.each { |c| attribs[c] = p.read_attribute(c) }
              p.update_attributes(属性)
            结尾
          结尾
    
          def move_data_to_model_table
            # 查找模型中所有记录的所有翻译属性。
            klass = self.class_name.constantize
            除非 klass.count > 则返回0
            all_translated_attributes = klass.all.collect{|m| m.属性}
            all_translated_attributes.each 执行 |translated_record|
              # 创建一个包含翻译后的列名称及其值的哈希。
              translated_attribute_names.inject(fields_to_update={}) 做 |f, name|
                f.update({name.to_sym =>translated_record[name.to_s]})
              结尾
    
              # 现在,用哈希值更新实际模型的记录。
              klass.update_all(fields_to_update, {:id =>translated_record['id']})
            结尾
          结尾
        结尾
      结尾
    结尾
    
  3. 使用以下内容创建迁移:

    类 TranslateAndMigratePages < ActiveRecord::迁移
      定义自我提升
        Page.create_translation_table!({
          :标题=> :细绳,
          :自定义标题=> :细绳,
          :meta_keywords => :细绳,
          :元描述=> :文本,
          :浏览器标题=> :细绳
        })
    
        say_with_time('将页面数据迁移到翻译表') do
          Page.move_data_to_translation_table
        结尾
      结尾
    
      绝对自我贬低
        say_with_time('将页面翻译值移动到主表中')
          Page.move_data_to_model_table
        结尾
        Page.drop_translation_table!
      结尾
    结尾
    

大量借用 Globalize 3 和 refinerycms。

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.

I just used the following to migrate content from the main table into a globalize2 translations table.

  1. Add the appropriate translates line to your model.
  2. Place the following in 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文