ruby on rails校验信息修改为中文

发布时间:2020-07-30 21:20:53 作者:骑猴上树
来源:网络 阅读:814
  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文件里有配置,一直在里面找,被自己的固定思维给圈住了

页面代码:

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

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

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

显示效果:

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

不然去掉那个方法会变为

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


推荐阅读:
  1. itertools.product方法怎么在Python中使用
  2. 封装python代码的方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

product errors title

上一篇:[Warning] TIMESTAMP with implicit DEFAULT value is deprecated.

下一篇:[cocos2d-x]场景动画切换过渡大全

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》