我有一个关于 Rails 引擎的问题,在关于 Rails 引擎的 Rails 指南中没有提到。我希望在这里得到它。
例如,我有一个名为 my_engine 的引擎和一个名为 my_app 的应用。
出于开发目的,在 my_app 的 Gemfile 中,我使用 :file 键简单地将 my_engine 包含在以下行中。
#my_app/Gemfile
...
gem "my_engine", :path => "./../my_engine"
...
my_engine 结构是这样的:
.
├── Gemfile
├── Gemfile.lock
├── app
│ ├── ...
| |...
|
├── config
│ ├── locales
│ │ └── models
│ │ └── products
│ │ ├── en.yml
│ │ └── zh-TW.yml
│ └── routes.rb
├── lib
│ ├── my_engine
│ │ ├── engine.rb
│ │ └── version.rb
│ ├── my_engine.rb
│ └── tasks
│ └── my_engine_tasks.rake
而且我发现,当我尝试检查 my_app 下的 I18n.load_path 时,没有指向 my_engine 的任何单一路径,这意味着,my_app 不会加载 my_engine 的区域设置事务。
>>rails console
Loading development environment (Rails 4.0.2)
2.1.0 :001 >I18n.load_path.each { |x| puts x }
我是否遗漏了一些配置或有关在 my_engine 中加载语言环境的一些重要步骤?
最佳答案
我正在使用
Rails 4.1.6
然后我像这样在 Gemfile 中安装了我的引擎
gem 'core', path: "core"
当我检查我的加载路径时
I18n.load_path.find_all { |p| p.match("core") }.each { |p| puts p }
我看到我的引擎中包含语言环境
/absolute_path/core/config/locales/de.yml
/absolute_path/core/config/locales/en.yml
因此默认 rails 正在加载您的引擎语言环境
文档中也提到了
http://edgeapi.rubyonrails.org/classes/Rails/Engine.html
Then ensure that this file is loaded at the top of your config/application.rb (or in your Gemfile) and it will automatically load models, controllers and helpers inside app, load routes at config/routes.rb, load locales at config/locales/, and load tasks at lib/tasks/.
关于ruby - Rails 4 引擎是否自动加载引擎的 i18n 语言环境文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22501311/