I18n: How to load default :en locales instead of "translation missing" errors?
Translation missing...
Default language set in Rails app is English (hence :en locales). We can change the language in file config/application.rb
:
config.i18n.default_locale = :en
Changing the language we have to translate all previous strings. Otherwise, we will see translation missing: bla bla bla...
How to avoid it and replace ugly errors with default :en strings?
Solution
Create a new file i18n_missing_translations.rb
and place there the following code:
i18n_simple_backend = I18n::Backend::Simple.new
old_handler = I18n.exception_handler
I18n.exception_handler = lambda do |exception, locale, key, options|
case exception
when I18n::MissingTranslation
i18n_simple_backend.translate(:en, key, options || {})
else
old_handler.call(exception, locale, key, options)
end
end
Put your file in config/initializers
folder and restart Rails server.
Written by Sebastian Muszyński
Related protips
2 Responses
This was perfect. Thanks!
over 1 year ago
·
That code is not necessary any more
config.i18n.fallbacks = true
config.i18n.fallbacks = %i[en]
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Rails
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#