【问题标题】:rails tutorial literred with hard coded error messages, how to localize?rails 教程中包含硬编码的错误消息,如何本地化?
【发布时间】:2010-09-25 01:52:12
【问题描述】:

刚刚浏览了 rails 教程,我注意到错误消息都是硬编码在控制器操作等中的。

从本地化资源文件中提取这些内容的模式是什么? (目前我在第 9 章)

【问题讨论】:

    标签: ruby-on-rails localization internationalization


    【解决方案1】:

    在您的 en.yml 文件中或在 config/locales/messages.en.yml 中创建一个新文件,添加来自控制器的硬编码错误消息

    en:
      errors:
        messages:
          not_found: "The record you searched doesn't exist"
          update_failed: "Your updated operation failed"
    

    要显示此消息,请使用

    (t "errors.messages.not_found")
    

    【讨论】:

      【解决方案2】:

      有一个 whole another guide 关于那个。

      (不知道您使用的是什么教程,但根据我的经验,guides.rubyonrails.org 提供了网络上最好的 Rails 参考资料)

      【讨论】:

      • @Blankman 没听说过,但祝你好运!如果您有任何困难,您可以随时咨询官方 Rails 指南。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多