【问题标题】:I18n and Interpolation?I18n 和插值?
【发布时间】:2015-03-25 17:29:24
【问题描述】:

我希望能够在存储在 yml 语言环境文件中的字符串中插入变量。我想出如何做到这一点的唯一方法是使用正则表达式。

例如在 en.yml 我有

---
en:
  byline: "By <strong>{{author}}</strong>"`

然后在我的 erb 文件中:

&lt;%= t(:byline).gsub!(/{{author}}/, current_page.data.author) %&gt;

有更简单的方法吗?

【问题讨论】:

    标签: ruby regex middleman string-interpolation


    【解决方案1】:

    请参阅 Rails 国际化 API RailsGuide 中的 Passing Variables to Translations

    您可以在翻译消息中使用变量并从视图中传递它们的值。

    # app/views/home/index.html.erb
    <%=t 'greet_username', user: "Bill", message: "Goodbye" %>
    
    # config/locales/en.yml
    en:
      greet_username: "%{message}, %{user}!"
    

    【讨论】:

      猜你喜欢
      • 2021-06-01
      • 1970-01-01
      • 2012-12-27
      • 2013-09-30
      • 1970-01-01
      • 1970-01-01
      • 2017-01-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多