Class: Lolita::I18n::Request::Translations
- Inherits:
-
Object
- Object
- Lolita::I18n::Request::Translations
- Defined in:
- lib/lolita-i18n/request.rb
Instance Method Summary collapse
- #final_value?(value) ⇒ Boolean
- #flatten_keys(hash, locale, prev_key = nil, &block) ⇒ Object
-
#initialize(translations_hash) ⇒ Translations
constructor
A new instance of Translations.
- #normalized(locale) ⇒ Object
- #translation_value(key, value, locale) ⇒ Object
Constructor Details
#initialize(translations_hash) ⇒ Translations
Returns a new instance of Translations.
103 104 105 106 |
# File 'lib/lolita-i18n/request.rb', line 103 def initialize(translations_hash) translations_hash.delete(:'i18n') @translations = translations_hash end |
Instance Method Details
#final_value?(value) ⇒ Boolean
133 134 135 136 |
# File 'lib/lolita-i18n/request.rb', line 133 def final_value?(value) !value.is_a?(Hash) || (value.is_a?(Hash) && value.keys.map(&:to_sym).include?(:other) && value.keys.size > 1 && !value.values.detect{|val| val.is_a?(Array) || val.is_a?(Hash)}) end |
#flatten_keys(hash, locale, prev_key = nil, &block) ⇒ Object
122 123 124 125 126 127 128 129 130 131 |
# File 'lib/lolita-i18n/request.rb', line 122 def flatten_keys hash,locale, prev_key = nil, &block hash.each_pair do |key,value| current_key = [prev_key, key].compact.join(".").to_sym if final_value?(value) yield current_key, translation_value(current_key,value,locale), original_translation_value(current_key,value) else flatten_keys(value,locale,current_key, &block) end end end |
#normalized(locale) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/lolita-i18n/request.rb', line 108 def normalized(locale) unless @normalized @normalized = {} flatten_keys(@translations,locale) do |key,value,original_value| @normalized[key] = { :translation => value, :original_translation => original_value, :url => Lolita.i18n.store["views.#{key}"] } end end @normalized end |
#translation_value(key, value, locale) ⇒ Object
138 139 140 141 142 143 |
# File 'lib/lolita-i18n/request.rb', line 138 def translation_value key, value, locale translation_value = ::I18n.t(key,:locale => locale, :default => default_value_from(value)) translation_value = {} if value.is_a?(Hash) && !translation_value.is_a?(Hash) translation_value = [] if value.is_a?(Array) && !translation_value.is_a?(Array) translation_value end |