How do you use a link_to helper in a YML file that is being sanitized?

421 Views Asked by At

I have this key in my locales.yml file that uses a link_to helper.

payment_types:
   credit_card: "Requires bank authorization. <%= link_to 'Tutorial', 'www.linktotutorial.com', target: '_blank' %>"

Previously we used an a tag with the href inside along with sanitize, like so:

sanitize(t("payment_types.#{payment_type}"),
                tags: %w(a), attributes: %w(target href)), payment_type

But I have to refactor it to use the link_to helper.

The problem is that sanitize is filtering the erb tag alltogether (nothing appears past "Requires bank authorization"), and I can't find a way to allow the erb tag and render the link_to properly. Getting rid of sanitize in the other hand, renders the string with the erb tags included.

Are erb tags even allowed in yml files?

1

There are 1 best solutions below

0
On

To parse that ERB tag you can create something like locales.yml.erb
But again there will be too much of hassle to check if Rails is internally parsing ERB yml or not.
And even if it is parsing then will link_to helper will be available or not.


Instead I would suggest a simple way:

# locales.yml
payment_types:
   credit_card_html: "Requires bank authorization. %{titorial_link}"
# Usage in views
= t("payment_types.#{payment_type}_html", tutorial_link: link_to('Tutorial', 'www.linktotutorial.com', target: '_blank'))

Please note _html suffix. It is for HTML safe translations.
Read more about HTML safe translations here