Rails: render ruby logic in i18n string
I'm currently translating a application and i've stumbled upon a problem. I'm using this in my views:
t("page.text")
and i have this is my yaml file:
page:
text: "This is my tekst with a开发者_如何学Go #{link_to "pages index, pages_path}"
This string gets outputted without the link_to logic in it, like this: "This is my tekst with a #{link_to "pages index, pages_path}". This is not what i want, i want the string parsed with the link_to function like this: "This is my tekst with a pages index" where pages index links to the /pages route...
Thanks.
Rails's i18n API allows you to use variable-based interpolation in your YAML file. To do what you're trying to do:
# config/en.yml
page:
text: "This is my test with a %{link}"
# view.html.erb
<%= t("page.text", :link => link_to("pages index", pages_path))
You can read more about it in the official Rails Guide: http://guides.rubyonrails.org/i18n.html#interpolation
精彩评论