I got a question about Rails engine, which is not mentioned under Rails guide about Rails engine. I hope to get it here.
I got one engine, for example, called my_engine, and one app, called my_app.
for development purpose, in my_app's Gemfile, I just simple include my_engine with following line, using :file key.
#my_app/Gemfile
...
gem "my_engine", :path => "./../my_engine"
...
and my_engine structure is like this:
.
├── 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
And I found that while I try to inspect I18n.load_path under my_app, there's no any single path point to my_engine, which means, my_app doesn't load my_engine's locale transactions.
>>rails console
Loading development environment (Rails 4.0.2)
2.1.0 :001 >I18n.load_path.each { |x| puts x }
Is it I missing some config or some important steps about loading locales in my_engine?
The Ruby I18n (shorthand for internationalization) gem which is shipped with Ruby on Rails (starting from Rails 2.2) provides an easy-to-use and extensible framework for translating your application to a single custom language other than English or for providing multi-language support in your application.
1 What are Engines? Engines can be considered miniature applications that provide functionality to their host applications. A Rails application is actually just a "supercharged" engine, with the Rails::Application class inheriting a lot of its behavior from Rails::Engine .
Mount within the Rails routes does the equivalent of a Unix mount . It actually tells the app that another application (usually a Rack application) exists on that location. It is used mostly for Rails Engines.
I18n::InvalidLocale: :en is not a valid locale. Bookmark this question.
I'm using
Rails 4.1.6
and I mounted my engine like this inside Gemfile
gem 'core', path: "core"
when I check my load paths
I18n.load_path.find_all { |p| p.match("core") }.each { |p| puts p }
I see locales included from my engine
/absolute_path/core/config/locales/de.yml
/absolute_path/core/config/locales/en.yml
so as default rails is loading your engine locales
it's also mentioned in documentation
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/.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With