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
-
#t_prepare_inflection_options(inflector, locale, options) ⇒ Hash
protected
This method tries to read
i18n_inflector_kinds
available in the current context. -
#translate(key, options) ⇒ String
(also: #t)
This method tries to feed itself with the data coming from i18n_inflector_kinds available in the current context.
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.
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 (inflector, locale, ) subopts = {} verifies = [:inflector_verify_methods] verifies = inflector..verify_methods if verifies.nil? is_lazy = [:inflector_lazy_methods] is_lazy = inflector..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.
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 = args.last test_locale = [:locale] else = {} 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 = (inflector, locale, ) # 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() super end |