Class: I18n::Backend::Redis

Inherits:
Object
  • Object
show all
Includes:
Base, Flatten
Defined in:
lib/i18n/backend/redis.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*addresses) ⇒ Redis

Instantiate the store.

Example:

RedisStore.new
  # => host: localhost,   port: 6379,  db: 0

RedisStore.new "example.com"
  # => host: example.com, port: 6379,  db: 0

RedisStore.new "example.com:23682"
  # => host: example.com, port: 23682, db: 0

RedisStore.new "example.com:23682/1"
  # => host: example.com, port: 23682, db: 1

RedisStore.new "example.com:23682/1/theplaylist"
  # => host: example.com, port: 23682, db: 1, namespace: theplaylist

RedisStore.new "localhost:6379/0", "localhost:6380/0"
  # => instantiate a cluster


29
30
31
# File 'lib/i18n/backend/redis.rb', line 29

def initialize(*addresses)
  @store = ::Redis::Store::Factory.create(addresses)
end

Instance Attribute Details

#storeObject

Returns the value of attribute store.



7
8
9
# File 'lib/i18n/backend/redis.rb', line 7

def store
  @store
end

Instance Method Details

#available_localesObject



50
51
52
53
54
55
56
# File 'lib/i18n/backend/redis.rb', line 50

def available_locales
  locales = @store.keys.map { |k| k =~ /\./; $` }
  locales.uniq!
  locales.compact!
  locales.map! { |k| k.to_sym }
  locales
end

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



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/i18n/backend/redis.rb', line 38

def store_translations(locale, data, options = {})
  escape = options.fetch(:escape, true)
  flatten_translations(locale, data, escape, false).each do |key, value|
    case value
    when Proc
      raise "Key-value stores cannot handle procs"
    else
      @store.set "#{locale}.#{key}", value
    end
  end
end

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



33
34
35
36
# File 'lib/i18n/backend/redis.rb', line 33

def translate(locale, key, options = {})
  options[:resolve] ||= false
  super locale, key, options
end