Module: I18n::Backend::Inflector

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

Overview

This module contains methods that add tokenized inflection support to internal I18n classes. It is intened to be included in the Simple backend module so that it will patch translate method in order to interpolate additional inflection tokens present in translations. Usually you don’t have to know what’s here to use it.

Constant Summary collapse

InflectorCfg =

Shortcut to configuration module.

I18n::Inflector::Config

Instance Method Summary collapse

Instance Method Details

#inflectorObject

This accessor allows to reach API methods of the inflector object associated with this class.



28
29
30
31
# File 'lib/i18n-inflector/backend.rb', line 28

def inflector
  inflector_try_init
  @inflector
end

#reload!Boolean

Note:

It calls I18n::Backend::Simple#reload!

Cleans up internal hashes containg kinds, inflections and aliases.

Returns:

  • (Boolean)

    the result of calling ancestor’s method



38
39
40
41
# File 'lib/i18n-inflector/backend.rb', line 38

def reload!
  @inflector = nil
  super
end

#store_translations(locale, data, options = {}) ⇒ Hash

Note:

If inflections are changed it will regenerate proper internal structures.

Stores translations in memory.

Returns:

  • (Hash)

    the stored translations

Raises:



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/i18n-inflector/backend.rb', line 105

def store_translations(locale, data, options = {})
  r = super
  inflector_try_init
  if data.respond_to?(:has_key?)
    subdata = data[:i18n] || data['i18n']
    unless subdata.nil?
      subdata = subdata[:inflections] || subdata['inflections']
      unless subdata.nil?
        db, db_strict = load_inflection_tokens(locale, r[:i18n][:inflections])
        @inflector.add_database(db, db_strict)
      end
    end
  end
  r
end

#translate(locale, key, options = {}) ⇒ String

Note:

The given options along with a translated string and the given locale are passed to I18n::Backend::Simple#translate and then the result is processed by Inflector::API#interpolate

Translates given key taking care of inflections.

Parameters:

  • locale (Symbol)

    locale

  • key (Symbol, String)

    translation key

  • options (Hash) (defaults to: {})

    a set of options to pass to the translation routines.

Returns:

  • (String)

    the translated string with interpolated patterns

See Also:



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/i18n-inflector/backend.rb', line 56

def translate(locale, key, options = {})
  inflector_try_init

  # take care about cache-awareness
  cached = if options.key?(:inflector_cache_aware)
             options[:inflector_cache_aware]
           else
             @inflector.options.cache_aware
           end

  if cached
    interpolate_options = options
    @inflector.options.prepare_options!(options)
  else
    interpolate_options = options.dup
    @inflector.options.clean_for_translate!(options)
  end

  # translate string using original translate
  translated_string = super

  # generate a pattern from key-based inflection object
  if translated_string.is_a?(Hash) && key.to_s[0..0] == InflectorCfg::Markers::STRICT_KIND
    translated_string = @inflector.key_to_pattern(translated_string)
  end

  # interpolate string
  begin
    @inflector.options.prepare_options!(interpolate_options) unless cached
    @inflector.interpolate(translated_string, locale, interpolate_options)

  # complete the exception by adding translation key
  rescue I18n::InflectionException => e
    e.key = key
    raise
  end
end