0.0
No commit activity in last 3 years
No release in over 3 years
Pre- and post-process you calls to I18n.t()
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.12
~> 10.0
~> 3.0

Runtime

>= 0
 Project Readme

I18n::Callbacks

Wrap your I18n.t calls in before and/or after hooks.

This gem provides the I18n::Backend::Callbacks backend.

You pass it an I18n backend to call plus before/after callbacks to pre-/post- process the calls.

Installation

Add this line to your application's Gemfile:

gem 'i18n-callbacks'

And then execute:

$ bundle

Or install it yourself as:

$ gem install i18n-callbacks

Usage

Supply either a :before or :after proc to the constructor, process I18n.t calls.

  • :before - takes 3 parameters: locale, key and options,
  • :after - takes 4 parameters: locale, key and options, and the result of the call to the backend that you are wrapping.

Suppose you want all translated strings to be prefixed by the key used to access them:

require "i18n"
require "i18n/callbacks"

wrapped = I18n::Backend::Simple.new

# ...load your translations (normally from YAML files)
wrapped.store_translations(:en, {foo: "bar"})

I18n.backend = I18n::Backend::Callbacks.new(
  wrapped,
  after: ->(locale, key, options={}, result) { "#{key}: #{result}" }
)

I18n.t(:foo) # => "foo: bar"

See also the examples directory.

Development

Run rake spec to run the tests.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/cantierecreativo/i18n-callbacks. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

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