简体   繁体   中英

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 #{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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM