I18nOnSteroids is a Ruby gem that enhances Rails' I18n functionality with advanced interpolation and piping features. It allows you to use more complex translation patterns directly in your locale files, reducing the need for view-specific formatting logic.
Add this line to your application's Gemfile:
gem 'i18n_on_steroids'
And then execute:
$ bundle install
Or install it yourself as:
$ gem install i18n_on_steroids
Once installed, I18nOnSteroids automatically enhances your Rails I18n functionality. You can use the following advanced features in your locale files:
Use ${}
for advanced interpolation with pipes:
en:
users:
count: "There are ${count | number_with_delimiter} users"
In your view:
<%= t('users.count', count: 1000) %>
# Output: "There are 1,000 users"
The goal is to support all the ActionView::Helpers methods that make sense in a translation context. The following pipes are currently available:
number_with_delimiter
: Formats numbers with thousands separatorspluralize
: Pluralizes a wordupcase
: Converts text to uppercasedowncase
: Converts text to lowercasecapitalize
: Capitalizes the first characterhtml_safe
: Marks the string as HTML safeformat:...
: Applies a format string to the value
You can mix regular interpolation %{}
with advanced interpolation ${}
:
en:
items:
summary: "Showing %{from} - %{to} of ${total | number_with_delimiter} ${item | pluralize:%{total}}"
In your view:
<%= t('items.summary', from: 1, to: 10, total: 1234, item: 'item') %>
# Output: "Showing 1 - 10 of 1,234 items"
After checking out the repo, run bin/setup
to install dependencies. Then, run rake test
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
.
Bug reports and pull requests are welcome on GitHub at https://github.com/games-directory/i18n_on_steroids. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.
- Fork the repository
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
Please make sure to update/add any relevant tests and documentation before submitting your pull request. Also, bundle exec rake
must pass without any failures.
The gem is available as open source under the terms of the MIT License.
Everyone interacting in the I18nOnSteroids project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.