3

I need to embed links in my translated texts. I followed this post, but it doesn't seem to work in rails 3 anymore as the html tags don't get rendered properly.

Anyone knows how to get this done in rails 3?

Update: Apparently, the html tags can be escaped by using the html_safe method. But does anyone know if there's another way to solve this problem without using html_safe?

I would like to avoid unescaping my html tags if possible, b/c I've encountered a situation where I have to pass in a text field into my translation, and I would like to avoid unescaping any strings that are user inputted.

Community
  • 1
  • 1
Jonathan Chiu
  • 1,637
  • 2
  • 16
  • 25

1 Answers1

2

Change {{url}} to %{url} and you should be good to go.

Update

Ok, thanks, that's important information about what "doesn't work" means :) So, you need to call the html_safe method on your call to link_to, eg.

link_to(t("log_in_href"), login_path).html_safe

This will tell Rails to render the HTML, not escaped.

smathy
  • 26,283
  • 5
  • 48
  • 68
  • Yeah I've done that, that's not the problem. The variable gets passed in, but the html tags still render as is. – Jonathan Chiu Apr 21 '11 at 05:51
  • 2
    E.g. "Don't have a paypal account? Get one here" becomes "Don't have a Paypal Account? Get one here." – Jonathan Chiu Apr 21 '11 at 05:53
  • The html_safe solves the problem for links, but I just encountered a situation where I have to pass in a full text field as a variable into the translation text, and I don't feel comfortable doing html_safe on that. Is there another way around this without using html_safe? – Jonathan Chiu Apr 21 '11 at 09:00
  • Split up whatever you're passing in so you *are* comfortable `html_safe`ing the HTML part of it. – smathy Apr 21 '11 at 14:18
  • Yeah, or I'll probably find some ways to sanitize it first. Thanks. – Jonathan Chiu Apr 21 '11 at 16:35
  • Just encountered the issue of rendering links/other html inside localized strings. As a note for other people: the html_safe should be called on the localized string not the link_to. E.g. t('.name', :link => root_path).html_safe – ghempton Apr 22 '11 at 01:08