应用程序无法访问引擎或 gem 中的翻译文件 [英] App cannot access translation files in engine or gem
问题描述
我尝试了所有我能想到的组合,但我无法让我的应用看到我的引擎提供的本地化内容.现在发动机运转良好.
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.
我发现 Rails_admin
也有同样的问题.i18n 文件所在的位置在一个单独的 gem 中.主应用程序似乎无法看到文件.我确定我指定 I18n.load_path
的方式一定有错误,但它让我很满意.
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.
任何线索.
推荐答案
我遇到了同样的问题,如果你正在开发 Rails 引擎,那么在 lib/engine_name/engine.rb 中添加以下几行
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
另一种方式
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
这篇关于应用程序无法访问引擎或 gem 中的翻译文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!