问题 如何在I18n语言环境中执行插值?


有没有办法做这样的事情:

en:
  welcome:
    hello there, #{current_user.first_name}!  It's nice to see you again.

这显然是行不通的,显然“#{”是yaml中的无效字符,因为当我把它拉出来时,那个字符串只显示为“你好”。

我能做的最好的事情就像:

en:
  welcome:
    hello there, (name)!  It's nice to see you again.

....

t(:welcome).gsub("(name)", current_user.first_name)

但我并不为此疯狂......必须有更好的方法来做这种事情。


7525
2018-06-30 14:24


起源



答案:


像这样替换你的en.yml

en:
  welcome:
    "hello there, %{name}!  It's nice to see you again."

和你的观点一样

<%=t(:welcome, :name=> current_user.first_name) %>

基本上它作为命名参数传递。你可以找到更多 Rails指南18n插值


15
2018-06-30 14:51