Friday, May 5, 2017

RAILS: How to eliminate `translation missing` from some I18n.t in production (I18n.t fallback enabled)?

Leave a Comment

In my Rails 4.2 app, all modules are developed with I18n.t(). Here is an example of the index.html.erb:

<table class="table table-hover">   <thead>         <tr>       <th><%= t('Create Date') %></th>       <th><%= t(@for_which.sub('_', ' ').titleize) %></th>       <th><%= t('Brief Note') %></th>       <th><%= t('Ranking Index') %></th>       <th><%= t('Active') %></th>       <th><%= t('Entered By') %></th>       <th></th>       <th></th>     </tr>      </thead>        <tbody> 

In local.rb under initializers, local is set to en:

I18n.enforce_available_locales = false I18n.default_locale = 'en' if Rails.env.production? || Rails.env.development? I18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')] 

But I still see error of translation missing from some of the I18n.t in both production and development, such as tab and index title as below: enter image description here

How do I get rid of error of translation missing?

UPDATE: The error translation missing shows up in production even with the following I18n.t fallback in environment/production.rb:

 # Enable locale fallbacks for I18n (makes lookups for any locale fall back to   # the I18n.default_locale when a translation cannot be found).   config.i18n.fallbacks = true 

UPDATE: Added CSS:

//hide all "translation missing" warnings: body.production span.translation_missing {   display: none; } 

But words on tab, buttons, and table header disappear: enter image description here

Here is portion of html source showing for one of the tab Employee Infos:

            <li class="dropdown">         <a id="dropdownuser" href="#" data-toggle="dropdown" class="dropdown-toggle" data-toggle="tooltip" data-placement="bottom" ><span class="glyphicons glyphicons-yen text-danger"></span> <span class="translation_missing" title="translation missing: en.Employee Info">Employee Info</span> <b class="caret" id="caret_medium"></b></a>               <ul id="banktransaction" class="dropdown-menu" role="menu">                  <li><a href="/tt/view_handler?index=1&amp;url=/tt/tsheet/employee_infos"><i class='glyphicon glyphicon-list'></i> translation missing: en.List</a></li>                 <li></li>                 <li><a href="/tt/view_handler?index=1&amp;url=/tt/tsheet/employee_infos/search"><i class='glyphicon glyphicon-list-alt'></i> translation missing: en.Search</a></li>                               </ul>             </li> 

2 Answers

Answers 1

in your #config/application.rb file you can do something like this

Rails.application.configure do |config|   config.action_view.raise_on_missing_translations = false end 

you can get more here

I hope that this is able to help you :)

Answers 2

Judging from the Rails 4.2 translate helper source code, I'm afraid you cannot simply switch off the warning message by a general setting.

From the ActionView code it is apparent that the translate helper by default forces raising an exception when passing the translation to the underlying I18n library. This behavior can be circumvented by a raise: false option passed to the translate helper and overriding the exception handler in the I18n library to return an empty string on missing translations. See the following tests in the Rails console:

# by default, the ActionView translate helper returns the "translation missing" # span on missing translation: helper.t('aa') => "<span class=\"translation_missing\" title=\"translation missing: en.aa\">Aa</span>"  # now let's force I18n library NOT to raise exception on missing translation: helper.t('aa',  raise: false) => "translation missing: en.aa"     # no SPAN any more, but still a warning message  # now let's tell I18n library to return nothing on missing translations I18n.exception_handler = ->(*args) { "" } helper.t('aa',  raise: false) => ""                               # finally this returns an empty string 

Although this approach works, it forces you to add raise: false parameter to all calls of t() in the templates which is probably not what you want.

However, there is a simpler solution:

Solution: hide the spans in your CSS

Yoy can leverage the fact that ActionView by default returns a SPAN with the translation_missing class as the warning about missing translation. Thus, you can simply hide all these spans in your CSS to get rid of the warnings:

# hide all "translation missing" warnings: span.translation_missing {   display: none; } 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment