Those that deal with I18n issues when developing a rails app will most likely have stumbled upon the problems with ActiveRecord messages. Currently the i18n team is undertaking various efforts to improve the situation amongst which is i18n lambda support.
This week Tim Lucas gave a preso at a ruby-on-rails meetup in Sydney presenting what he calls "view-first validations". I couldn't attend the meetup, but a gist was send around: http://gist.github.com/107555.
I thought the idea presented could be put to good use to solve the i18n issues that face ARs validate_* methods. And so I came up with a slight variation http://gist.github.com/108497 which using the gettext API to do the translations (but you could use I18n.t instead of course) and doesn't require the explicit use of the :message option for every call to the validate_* methods.
There are various reasons why I like this way of dealing with the validate_* messages:
- The actual translation of the validate_* message can be deferred to the view level, which is a more proper place imho.
- If you use the gettext API for translations then this way of working prevents having possible markup data in your models because sometimes a bit of markup may be needed in a translation string.
- No need for complicated lambda messages.
- It makes testing of models easier and clean again as you test against symbols in the errors array instead of testing against possibly translated messages.
Comments
No comments yet.