Rails legacy app and Ruby 2 error: can not load translations from the file type yml is not known

对着背影说爱祢 提交于 2019-12-05 10:55:59
Jignesh Gohel

Finally resolved the error

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

by monkey-patching the Rails’s I18n::Backend::Base#load_file(filename) method.

The solution is as follows:

1.1 Created a file named ruby2.rb at /config/initializers

1.2 Added following contents to /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 Finally ran

   $ rake db:schema:load RAILS_ENV=test

and the schema was successfully loaded.

Most helpful references I could find and which helped me get to the solution:

  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/
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!