t('act...">

Remove html tags from I18n "translation missing" messages

Consider the following code in a view:

<%= link_to 'Delete!', item , :confirm => t('action.item.confirm_deletion'), :method => :delete %> 

It is usually issued as:

 <a href="/items/123" data-confirm="Confirm deletion?" data-method="delete" rel="nofollow">Delete!</a> 

But if for some reason there is no translation for action.item.confirm_deletion (incomplete yml file, typos, etc.), it looks like:

 <a href="/items/123" data-confirm="<span class="translation_missing" title="translation missing: sv.action.item.confirm_deletion">Confirm Deletion</span>" data-method="delete" rel="nofollow">Delete!</a> 

which is invalid html and the user will see broken html tags on the main page. In some cases, this can also be a security risk.

I know that I could use some shielding every time I18n.t is called, but this is repeated once more for the task.

So, my question is: Is there a way to make the "translation missing" - the locations do not contain html code.

+6
source share
2 answers

There are several solutions for you.

You can list the translation method yourself and call it using a custom value :default (I would prefer this method):

 module ActionView module Helpers module TranslationHelper alias_method :translate_without_default :translate def translate(key, options = {}) options.merge!(:default => "translation missing: #{key}") unless options.key?(:default) translate_without_default(key, options) end end end end 

Or you can overwrite the default value:

 module I18n class MissingTranslation def html_message "translation missing: #{keys.join('.')}" end end end 
+4
source

In rails 4.2, you need to override the translate helper in the view:

https://github.com/rails/rails/blob/v4.2.5/actionview/lib/action_view/helpers/translation_helper.rb#L78

In rails 5 you can install .rb in your application:

 config.action_view.debug_missing_translation = false 
+1
source

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


All Articles