hi, how can i override rails errors mesages (that are in english) to display them in an other language? thanks hangon
olivier hericord wrote:> how can i override rails errors mesages (that are in english) to > display them in an other language?You can look at how it is done in RForum (look at localization.rb, and its uses). -- Best regards, Alexey Verkhovsky Ruby Forum: http://ruby-forum.org (moderator) RForum: http://rforum.andreas-s.net (co-author) Instiki: http://instiki.org (maintainer)
Which error messages are you referring to? If you mean the ones created by the validation macros, then there''s a parameter for your own message, eg: validates_uniqueness_of :username, { :message => "Användarnamnet är redan registrerat" } and if you''re using english column names (ie "username" which, in for example swedish, would be "användarnamn") you can either create a lookup table in your model that "translates" the attribute name for display purposes, or as in the above example, simply take the easy road and pass the field/attribute name in the error message. Then you can output it in you view with something like this: <% if @post.errors.count > 0 %> <h3>The following errors was found</h3> <ul> <% @post.errors.each do |attr_name, message| %> <li><%= message %></li> <% end %> </ul> <% end %> This means of course that you''ll have to pass the attribute name in every error message, but to my knowledge that is the easiest way. However, if you where talking about a full-on localization module there doesn''t seem to be anything done together with rails yet, apart from the yaml implemention in RForum (as alexey said) -- johan On Mon, 17 Jan 2005 01:35:13 +0100, olivier hericord <hangonsoft-GANU6spQydw@public.gmane.org> wrote:> hi, > > how can i override rails errors mesages (that are in english) to > display them in an other language? > > thanks > > hangon > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Johan Sørensen Professional Futurist www.johansorensen.com
module ActiveRecord class Errors @@default_error_messages = { :inclusion => "is not included in the list", :invalid => "is invalid", :confirmation => "doesn''t match confirmation", :accepted => "must be accepted", :empty => "can''t be empty", :too_long => "is too long (max is %d characters)", :too_short => "is too short (min is %d characters)", :wrong_length => "is the wrong length (should be %d characters)", :taken => "has already been taken", } cattr_accessor :default_error_messages end end snowdevil $ ruby script/console development Loading environment... irb(main):001:0> ActiveRecord::Errors.default_error_messages[:empty] => "can''t be empty" irb(main):002:0> ActiveRecord::Errors.default_error_messages[:empty] "dude...." => "dude...." Hope this helps On Mon, 17 Jan 2005 01:35:13 +0100, olivier hericord <hangonsoft-GANU6spQydw@public.gmane.org> wrote:> hi, > > how can i override rails errors mesages (that are in english) to > display them in an other language? > > thanks > > hangon > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Tobi http://www.hieraki.org - Open source book authoring http://blog.leetsoft.com - Technical weblog
* Tobias Luetke <tobias.luetke-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> module ActiveRecord > class Errors > @@default_error_messages = {...} > end > endThat''s a very good hint, I hadn''t seen that before, thanks. Unfortunately, that''s not the end of the story: module ActionView module Helpers module ActiveRecordHelper def error_messages_for(object_name, options={}) [SNIP] content_tag("div", content_tag( options[:header_tag] || "h2", "#{pluralize(object.errors.count, "error")} prohibited this #{object_name.gsub("_", " ")} from being saved" ) [SNIP] Of course you can work around that with your own error-displaying view-helper function as well, but... Wolfgang
hangonsoft-GANU6spQydw@public.gmane.org
2005-Jan-20 10:05 UTC
Re: Re: how to override rails errors messages?
The best solution seems to localized the Rails Code for everyone. we got to work on putting ''human messages'' out of the rails code... someone for this ticket or patch ? :D> * Tobias Luetke <tobias.luetke-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: > > module ActiveRecord > > class Errors > > @@default_error_messages = {...} > > end > > end > > That''s a very good hint, I hadn''t seen that before, thanks. > > Unfortunately, that''s not the end of the story: > module ActionView > module Helpers > module ActiveRecordHelper > def error_messages_for(object_name, options={}) > [SNIP] > content_tag("div", > content_tag( > options[:header_tag] || "h2", > "#{pluralize(object.errors.count, "error")} prohibited this > #{object_name.gsub("_", " ")} from being saved" > ) > [SNIP] > > Of course you can work around that with your own error-displaying > view-helper function as well, but... > > Wolfgang > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >