8
votes

I am using I18n-js, and my client side I18n.t calls all return a translation missing message when running in production.

All is ok in development and test.

The root of this issue appears to be in the asset pipeline.

I18n.load_path does not contain any of my translations (when running bin/rake assets:precompile) it only contains the following paths:

["/home/chris/.rvm/gems/ruby-1.9.3-p125@Project/gems/activesupport-3.2.3/lib/active_support/locale/en.yml",
"/home/chris/.rvm/gems/ruby-1.9.3-p125@Project/gems/activemodel-3.2.3/lib/active_model/locale/en.yml",
"/home/chris/.rvm/gems/ruby-1.9.3-p125@Project/gems/activerecord-3.2.3/lib/active_record/locale/en.yml",
"/home/chris/.rvm/gems/ruby-1.9.3-p125@Project/gems/actionpack-3.2.3/lib/action_view/locale/en.yml"]

These look like the default activesupport, activemodel, activerecord and actionpack translations from the gems...

My translation paths do however get set as expected when running bin/rails console in development and production:

1.9.3p125 :002 > I18n.load_path
=> ["/home/chris/.rvm/gems/ruby-1.9.3-p125@Project/gems/activesupport-3.2.3/lib/active_support/locale/en.yml",
"/home/chris/.rvm/gems/ruby-1.9.3-p125@Project/gems/activemodel-3.2.3/lib/active_model/locale/en.yml",
"/home/chris/.rvm/gems/ruby-1.9.3-p125@Project/gems/activerecord-3.2.3/lib/active_record/locale/en.yml",
"/home/chris/.rvm/gems/ruby-1.9.3-p125@Project/gems/actionpack-3.2.3/lib/action_view/locale/en.yml",
"/home/chris/.rvm/gems/ruby-1.9.3-p125@Project/gems/carrierwave-0.6.1/lib/carrierwave/validations/../locale/en.yml",
"/home/chris/.rvm/gems/ruby-1.9.3-p125@Project/gems/devise-2.0.4/config/locales/en.yml",
"/media/sf_code/Project/config/locales/active_record.en.yml",
"/media/sf_code/Project/config/locales/project.en.yml"]

Indeed the I18n documentation states: "The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded."

I've also tried specifying in application.rb

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

But still no joy.

Anyone have any idea what could cause I18n.load_path not to be set only when running assets:precompile?

Thanks for any ideas

3

3 Answers

3
votes

This is actually due to a asset configuration flag that you probably have set in your config/application.rb.

config.assets.initialize_on_precompile = false

The rake assets:precompile rake task checks for this flag, and if found to be false, only loads the assets group and does not fully initialize the application. In turn, application locales are not added to the I18n.load_path.

0
votes

Did you define a default locale ? If not, you can do it adding this line in your config/application.rb

config.i18n.default_locale = :fr

Maybe you should add this line too in your production.rb in order to enable locale fallbacks for I18n :

config.i18n.fallbacks = true

Then, be carful that you have no tab indentation in your locale file but only spaces indentation.

Locale is not an assets, assets are only js, css and images so there is no link between assets compilation and locales.

0
votes

This is because the i18n.js is not compiled and processed under public/assets to do so you have to add the following line in config/environments/production.rb

config.assets.precompile += %w( i18n.js en_locale.js fr_locale.js )

Then run rake assets:precompile you will notice that public/assets/i18n-MD5.js now exists and ready to be served.