user938363
user938363

Reputation: 10350

Rails 4.2 - how to turn off I18n error `translation missing` on views

In our Rails 4.2 app views, there is I18n error whenever there is no key found in zh-CN file:

 translation missing: zh-CN.no key 

Here is in local.rb under config/initializers/:

I18n.default_locale = 'zh-CN' if Rails.env.production? || Rails.env.development?

We would like to turn off this error and prevent it from showing on views. There is a post about the error for Rails 3. However the solutions are not working with Rails 4.2. Also config.i18n.fallbacks = false/true does not do the trick.

Upvotes: 0

Views: 3177

Answers (2)

Yang Hailong
Yang Hailong

Reputation: 316

We can config I18n callbacks in Rails application. For example, when zh-CN translation missing, I18n will fallback to en. Configuration in my Rails 4.2.2 application like below:

config/application.rb

config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}').to_s]
config.i18n.default_locale = :"zh-CN"
config.i18n.fallbacks = true

config/initializers/i18n.rb

Rails.configuration.after_initialize do
  I18n.fallbacks.map(:"zh-CN" => :en)
end

Reference doc:

  1. https://github.com/svenfuchs/i18n/wiki/Fallbacks
  2. http://web.archive.org/web/20151019133539/paulgoscicki.com/archives/2015/02/enabling-i18n-locale-fallbacks-in-rails/

Upvotes: 0

Francesco Belladonna
Francesco Belladonna

Reputation: 11689

You must add both a fallback locale and a fallback language.

config.i18n.default_locale = :en
config.i18n.fallbacks = true

This should force I18n to fallback to english. I use it extensively on a Rails 4.1 app, I'm not sure if something changed over it for 4.2 though.

Upvotes: 4

Related Questions