【问题标题】:Rails legacy app and Ruby 2 error: can not load translations from the file type yml is not knownRails 遗留应用程序和 Ruby 2 错误:无法从文件类型 yml 加载翻译未知
【发布时间】:2014-09-21 23:49:47
【问题描述】:

我有一个旧版 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+ 版本之间存在不兼容。

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

谢谢, 吉格尼什

【问题讨论】:

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


    【解决方案1】:

    终于解决了这个错误

     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/

    【讨论】:

    • 如果 rake 失败,很可能是由于供应商/插件中有一些遗留插件需要加载 rake 任务。如果您从不使用这些任务,则可以删除它们。否则,请转到您的 rails gem 代码并注释掉检查是否存在已弃用路径的条件。然后你的 rake 任务将运行。
    猜你喜欢
    • 1970-01-01
    • 2022-09-23
    • 2016-04-10
    • 2022-10-23
    • 1970-01-01
    • 2017-10-16
    • 2012-12-03
    • 1970-01-01
    • 2023-02-20
    相关资源
    最近更新 更多