Rails 3: Using HTML in i18n form helper translations
Asked Answered
E

4

10

I am using the automatic form label helper for creating my form labels and having them translated via the i18n support, however, I want to have HTML in the label and I can't figure out how to make it HTML safe.

For example:

en:
  helpers:
    label:
      product:
        name: 'Your Product Name <small>Try to be creative</small>'

Ends up as:

<label for="product_name">Your Product Name &lt;Try to be creative&gt;</label>

But I want it to be:

<label for="product_name">Your Product Name <small>Try to be creative</small></label>

Is there a way for me to specify the translation as html_safe so that it doesn't get encoded before the output?

Also, this seems like the most semantic way for the HTML to be setup but I am open to suggestions if I am approaching this entirely the wrong way.

Thanks :)

Updated:

<%= form_for @product do |f| %>
  <%= f.label :name %>
  <%= f.text_field :name %>
<% end %>
Elboa answered 7/12, 2010 at 1:44 Comment(0)
P
12

Don't know how your actual helper is, but you could use html_safe to do that easily (provided that your label's value won't be input by other users).

something like: t("helpers.label.product.name").html_safe

If this is not working, please give your implementation of your helper method, or only the lines for outputting the result.

====== UPDATED ======

Thanks to your updated code, now I know what's the best answer :D

I don't know too if you really want helpers.label.product.name.

But there is another way I think would be better, which is define as he following:

en:
  activerecord:
    attributes:
      product:
        labels:
          name: "Your Product Name <small>Try to be creative</small>"

If you don't want to build your own custom form builder, use this:

= f.label :name, Product.human_attribute_name("labels.name").html_safe

Actually if you define your own form builder, it easy redefine the label method to generate it's text automatically.

Phonic answered 7/12, 2010 at 2:59 Comment(3)
I am using the standard Rails helper: api.rubyonrails.org/classes/ActionView/Helpers/…Elboa
So are you using on a model? Could you mind pasting your code here?Phonic
I added the use to the original question. I'm just using the standard Rails form helper for a model.Elboa
B
15

The way to make a normal key automatically html safe in rails3 is to append _html or .html to the key name. E.g. name_html. This only works via the translate/t method in the view, i.e. not via I18n.t This isn't going to work here anyway though as the standard form helper doesn't try that version of the key.

Creating your own form builder is the way to go as suggested. You could also do

f.label :name, t('helpers.label.product.name_html')

If this only happens in a couple of places.

Bollworm answered 7/12, 2010 at 10:33 Comment(0)
P
12

Don't know how your actual helper is, but you could use html_safe to do that easily (provided that your label's value won't be input by other users).

something like: t("helpers.label.product.name").html_safe

If this is not working, please give your implementation of your helper method, or only the lines for outputting the result.

====== UPDATED ======

Thanks to your updated code, now I know what's the best answer :D

I don't know too if you really want helpers.label.product.name.

But there is another way I think would be better, which is define as he following:

en:
  activerecord:
    attributes:
      product:
        labels:
          name: "Your Product Name <small>Try to be creative</small>"

If you don't want to build your own custom form builder, use this:

= f.label :name, Product.human_attribute_name("labels.name").html_safe

Actually if you define your own form builder, it easy redefine the label method to generate it's text automatically.

Phonic answered 7/12, 2010 at 2:59 Comment(3)
I am using the standard Rails helper: api.rubyonrails.org/classes/ActionView/Helpers/…Elboa
So are you using on a model? Could you mind pasting your code here?Phonic
I added the use to the original question. I'm just using the standard Rails form helper for a model.Elboa
H
0

I solved this with a new function, which I placed in a helper file:

def labeltext_with_html(model,field)
  I18n.t("helpers.label.#{model}.#{field}").html_safe
end

I call it like this:

label(my_model, my_field, labeltext_with_html(my_model, my_field))

Just adjust the variables to suit your use case. The label tag may also need .html_safe, depending on the use case.

Hypothesize answered 5/12, 2020 at 9:44 Comment(0)
B
-8

The other option is to do the following:

    name: '<span>Your Product Name <small>Try to be creative</small></span>'
Borras answered 9/8, 2012 at 23:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.