Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Rails 4 engine autoload engine's i18n locales file?

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?

like image 533
Simon Iong Avatar asked Mar 19 '14 09:03

Simon Iong


People also ask

What is I18n Rails?

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.

What is an engine in Rails?

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 .

What is Mount in Rails routes?

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.

Is en a valid locale?

I18n::InvalidLocale: :en is not a valid locale. Bookmark this question.


Video Answer


1 Answers

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/.

like image 166
Lewy Avatar answered Oct 08 '22 21:10

Lewy