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
Instance Method Summary collapse
-
#inflector ⇒ Object
This accessor allows to reach API methods of the inflector object associated with this class.
-
#reload! ⇒ Boolean
Cleans up internal hashes containg kinds, inflections and aliases.
-
#store_translations(locale, data, options = {}) ⇒ Hash
Stores translations in memory.
-
#translate(locale, key, options = {}) ⇒ String
Translates given key taking care of inflections.
Instance Method Details
#inflector ⇒ Object
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
It calls I18n::Backend::Simple#reload!
Cleans up internal hashes containg kinds, inflections and aliases.
38 39 40 41 |
# File 'lib/i18n-inflector/backend.rb', line 38 def reload! @inflector = nil super end |
#store_translations(locale, data, options = {}) ⇒ Hash
If inflections are changed it will regenerate proper internal structures.
Stores translations in memory.
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, = {}) 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
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.
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, = {}) inflector_try_init # take care about cache-awareness cached = if .key?(:inflector_cache_aware) [:inflector_cache_aware] else @inflector..cache_aware end if cached = @inflector..() else = .dup @inflector..clean_for_translate!() 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..() unless cached @inflector.interpolate(translated_string, locale, ) # complete the exception by adding translation key rescue I18n::InflectionException => e e.key = key raise end end |