No release in over 3 years
A pure Ruby implementation of ICU Message Format that integrates with the ruby-i18n gem via a chainable backend.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Runtime

>= 1.0
 Project Readme

I18n::MessageFormat

ICU Message Format support for the Ruby i18n gem. Pure Ruby parser, no native dependencies.

Installation

bundle add i18n-message_format

Usage

Standalone

require "i18n/message_format"

I18n::MessageFormat.format(
  "{name} has {count, plural, one {# item} other {# items}}",
  name: "Alice", count: 3
)
# => "Alice has 3 items"

With I18n Backend

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!"

Supported Syntax

  • 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 Formatting

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} calls I18n.localize with the value and locale. The optional style (e.g. {count, number, integer}) is passed as the :format option.
  • Date{d, date, short} calls I18n.localize with :format => :short. The style maps directly to an I18n date format key. Common styles are short, medium, long, and full, but any key defined under date.formats in your locale file works.
  • Time{t, time, short} works the same way, looking up keys under time.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

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.

Ordinal Rules

Install built-in ordinal rules for selectordinal support:

I18n::MessageFormat::OrdinalRules.install(:en)

Development

After checking out the repo, run bin/setup to install dependencies. Run tests with bundle exec rake test.

Contributing

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.

License

The gem is available as open source under the terms of the MIT License.