Rails: Remove missing translation errors

Paritosh Singh picture Paritosh Singh · Oct 16, 2012 · Viewed 16.2k times · Source

I am using internationalization for english (en) and french (fr), I have used en.yml for limited use and most of the translations I am writing in fr.yml.

With locale as fr everything works good, but with en it shows me error as missing translation span.

For eg if I have got something like

<%= text_field_tag( "search", params[:search], :placeholder=>t("Search"), :class=>"search_input") %>

and i get output for en is:

<input class="search_input" id="search" name="search" placeholder="<span class=" translation_missing"="" title="translation missing: en.Search">

What I want is that it should turn off translation errors for english, since english is my default language, but for some cases I've used en.yml.

Or if this is not possible then whole error message should be removed.

Thanks

Answer

Simon Perepelitsa picture Simon Perepelitsa · Apr 17, 2014

The implementation of HTML missing translation errors has been changed in Rails 4.1. Now instead of I18n library it is handled on the view helper layer. Namely, in "translate" helper method (see action_view/helpers/translation_helper.rb).

A clean way to do this now is to override the helper method and handle the exception yourself.

# app/helpers/i18n_helper.rb

module I18nHelper
  def translate(key, options={})
    super(key, options.merge(raise: true))
  rescue I18n::MissingTranslationData
    key
  end
  alias :t :translate
end