本站首页    管理页面    写新日志    退出


«August 2025»
12
3456789
10111213141516
17181920212223
24252627282930
31


公告
 本博客在此声明所有文章均为转摘,只做资料收集使用。

我的分类(专题)

日志更新

最新评论

留言板

链接

Blog信息
blog名称:
日志总数:1304
评论数量:2242
留言数量:5
访问次数:7579129
建立时间:2006年5月29日




[Ruby on Rails]Active Record 错误信息本地化
软件技术

lhwork 发表于 2007/2/8 10:00:32

ActiveRecord出错信息是已经格式化过的英文字符串,这很不方便做本地化处理。要想做本地化,必须保留错误数据,在显示时再格式化为本地语言。不过ActiveRecord过早地把错误信息格式化为字符串,基本上已经断绝了本地化这条路。 为了让ActiveRecord错误信息可以本地化,我采用打补丁的方式。查看验证这部分代码,发现格式化字符串分散在各个验证方法中,一一重写不大合算。好在它是调用default_error_messages方法来取得错误信息字符串,于是考虑在这里做点文章。 代码 class ActiveRecord::ValidateError      attr_reader :error      def initialize(error, format, *args)        @error = error        @format = format        @args = args      end            def to_s        return @format if @args.empty?        @format % @args     end    end       class ActiveRecord::Errors      def self.default_error_messages        def self.default_error_messages          @@_error_messages       end        @@_error_messages = {}        @@default_error_messages.each do |key, value|          @@_error_messages[key] = ActiveRecord::ValidateError.new(key, value)        end        @@_error_messages     end   


阅读全文(2156) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.063 second(s), page refreshed 144752715 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号