Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/svenfuchs/i18n-message

Object-oriented abstraction for looking up translations from I18n.translate
https://github.com/svenfuchs/i18n-message

Last synced: about 2 months ago
JSON representation

Object-oriented abstraction for looking up translations from I18n.translate

Awesome Lists containing this project

README

        

h1. I18n Message

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

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

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

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

h3. 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'

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

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