How to enable Rails I18n translation errors in views?

I created a new Rails 3 project. I am trying to use translations in my views as follows:

= t('.translate_test') 

In my browser, I look at "translate_test" instead of "my test translation" witch I set in en.yml.

My main question is why I don’t see an error, for example, "Missing translation: en ..." ?

+6
source share
4 answers

In Rails 3, they no longer display this text. If you check the item in the html source, you will see a message with no translation.

You can disable backups, try including the following in your environment or initializer:

 config.i18n.fallbacks = false 
+8
source

I created this initializer to raise exceptions - args are passed so you know which i18n key is missing!

 # only for development and test if Rails.env.development? || Rails.env.test? # raises exception when there is a wrong/no i18n key module I18n class JustRaiseExceptionHandler < ExceptionHandler def call(exception, locale, key, options) if exception.is_a?(MissingTranslationData) raise exception.to_exception else super end end end end I18n.exception_handler = I18n::JustRaiseExceptionHandler.new end 

A source

+10
source

I use the simplest and most concrete solution to display errors in the view when there is no translation by adding this style to your application.css.scss or any global stylesheet:

 .translation_missing{ font-size: 30px; color: red; font-family: Times; &:before{ content: "Translation Missing :: "; font-size: 30px; font-family: Times; color: red; } } 
+4
source

Add monkeypatch to your application.rb so that an exception occurs if there is no translation:

 module ActionView::Helpers::TranslationHelper def t_with_raise(*args) value = t_without_raise(*args) if value.to_s.match(/title="translation missing: (.+)"/) raise "Translation missing: #{$1}" else value end end alias_method :translate_with_raise, :t_with_raise alias_method_chain :t, :raise alias_method_chain :translate, :raise end 
0
source

Source: https://habr.com/ru/post/904016/


All Articles