Rails遗留应用程序和Ruby 2错误:无法加载文件类型yml的翻译未知

时间:2014-07-30 10:59:10

标签: ruby-on-rails-2 ruby-2.1

我有一个遗留的Rails应用程序,我想升级到最近的Rails和Ruby版本。首先,我尝试使用Ruby 2.1.2设置应用程序

$ rails -v
Rails 2.3.18

$ ruby -v
ruby 2.1.2p95 (2014-05-08 revision 45877) [i686-linux]

当我尝试运行rake任务rake db:schema:load RAILS_ENV=test时,我遇到了以下错误

 can not load translations from /activesupport-2.3.18/lib/active_support/locale/en.yml, the file type yml is not known

通过Google搜索我发现以下引用https://github.com/rails/rails/issues/10514,其中提到Rails 2.3和Ruby 2+版本之间存在不兼容性。

有人可以帮我申请参考链接中提到的猴子补丁吗?

谢谢, Jignesh

1 个答案:

答案 0 :(得分:10)

最后解决了错误

 can not load translations from /activesupport-2.3.18/lib/active_support/locale/en.yml, the file type yml is not known

通过猴子修补Rails的I18n :: Backend :: Base#load_file(filename)方法。

解决方案如下:

1.1在/ config / initializers

创建名为ruby2.rb的文件

1.2将以下内容添加到/config/initializers/ruby2.rb

  if Rails::VERSION::MAJOR == 2 && RUBY_VERSION >= '2.0.0'
    module I18n
      module Backend
        module Base
          def load_file(filename)
            type = File.extname(filename).tr('.', '').downcase
            # As a fix added second argument as true to respond_to? method
            raise UnknownFileType.new(type, filename) unless respond_to?(:"load_#{type}", true)
            data = send(:"load_#{type}", filename) # TODO raise a meaningful exception if this does not yield a Hash
            data.each { |locale, d| store_translations(locale, d) }
          end
        end
      end
    end
  end

1.3终于跑了

   $ rake db:schema:load RAILS_ENV=test

并且架构已成功加载。

我能找到最有帮助的参考资料,并帮助我找到解决方案:

  1. https://github.com/rails/rails/issues/10514
  2. https://www.lucascaton.com.br/2014/02/28/have-a-rails-2-app-you-can-run-it-on-the-newest-ruby/