1. 要有zh-CN.yml这个文件,这个可以去github上下载,有别人做的

  2. 要安装i18n 的gem

  3. config下application.rb里要有config.i18n.default_locale = "zh-CN"

  4. 在yml文件里加上

  5. zh-CN:

  6.   activerecord:  

  7.     attributes:  

  8.       product:  

  9.         title: "标题"  

  10.         description: "描述"

  11.         p_w_picpath_url: "图片"

    因为我猜测是和下边标红的有关系,有对应的messages,却没有attributes,经过查询资料后发现确实是这个问题,上边product是我的model,title等是我的数据库对应字段名称,试过后提示果然是中文了。

  12.  errors:

  13.     format: ! '%{attribute}%{message}'

  14.     messages:

  15.       accepted: 必须是可被接受的

    但是还有一个问题,在页面的顶端还有英文:

  16. 3 errors prohibited this product from being saved:

终于找到了,这个其实是自己想多了,他就在页面上,我却以为是yml文件里有配置,一直在里面找,被自己的固定思维给圈住了

页面代码:

 <h2><%= pluralize(@product.errors.count, "error") %> prohibited this product from being saved:</h2>

但是如果显示汉字不止要把prohibited this product from being saved:换为汉字,还要去掉pluralize,因为不去掉这个方法,他会根据count返回的数字有处理,单数正常显示,复数后边会有个s,所以变成这样可以:

 <h2><%= @product.errors.count %> 个错误发生导致无法被保存</h2>

显示效果:

3 个错误发生导致无法被保存

不然去掉那个方法会变为

3 s个错误发生导致无法被保存