Permalink
Browse files

Added a KeyValue backend (an API test is failing and need to add some…

… unit tests to the backend storing process).
  • Loading branch information...
1 parent 928fdb4 commit 28ca5f53ade7f545f8c0804e93564d4686b416a4 @josevalim josevalim committed Apr 28, 2010
Showing with 111 additions and 0 deletions.
  1. +1 −0 lib/i18n/backend.rb
  2. +79 −0 lib/i18n/backend/key_value.rb
  3. +31 −0 test/api/key_value_test.rb
View
1 lib/i18n/backend.rb
@@ -12,6 +12,7 @@ module Backend
autoload :Gettext, 'i18n/backend/gettext'
autoload :Helpers, 'i18n/backend/helpers'
autoload :InterpolationCompiler, 'i18n/backend/interpolation_compiler'
+ autoload :KeyValue, 'i18n/backend/key_value'
autoload :Metadata, 'i18n/backend/metadata'
autoload :Pluralization, 'i18n/backend/pluralization'
autoload :Simple, 'i18n/backend/simple'
View
79 lib/i18n/backend/key_value.rb
@@ -0,0 +1,79 @@
+# encoding: utf-8
+require 'i18n/backend/base'
+require 'active_support/json'
+
+module I18n
+ module Backend
+ # This is a basic backend for key value stores. It receives on
+ # initialization the store, which should respond to three methods:
+ #
+ # * store#[](key) - Used to get a value
+ # * store#[]=(key, value) - Used to set a value
+ # * store#keys - Used to get all keys
+ #
+ # Since these stores only supports string, all values are converted
+ # to JSON before being stored, allowing it to also store booleans,
+ # hashes and arrays. However, this store does not support Procs.
+ #
+ # As the ActiveRecord backend, Symbols are just supported when loading
+ # translations from the filesystem or through explicit store translations.
+ #
+ # Also, avoid calling I18n.available_locales since it's a somehow
+ # expensive operation in most stores.
+ #
+ # == Example
+ #
+ # To setup I18n to use TokyoCabinet in memory is quite straightforward:
+ #
+ # require 'rufus/tokyo/cabinet' # gem install rufus-tokyo
+ # I18n.backend = I18n::Backend::KeyValue.new(Rufus::Tokyo::Cabinet.new('*'))
+ #
+ class KeyValue
+ attr_accessor :store
+
+ include Base, Flatten
+
+ def initialize(store)
+ @store = store
+ end
+
+ # Mute reload! since we really don't want to clean the database.
+ def reload!
+ end
+
+ def available_locales
+ @store.keys.map { |k| k.split(".").first.to_sym }.uniq
+ end
+
+ protected
+
+ def lookup(locale, key, scope = [], options = {})
+ key = normalize_keys(locale, key, scope, options[:separator])
+ value = @store["#{locale}.#{key}"]
+ value = ActiveSupport::JSON.decode(value) if value
+ value.is_a?(Hash) ? deep_symbolize_keys(value) : value
+ end
+
+ def merge_translations(locale, data, options = {})
+ flatten_translations(locale, data, true).each do |key, value|
+ key = "#{locale}.#{key}"
+
+ case value
+ when Hash
+ old_value = @store[key]
+ if old_value
+ old_value = ActiveSupport::JSON.decode(old_value)
+ value = old_value.merge(value) if old_value.is_a?(Hash)
+ end
+ when Proc
+ raise "Key-value stores cannot handle procs"
+ when Symbol
+ value = nil
+ end
+
+ @store[key] = ActiveSupport::JSON.encode(value) unless value.nil?
+ end
+ end
+ end
+ end
+end
View
31 test/api/key_value_test.rb
@@ -0,0 +1,31 @@
+# encoding: utf-8
+$:.unshift(File.expand_path(File.dirname(__FILE__) + '/../')); $:.uniq!
+require 'test_helper'
+require 'api'
+require 'rubygems'
+require 'rufus/tokyo'
+
+$store = Rufus::Tokyo::Cabinet.new('*')
+
+class I18nKeyValueApiTest < Test::Unit::TestCase
+ include Tests::Api::Basics
+ include Tests::Api::Defaults
+ include Tests::Api::Interpolation
+ include Tests::Api::Link
+ include Tests::Api::Lookup
+ include Tests::Api::Pluralization
+ # include Tests::Api::Procs
+ include Tests::Api::Localization::Date
+ include Tests::Api::Localization::DateTime
+ include Tests::Api::Localization::Time
+ # include Tests::Api::Localization::Procs
+
+ def setup
+ I18n.backend = I18n::Backend::KeyValue.new($store)
+ super
+ end
+
+ test "make sure we use the KeyValue backend" do
+ assert_equal I18n::Backend::KeyValue, I18n.backend.class
+ end
+end

0 comments on commit 28ca5f5

Please sign in to comment.