0.01
No commit activity in last 3 years
No release in over 3 years
Extends the Ruby I18n gem with global variables. The globals will be available for interpolation in every translation without explicitly specifying them in a call to I18n.translate. The variables can be accessed through I18n.config.globals.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.6
>= 0

Runtime

>= 0
 Project Readme

I18n::Globals Build Status

Adds support for I18n global variables, which will be available for interpolation into every translation.

Description

Extends the Ruby I18n gem with global variables. The globals will be available for interpolation in every translation without explicitly specifying them in a call to I18n.translate. The variables can be accessed through I18n.config.globals.

Installation

Add this line to your application's Gemfile:

gem 'i18n-globals'

And then execute:

$ bundle

Or install it yourself as:

$ gem install i18n-globals

Usage

Add your global variables to the I18n.config.globals hash:

I18n.config.globals[:company] = 'Initech'

Your global variables will then be automatically interpolated into every translation:

# The translation of 'greeting' is 'Welcome to %{company}!'
I18n.t 'greeting' # => 'Welcome to Initech!'

You can override the globals:

I18n.t 'greeting', company: 'Initrode' # => 'Welcome to Initrode!'

It is also possible to mix globals and ordinary variables:

# The translation of 'signature' is '%{president}, President of %{company}'
I18n.t 'signature', president: 'Bill Lumbergh' # => 'Bill Lumbergh, President of Initech'

If you're using Rails, it can be useful to specify your globals in a before_action:

class EmployeesController < ApplicationController
  before_action :set_i18n_globals

  # ...

  private

  def set_i18n_globals
    I18n.config.globals[:company] = Company.current.name
  end
end

Now you can interpolate the company variable into every translation in your template:

<%= t 'greeting' %>
<%= t 'signature', president: 'Bill Lumbergh' %>

And this would produce:

Welcome to Initech!
Bill Lumbergh, President of Initech

Contributing

  1. Fork it ( https://github.com/attilahorvath/i18n-globals/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request