Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3, i18n: Interpolating html tags into text

My problem is exactly the one described in this question. Only difference: I'm using Rails 3 (3.0.4 exactly), which uses the %{...} syntax rather than the old {{...}}.

Problem: Rails 3 escapes all tags inside %{...} so I cannot interpolate, say, links in there.

Is there any way to overcome this?

like image 723
DeX3 Avatar asked Mar 20 '11 09:03

DeX3


2 Answers

I finally figured it out, you have to use _html as a suffix for any yaml-keys, that you do not want html-escaped:

paragraph_html: "This is some text with a %{link}"
paragraph_linktext: "really cool link"

and then

link = link_to t( paragraph_linktext ), "http://www.example.com"
<%= t( paragraph_html, :link => link ) %>

and voila, there you have your i18n-ized link.

like image 135
DeX3 Avatar answered Oct 31 '22 05:10

DeX3


You can do like this

my_translation: "Click to access %{link} page"

then:

<%= t( my_translation, :link => link_to("Google", "http://google.com") ).html_safe %>

And everything will be ok!

like image 25
João Fraga Avatar answered Oct 31 '22 05:10

João Fraga