Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a uniform api between Simple, KeyValue and Chain stores #109

Merged
merged 4 commits into from
Dec 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions lib/i18n/backend/chain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ def initialize(*backends)
self.backends = backends
end

def initialized?
backends.all? do |backend|
backend.instance_eval do
return false unless initialized?
end
end
true
end

def reload!
backends.each { |backend| backend.reload! }
end
Expand Down Expand Up @@ -74,6 +83,19 @@ def localize(locale, object, format = :default, options = EMPTY_HASH)
end

protected
def init_translations
backends.each do |backend|
backend.send(:init_translations)
end
end

def translations
backends.first.instance_eval do
init_translations unless initialized?
translations
end
end

def namespace_lookup?(result, options)
result.is_a?(Hash) && !options.has_key?(:count)
end
Expand Down
24 changes: 24 additions & 0 deletions lib/i18n/backend/key_value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ def initialize(store, subtrees=true)
@store, @subtrees = store, subtrees
end

def initialized?
!@store.nil?
end

def store_translations(locale, data, options = EMPTY_HASH)
escape = options.fetch(:escape, true)
flatten_translations(locale, data, escape, @subtrees).each do |key, value|
Expand Down Expand Up @@ -105,6 +109,26 @@ def available_locales

protected

# Queries the translations from the key-value store and converts
# them into a hash such as the one returned from loading the
# haml files
def translations
@translations = @store.keys.clone.map do |main_key|
main_value = JSON.decode(@store[main_key])
main_key.to_s.split(".").reverse.inject(main_value) do |value, key|
{key.to_sym => value}
end
end.inject{|hash, elem| hash.deep_merge!(elem)}.deep_symbolize_keys
end

def init_translations
# NO OP
# This call made also inside Simple Backend and accessed by
# other plugins like I18n-js and babilu and
# to use it along with the Chain backend we need to
# provide a uniform API even for protected methods :S
end

def subtrees?
@subtrees
end
Expand Down
33 changes: 33 additions & 0 deletions test/backend/chain_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,39 @@ def setup
I18n.backend.store_translations :foo, {:bar => :baz}, {:option => 'persists'}
end

test 'store should call initialize on all backends and return true if all initialized' do
@first.send :init_translations
@second.send :init_translations
assert I18n.backend.initialized?
end

test 'store should call initialize on all backends and return false if one not initialized' do
@first.reload!
@second.send :init_translations
assert !I18n.backend.initialized?
end

test 'should reload all backends' do
@first.send :init_translations
@second.send :init_translations
I18n.backend.reload!
assert !@first.initialized?
assert !@second.initialized?
end

test 'should be able to get all translations of the first backend' do
assert_equal I18n.backend.send(:translations),
en: {
foo: 'Foo',
formats: {
short: 'short',
subformats: { short: 'short' }
},
plural_1: { one: "%{count}" },
dates: { a: 'A' }
}
end

protected

def backend(translations)
Expand Down
13 changes: 13 additions & 0 deletions test/backend/key_value_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ def assert_flattens(expected, nested, escape=true, subtree=true)
end
end

test 'initialized? checks that a store is available' do
setup_backend!
I18n.backend.reload!
assert_equal I18n.backend.initialized?, true
end

test 'translations gets the translations from the store' do
setup_backend!
I18n.backend.send(:translations)
expected = { :en => {:foo => { :bar => 'bar', :baz => 'baz' }} }
assert_equal expected, translations
end

test "subtrees enabled: given incomplete pluralization data it raises I18n::InvalidPluralizationData" do
setup_backend!
store_translations(:en, :bar => { :one => "One" })
Expand Down