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

发布于 2024-11-17 22:22:17 字数 415 浏览 1 评论 0原文

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

慈悲佛祖 2024-11-24 22:22:17

像这样替换你的 en.yml

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

和像这样的视图

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

基本上它是作为命名参数传递的。您可以在 Rails Guides 18n 插值 中找到更多信息

Replace your en.yml like this

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

and your view like this

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

Basically it is passed as a named argument. You can find more at Rails Guides 18n Interpolation

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文