i18n

https://github.com/svenfuchs/i18n

Ruby

Internationalization (i18n) library for Ruby

I18n::Base#t

Translates, pluralizes and interpolates a given key using a given locale,
scope, and default, as well as interpolation values.

*LOOKUP*

Translation data is organized as a nested hash using the upper-level keys
as namespaces. <em>E.g.</em>, ActionView ships with the translation:
<tt>:date => {:formats => {:short => "%b %d"}}</tt>.

Translations can be looked up at any level of this hash using the key argument
and the scope option. <em>E.g.</em>, in this example <tt>I18n.t :date</tt>
returns the whole translations hash <tt>{:formats => {:short => "%b %d"}}</tt>.

Key can be either a single key or a dot-separated key (both Strings and Symbols
work). <em>E.g.</em>, the short format can be looked up using both:
  I18n.t 'date.formats.short'
  I18n.t :'date.formats.short'

Scope can be either a single key, a dot-separated key or an array of keys
or dot-separated keys. Keys and scopes can be combined freely. So these
examples will all look up the same short date format:
  I18n.t 'date.formats.short'
  I18n.t 'formats.short', :scope => 'date'
  I18n.t 'short', :scope => 'date.formats'
  I18n.t 'short', :scope => %w(date formats)

*INTERPOLATION*

Translations can contain interpolation variables which will be replaced by
values passed to #translate as part of the options hash, with the keys matching
the interpolation variable names.

<em>E.g.</em>, with a translation <tt>:foo => "foo %{bar}"</tt> the option
value for the key +bar+ will be interpolated into the translation:
  I18n.t :foo, :bar => 'baz' # => 'foo baz'

*PLURALIZATION*

Translation data can contain pluralized translations. Pluralized translations
are arrays of singluar/plural versions of translations like <tt>['Foo', 'Foos']</tt>.

Note that <tt>I18n::Backend::Simple</tt> only supports an algorithm for English
pluralization rules. Other algorithms can be supported by custom backends.

This returns the singular version of a pluralized translation:
  I18n.t :foo, :count => 1 # => 'Foo'

These both return the plural version of a pluralized translation:
  I18n.t :foo, :count => 0 # => 'Foos'
  I18n.t :foo, :count => 2 # => 'Foos'

The <tt>:count</tt> option can be used both for pluralization and interpolation.
<em>E.g.</em>, with the translation
<tt>:foo => ['%{count} foo', '%{count} foos']</tt>, count will
be interpolated to the pluralized translation:
  I18n.t :foo, :count => 1 # => '1 foo'

*DEFAULTS*

This returns the translation for <tt>:foo</tt> or <tt>default</tt> if no translation was found:
  I18n.t :foo, :default => 'default'

This returns the translation for <tt>:foo</tt> or the translation for <tt>:bar</tt> if no
translation for <tt>:foo</tt> was found:
  I18n.t :foo, :default => :bar

Returns the translation for <tt>:foo</tt> or the translation for <tt>:bar</tt>
or <tt>default</tt> if no translations for <tt>:foo</tt> and <tt>:bar</tt> were found.
  I18n.t :foo, :default => [:bar, 'default']

*BULK LOOKUP*

This returns an array with the translations for <tt>:foo</tt> and <tt>:bar</tt>.
  I18n.t [:foo, :bar]

Can be used with dot-separated nested keys:
  I18n.t [:'baz.foo', :'baz.bar']

Which is the same as using a scope option:
  I18n.t [:foo, :bar], :scope => :baz

*LAMBDAS*

Both translations and defaults can be given as Ruby lambdas. Lambdas will be
called and passed the key and options.

E.g. assuming the key <tt>:salutation</tt> resolves to:
  lambda { |key, options| options[:gender] == 'm' ? "Mr. %{options[:name]}" : "Mrs. %{options[:name]}" }

Then <tt>I18n.t(:salutation, :gender => 'w', :name => 'Smith') will result in "Mrs. Smith".

It is recommended to use/implement lambdas in an "idempotent" way. E.g. when
a cache layer is put in front of I18n.translate it will generate a cache key
from the argument values passed to #translate. Therefor your lambdas should
always return the same translations/values per unique combination of argument
values.

Source | Google | Stack overflow

Edit

git clone [email protected]:svenfuchs/i18n.git

cd i18n

open lib/i18n.rb

Contribute

# Make a new branch

git checkout -b -your-name--update-docs-I18n--Base-t-for-pr


# Commit to git

git add lib/i18n.rbgit commit -m "better docs for I18n::Base#t"


# Open pull request

gem install hub # on a mac you can `brew install hub`

hub fork

git push <your name> -your-name--update-docs-I18n--Base-t-for-pr

hub pull-request


# Celebrate!