Proposal: validation error messages in ActiveModel

(requires I18n current master)

This stuff is a very rough draft of how I propose to deal with validation error messages (and potentially other usecases that need I18n support, such as form labels).

It might go into Rails or end up in some plugin or gem.

I’m not looking so much at the actual integration to ActiveModel, yet. So far I’m just trying to sketch out the underlying stuff so that it covers all requirements in a consistent way.

I want this stuff to be re-usable in other places where similar patterns occur when it comes to I18n, such as form labels and potentially other stuff.

Problems solved

  • Messages in class method calls are treated as plain text by default (users might optionally want to treat them as translation keys for gettext-style class method calls).
  • Symbols in class method calls are treated as translation keys.
  • Use messages from class method calls consistently with translation keys that are implicitly used for a particular validation.
  • Different translation variants (such as :full_message) for the same message must be possible.
  • Translation calls should optionally be able to cascade over scopes.
  • Support format strings, e.g. “{attribute} {message}”, transparently


The Error class is the meant to work as a default Error class in ActiveModel.

I18n::Message is the main base class and is supposed to work situations such as error messages, form labels etc.

The Format class is used internally to format other strings (see below).


The Message class comes with six modules that each provide a single feature. This way everything’s completely optional and pluggable (thus extensible and easy to customize) even though we might choose to not impose this complexity on the enduser (and maybe pick a default configuration/provide a configuration dsl instead).


This module just takes a Message OR translation key (Symbol) and interpolation values. Calling #to_s will use the string or translate the key and interpolate the values. The module is organized in a way so that other modules can hook in easily.


This module will also translate Messages. This behavior is frequently asked for by people who want to use Gettext-style translation keys in class-level calls as in validates_presence_of :foo, :message => ‘The message’


This module adds the ability to specify Hashes as strings both at class-level and in translation data. E.g.:

validates_presence_of :foo, :message => { :short => ‘Short!’, :full => ‘The full message!’ }

Calling #to_s on the I18n::Message instance will take a variant key and try to find and use the variant. It defaults to the :short variant.


This module adds the ability to specify format strings both at class-level and in translation data. E.g.:

validates_presence_of :foo, :format => ‘The formatted {message}.’

This module also works in combination with the Variants module in the way one would expect:

validates_presence_of :foo, :message => { :short => ‘foo’, :full => ‘FOO’ } :format => { :full => ‘The formatted {message}.’ }

message.to_s(:full) would then wrap the :full variant ‘FOO’ into the :full format string and yield ‘The formatted FOO’. message.to_s (defaults to :short) would just yield ‘foo’ as expected.