如何在 I18n 语言环境中执行插值?
有没有办法做这样的事情:
en:
welcome:
hello there, #{current_user.first_name}! It's nice to see you again.
这显然行不通,显然“#{”在 yaml 中是无效字符,因为当我把它拉出来时,该字符串显示为“hello there”。
我能做的最好的事情就是:
en:
welcome:
hello there, (name)! It's nice to see you again.
....
t(:welcome).gsub("(name)", current_user.first_name)
但我对此并不疯狂......一定有更好的方法来做这种事情。
Is there a way to do something like this:
en:
welcome:
hello there, #{current_user.first_name}! It's nice to see you again.
That obviously won't work, and apparently "#{" is invalid characters in yaml, because that string shows up as just "hello there, " when I pull it out.
The best I could do was something like:
en:
welcome:
hello there, (name)! It's nice to see you again.
....
t(:welcome).gsub("(name)", current_user.first_name)
But I am not crazy about that... There must be a better way to do this sort of thing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
像这样替换你的 en.yml
和像这样的视图
基本上它是作为命名参数传递的。您可以在 Rails Guides 18n 插值 中找到更多信息
Replace your en.yml like this
and your view like this
Basically it is passed as a named argument. You can find more at Rails Guides 18n Interpolation