【问题标题】:Why is the word "notifications" causing i18n to break in rails?为什么“通知”这个词会导致 i18n 中断?
【发布时间】:2012-08-14 05:53:24
【问题描述】:

我在当前的 Rails 项目中遇到了一个非常奇怪的 i18n 问题。每当我尝试使用密钥 notifications 时,都会收到翻译缺失消息。例如:

#en-US.yml
en-US:
  notifications:
    index:
      title: 'notification page'

#/notifications/index.html.erb
<%= content_for :title, t('.title') -%>

这会失败并告诉我翻译丢失了

#en-US.yml
en-US:
  notifications:
    index:
      title: 'notification page'

#/notifications/index.html.erb
<%= content_for :title, t('notifications.index.title') -%>

这也会失败。

#en-US.yml
en-US:
  notification:
    index:
      title: 'notification page'

#/notifications/index.html.erb
<%= content_for :title, t('notification.index.title') -%>

奇怪的是,这会起作用,删除 s 将允许它找到翻译。

#en-US.yml
en-US:
  notifications1:
    index:
      title: 'notification page'

#/notifications/index.html.erb
<%= content_for :title, t('notifications1.index.title') -%>

这也可以,在通知末尾添加1 即可。

Rails 似乎不喜欢通知这个词。这是一个问题,因为我不想为此重命名整个模型,而且我想使用 i18n.t 视图快捷方式来保持一致性。 notifications 是保留字吗?找不到它有什么原因吗?

【问题讨论】:

    标签: ruby-on-rails-3 internationalization


    【解决方案1】:

    我不确定为什么在你的情况下它会失败,但我自己试了一下,它不是保留字,我的语言环境文件:

    en:
      notifications:
        foo: bar
    

    结果:

    1.8.7 :001 > I18n.t('notifications.foo')
     => "bar"
    

    此测试是使用 Rails 3.1.5 和 Ruby 1.8.7 完成的。

    在你的情况下,你得到以下?

    1.8.7 :001 > I18n.t('notifications')
     => "translation missing: en.notifications" 
    

    【讨论】:

      【解决方案2】:

      虽然不是一个确切的答案,但我发现I18n中有几个词不能用作键。我假设 I18n 代码在其他地方使用它们。

      其他包括“否”、“是”、“开”等。

      只需使用同义词或执行“通知_”之类的操作即可使其与众不同。

      【讨论】:

        猜你喜欢
        • 2022-10-20
        • 2013-02-25
        • 2014-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-17
        • 2014-10-02
        相关资源
        最近更新 更多