【问题标题】:how can I perform interpolation in I18n locales?如何在 I18n 语言环境中执行插值?
【发布时间】:2011-09-26 01:12:38
【问题描述】:

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

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)

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

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 yaml


    【解决方案1】:

    像这样替换你的 en.yml

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

    你的看法是这样的

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

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

    【讨论】:

      猜你喜欢
      • 2011-08-31
      • 2018-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-17
      • 2015-08-05
      • 1970-01-01
      相关资源
      最近更新 更多