Skip to content

Commit

Permalink
Fix bulk lookups in Chain backend.
Browse files Browse the repository at this point in the history
Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information
lawrencepit authored and josevalim committed Jun 8, 2010
1 parent 80c4150 commit 9d89d7b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
25 changes: 15 additions & 10 deletions lib/i18n/backend/chain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,25 @@ def available_locales
end

def translate(locale, key, options = {})
default = options.delete(:default)
namespace = {}
backends.each do |backend|
begin
options.update(:default => default) if default and backend == backends.last
translation = backend.translate(locale, key, options)
if namespace_lookup?(translation, options)
namespace.update(translation)
elsif !translation.nil?
return translation
default = options.delete(:default)
begin
backends.each do |backend|
begin
options.update(:default => default) if default and backend == backends.last
translation = backend.translate(locale, key, options)
if namespace_lookup?(translation, options)
namespace.update(translation)
elsif !translation.nil?
return translation
end
rescue MissingTranslationData
end
rescue MissingTranslationData
end
ensure
options.update(:default => default)
end

return namespace unless namespace.empty?
raise(I18n::MissingTranslationData.new(locale, key, options))
end
Expand Down
2 changes: 2 additions & 0 deletions test/backend/chain_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ def setup

test "bulk lookup collects results from all backends" do
assert_equal ['Foo', 'Bar'], I18n.t([:foo, :bar])
assert_equal ['Foo', 'Bar', 'Bah'], I18n.t([:foo, :bar, :bah], :default => 'Bah')
assert_equal [{ :short => 'short', :long => 'long' }, { :one => 'one' }, 'Bah'], I18n.t([:formats, :plural_2, :bah], :default => 'Bah')
end

protected
Expand Down

0 comments on commit 9d89d7b

Please sign in to comment.