Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable Rails I18n translation errors in views?

I created new Rails 3 project. I try to use translations in my views like this:

= t('.translate_test')

In my browser i looks "translate_test" instead "my test translation" witch i set in en.yml.

My main question - why i can't see error like "Missing translation: en ..." ?

like image 802
prosto.vint Avatar asked Dec 18 '11 10:12

prosto.vint


3 Answers

I've created this initializer to raise an exception - args are passed so you will 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

Source

like image 98
Betty St Avatar answered Nov 02 '22 15:11

Betty St


In Rails 3 they don't show you this text anymore. If you inspect the element in the html source you will see the translation missing message.

You can turn fallbacks off, try to put in your environment or an initializer the following:

config.i18n.fallbacks = false
like image 36
SteenhouwerD Avatar answered Nov 02 '22 13:11

SteenhouwerD


I use the simplest and view specific solution to display the errors in View when the translation is missing by adding this style in 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;
 }
}
like image 4
Sur Max Avatar answered Nov 02 '22 15:11

Sur Max