Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 016fee30f5
Fetching contributors…

Cannot retrieve contributors at this time

file 71 lines (49 sloc) 2.825 kb

I18n Message

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

Classes

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).

Features

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).

I18n::Message::Base

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.

I18n::Message::Gettext

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’

I18n::Message::Variants

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.

I18n::Message::Formatted

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.

Something went wrong with that request. Please try again.