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



我有一个遗留的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

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

有谁能帮我应用参考链接中提到的猴子补丁吗?

谢谢,Jignesh

终于解决了错误

 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/

最新更新