module I18n::Base
def translate(key = nil, throw: false, raise: false, locale: nil, **options) # TODO deprecate :raise
I18n.t(:salutation, any_hash)
I18n.t(:salutation, { :gender => 'w', :name => 'Smith' })
Bad:
I18n.t(:salutation, **any_hash)
I18n.t(:salutation, **{ :gender => 'w', :name => 'Smith' })
I18n.t(:salutation, :gender => 'w', :name => 'Smith')
Good:
The "hash" parameter must be passed as keyword argument.
There is a breaking change in ruby that produces warning with ruby 2.7 and won't work as expected with ruby 3.0
This method uses keyword arguments.
*Ruby 2.7+ keyword arguments warning*
values.
always return the same translations/values per unique combination of argument
from the argument values passed to #translate. Therefore your lambdas should
a cache layer is put in front of I18n.translate it will generate a cache key
It is recommended to use/implement lambdas in an "idempotent" way. E.g. when
lambda { |key, options| options[:gender] == 'm' ? "Mr. %{name}" : "Mrs. %{name}" }
so the following lambda would give the same result:
Note that the string returned by lambda will go through string interpolation too,
Then I18n.t(:salutation, :gender => 'w', :name => 'Smith') will result in "Mrs. Smith".
lambda { |key, options| options[:gender] == 'm' ? "Mr. #{options[:name]}" : "Mrs. #{options[:name]}" }
E.g. assuming the key :salutation resolves to:
called and passed the key and options.
Both translations and defaults can be given as Ruby lambdas. Lambdas will be
*LAMBDAS*
I18n.t [:foo, :bar], :scope => :baz
Which is the same as using a scope option:
I18n.t [:'baz.foo', :'baz.bar']
Can be used with dot-separated nested keys:
I18n.t [:foo, :bar]
This returns an array with the translations for :foo and :bar.
*BULK LOOKUP*
I18n.t :foo, :default => [:bar, 'default']
or default if no translations for :foo and :bar were found.
Returns the translation for :foo or the translation for :bar
I18n.t :foo, :default => :bar
translation for :foo was found:
This returns the translation for :foo or the translation for :bar if no
I18n.t :foo, :default => 'default'
This returns the translation for :foo or default if no translation was found:
*DEFAULTS*
I18n.t :foo, :count => 1 # => '1 foo'
be interpolated to the pluralized translation:
:foo => ['%{count} foo', '%{count} foos'], count will
E.g., with the translation
The :count option can be used both for pluralization and interpolation.
I18n.t :foo, :count => 2 # => 'Foos'
I18n.t :foo, :count => 0 # => 'Foos'
These both return the plural version of a pluralized translation:
I18n.t :foo, :count => 1 # => 'Foo'
This returns the singular version of a pluralized translation:
pluralization rules. Other algorithms can be supported by custom backends.
Note that I18n::Backend::Simple only supports an algorithm for English
are arrays of singular/plural versions of translations like ['Foo', 'Foos'].
Translation data can contain pluralized translations. Pluralized translations
*PLURALIZATION*
I18n.t :foo, :bar => 'baz' # => 'foo baz'
value for the key +bar+ will be interpolated into the translation:
E.g., with a translation :foo => "foo %{bar}" the option
the interpolation variable names.
values passed to #translate as part of the options hash, with the keys matching
Translations can contain interpolation variables which will be replaced by
*INTERPOLATION*
I18n.t 'short', :scope => %w(date formats)
I18n.t 'short', :scope => 'date.formats'
I18n.t 'formats.short', :scope => 'date'
I18n.t 'date.formats.short'
examples will all look up the same short date format:
or dot-separated keys. Keys and scopes can be combined freely. So these
Scope can be either a single key, a dot-separated key or an array of keys
I18n.t :'date.formats.short'
I18n.t 'date.formats.short'
work). E.g., the short format can be looked up using both:
Key can be either a single key or a dot-separated key (both Strings and Symbols
returns the whole translations hash {:formats => {:short => "%b %d"}}.
and the scope option. E.g., in this example I18n.t :date
Translations can be looked up at any level of this hash using the key argument
:date => {:formats => {:short => "%b %d"}}.
as namespaces. E.g., ActionView ships with the translation:
Translation data is organized as a nested hash using the upper-level keys
*LOOKUP*
scope, and default, as well as interpolation values.
Translates, pluralizes and interpolates a given key using a given locale,
def translate(key = nil, throw: false, raise: false, locale: nil, **options) # TODO deprecate :raise locale ||= config.locale raise Disabled.new('t') if locale == false enforce_available_locales!(locale) backend = config.backend if key.is_a?(Array) key.map do |k| translate_key(k, throw, raise, locale, backend, options) end else translate_key(key, throw, raise, locale, backend, options) end end