class TranslationPanel::RedisBackend
Public Class Methods
new(store)
click to toggle source
# File lib/translation_panel/redis_backend.rb, line 5 def initialize(store) @subtrees = false @store = store end
Protected Instance Methods
expand_keys(flatten_hash)
click to toggle source
Transforms flatten hash into nested hash
expand_keys "some.one" => "a", "some.another" => "b" # => "some" => {"one" => "a", "another" => "b"}
# File lib/translation_panel/redis_backend.rb, line 39 def expand_keys(flatten_hash) expanded_hash = {} flatten_hash.each do |key, value| key_parts = key.partition "." if key_parts[2].empty? expanded_hash.deep_merge! key => value else expanded_hash.deep_merge! key_parts[0] => expand_keys(key_parts[2] => value) end end expanded_hash end
get_count_keys(locale, key)
click to toggle source
Creates empty translations for absented pluralization keys. Returns hash with plural keys and their values (even from another backend)
# File lib/translation_panel/redis_backend.rb, line 54 def get_count_keys(locale, key) empty_translations = {} store_empty_translations = false count_keys = I18n.t!('i18n.plural.keys', :locale => locale).inject({}) do |result, plural_key| full_key = "#{key}.#{plural_key}" value = get_translate(locale, full_key) if value store_empty_translations = true result.merge plural_key.to_s => value else empty_translations.merge! full_key => "" result end end if store_empty_translations && empty_translations.present? I18n.backend.store_translations locale, empty_translations, :escape => false end count_keys rescue {} end
get_translate(locale, key)
click to toggle source
returns translation key if any, otherwise nil
# File lib/translation_panel/redis_backend.rb, line 77 def get_translate(locale, key) I18n.t!(key, :locale => locale) rescue nil end
lookup(locale, key, scope = [], options = {})
click to toggle source
# File lib/translation_panel/redis_backend.rb, line 11 def lookup(locale, key, scope = [], options = {}) key = normalize_flat_keys(locale, key, scope, options[:separator]) count = options[:count] if !count && value = @store["#{locale}.#{key}"] TranslationPanel.push key if TranslationPanel.show? ActiveSupport::JSON.decode(value) if value else # look in namespace pluralization_result = count ? get_count_keys(locale, key) : {} full_keys = @store.keys "#{locale}.#{key}.*" if full_keys.empty? TranslationPanel.push key if TranslationPanel.show? nil else keys = full_keys.map{ |full_key| full_key.partition("#{locale}.")[2] } TranslationPanel.push keys if TranslationPanel.show? flatten_result = full_keys.inject({}) do |result, full_key| value = @store[full_key] value = ActiveSupport::JSON.decode(value) if value result.merge full_key.partition("#{locale}.#{key}.")[2] => value end expand_keys(pluralization_result.merge(flatten_result)).deep_symbolize_keys end end end