Module: I18n::Inflector::Rails::InflectedTranslate

Defined in:
lib/i18n-inflector-rails/inflector.rb

Overview

This module contains a variant of the translate method that uses i18n_inflector_kinds available in the current context. The method from this module will wrap the ActionView::Helpers::TranslationHelper#translate method.

Instance Method Summary collapse

Instance Method Details

#t_prepare_inflection_options(inflector, locale, options) ⇒ Hash (protected)

This method tries to read i18n_inflector_kinds available in the current context.

Returns:

  • (Hash)

    the inflection options (kind => value)


337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/i18n-inflector-rails/inflector.rb', line 337

def t_prepare_inflection_options(inflector, locale, options)
  subopts = {}

  verifies = options[:inflector_verify_methods]
  verifies = inflector.options.verify_methods if verifies.nil?
  is_lazy  = options[:inflector_lazy_methods]
  is_lazy  = inflector.options.lazy_methods if is_lazy.nil?

  return subopts if (verifies && !respond_to?(:i18n_inflector_kinds))

  i18n_inflector_kinds.each_pair do |kind, meth|
    next if meth.nil?                                   # kind is registered but disabled from usage
    next if verifies && !respond_to?(meth)
    obj = method(meth)
    obj = obj.call { next kind, locale } unless is_lazy # lazy_methods is disabled
    subopts[kind] = obj
  end
  return subopts
end

#translate(key, options) ⇒ String Also known as: t

This method tries to feed itself with the data coming from i18n_inflector_kinds available in the current context. That data contains inflection pairs (kind => value) that will be passed to the interpolation method from I18n::Inflector through ActionView::Helpers::TranslationHelper#translate.

You may also pass inflection options directly, along with other options, without registering methods responsible for delivering tokens. See I18n Inflector documentation for more info about inflection options.

Returns the translated string with inflection patterns interpolated.

Parameters:

  • key (String)

    translation key

  • options (Hash)

    a set of options to pass to the translation routines

Options Hash (options):

Returns:

  • (String)

    the translated string with inflection patterns interpolated

Raises:

  • (I18n::InvalidInflectionKind)
  • (I18n::InvalidInflectionOption)
  • (I18n::InvalidInflectionToken)
  • (I18n::MisplacedInflectionToken)

299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/i18n-inflector-rails/inflector.rb', line 299

def translate(*args)
  opts_present  = args.last.is_a?(Hash)
  if opts_present
    options = args.last
    test_locale = options[:locale]
  else
    options = {}
  end
  test_locale ||= I18n.locale
  inflector = I18n.backend.inflector

  # return immediately if the locale is not supported
  return super unless inflector.inflected_locale?(test_locale)

  # collect inflection variables that are present in this context
  subopts  = t_prepare_inflection_options(inflector, locale, options)

  # jump to translate if no inflection options are present
  return super if subopts.empty?

  # pass options and call translate
  args.pop if opts_present
  args.push subopts.merge(options)
  super
end