论坛首页 Ruby版 rails

两步搞定”错误消息中文化“

浏览 293 次
精华帖 (0) :: 良好帖 (1) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
时间:2008-08-06 关键字: humanize
1 修改rails的default_error_message
ActiveRecord::Errors.default_error_messages={
	:inclusion => " 没有包括在列表内!" ,
	:exclusion => " 已被保存过了!" ,
	:invalid => " 是无效的!" ,
	:confirmation => " 不匹配的信息!" ,
	:accepted => " 必须接受此选项!" ,
	:empty => " 不能为空!" ,
	:blank => " 不能为空!" ,
	:too_long => " 太长了(最大是 %d 个英文字符)!" ,
	:too_short => " 太短了(最小是 %d 个英文字符)!" ,
	:wrong_length => " 长度有错误(应该是 %d 个英文字符)!" ,
	:taken => " 已被使用!" ,
	:not_a_number => " 不是个数字!"
}

2 修改一下humanize
我让Gloc帮我管理中文消息,zh.yml中写着一下内容:
  name: 名称
  content:内容
  title: 标题
  .................

在config/initializers/inflections.rb中加入
Inflector.module_eval do
	def humanize_with_gloc(str)
		humanize= GLoc.l str
		if humanize==str
			humanize_without_gloc(str)
		else
			humanize
		end
	end
	alias_method_chain :humanize, :gloc
end


然后,你的错误消息就会有:“名称 不能为空!”之类的了。
   
时间:2008-08-10
这种修改只能得到一小部分的好处,大多数开发还是要用自定义的错误信息的。觉得意义不甚大。
   
0 请登录后投票
论坛首页 Ruby版 rails

跳转论坛:
JavaEye推荐