ICU Message Format support for the Ruby i18n gem. Pure Ruby parser, no native dependencies.
bundle add i18n-message_formatrequire "i18n/message_format"
I18n::MessageFormat.format(
"{name} has {count, plural, one {# item} other {# items}}",
name: "Alice", count: 3
)
# => "Alice has 3 items"Store your Message Format strings in separate YAML files:
# config/locales/mf/en.yml
en:
greeting: "Hello {name}!"
items: "{count, plural, one {# item} other {# items}}"Configure the backend:
I18n.backend = I18n::Backend::Chain.new(
I18n::MessageFormat::Backend.new("config/locales/mf/*.yml"),
I18n::Backend::Simple.new
)
I18n.t("greeting", name: "Alice")
# => "Hello Alice!"- Simple arguments:
{name} - Number format:
{count, number} - Date format:
{d, date, short} - Time format:
{t, time, short} - Plural:
{count, plural, one {# item} other {# items}} - Select:
{gender, select, male {He} female {She} other {They}} - Selectordinal:
{pos, selectordinal, one {#st} two {#nd} few {#rd} other {#th}} - Nested messages
- Escaped braces:
'{ '} ''
Number, date, and time format arguments delegate to I18n.localize, so their output depends on the format definitions in your I18n backend.
- Number —
{count, number}callsI18n.localizewith the value and locale. The optional style (e.g.{count, number, integer}) is passed as the:formatoption. - Date —
{d, date, short}callsI18n.localizewith:format => :short. The style maps directly to an I18n date format key. Common styles areshort,medium,long, andfull, but any key defined underdate.formatsin your locale file works. - Time —
{t, time, short}works the same way, looking up keys undertime.formats.
For example, with these locale definitions:
en:
date:
formats:
short: "%b %-d"
long: "%B %-d, %Y"
time:
formats:
short: "%H:%M"The pattern "Updated on {d, date, short}" with d: Date.new(2026, 1, 15) produces "Updated on Jan 15".
Plural rules are also delegated to the I18n backend. Any gem which supplies plural rules to I18n will work with I18n::MessageFormat, e.g. rails-i18n.
Install built-in ordinal rules for selectordinal support:
I18n::MessageFormat::OrdinalRules.install(:en)After checking out the repo, run bin/setup to install dependencies. Run tests with bundle exec rake test.
Bug reports and pull requests are welcome on GitHub at https://github.com/aergonaut/i18n-message_format. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.
The gem is available as open source under the terms of the MIT License.