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
At first sight the Rails I18n guide seem pretty good, and cover this pretty well (e.g. an example to declare a custom exception handler).
But according to this ticket it does not work in rails since 4.0.2 and up (but should be fixed in latest rails 4.1 release).
Apparently the behaviour has changed and the exception handler is ignored now.
Available options:
raise: true
, which will force the exceptionhandler to be used. E.g.t('.missing', raise: true)
.config.action_view.raise_on_missing_translations = true
(see merge ticket for more info)t('.missing', default: 'use this instead')
I18n library uses exception handler to decide what to do with missing translations. By default it returns "translation missing" message:
You can extend the exception handler to just return translation key when the translation is missing:
Then assuming you have only french translations:
try this
and in your en.yml file write
Given below code is to remove missing translation for english, we monkey patch MissingTranslation module of I18n, by putting it in initializers.
If you don't want to fill in translations for English, a possible solution here would be to have your translations fallback to French.
You can achieve this by adding the following code to an initializer (eg.
config/initalizers/i18n.rb
) :If French is set up as your default locale, this should "Just Work".
Otherwise you may need to add a custom fallback rule to the initializer :
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.