App cannot access translation files in engine or gem

不问归期 提交于 2020-01-02 03:41:04

问题


I tried every combination I can think of but I cannot get my app to see the localized content provided by my engine. Now the engine does just fine.

I see the same problem with Rails_admin. Where it's i18n files are in a separate gem. The main app cannot seem to see the files. I'm sure there must be an error in how I'm specifying the I18n.load_path, but it's got me beat.

from Ryan Bates rails cast:

I18n.load_path += Dir[Rails.root.join('config', 'locale', '*.{rb,yml}')]

And one of my hack attempts:

I18n.load_path += Dir[Rails.root.join('**','locales', '**', '*.{rb,yml}')]

And any reference from inside the app results in a translation not found.

Any clues.


回答1:


I was facing same issue , If your developing rails engine then add following lines to lib/engine_name/engine.rb

module MyEngine
  class MyEngine < Rails::Engine
    config.before_initialize do                                                      
      config.i18n.load_path += Dir["#{config.root}/config/locales/**/*.yml"]
    end
  end
end

Another way

module MyEngine
  class MyEngine < Rails::Engine
    initializer 'MyEngine', before: :load_config_initializers do
      Rails.application.config.i18n.load_path += Dir["#{config.root}/config/locales/**/*.yml"]        
    end
  end
end


来源:https://stackoverflow.com/questions/23986512/app-cannot-access-translation-files-in-engine-or-gem

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