From 2e875d3ccadb2950153a81cddaf6fea07ffbd393 Mon Sep 17 00:00:00 2001 From: Gerhard Schlager Date: Sat, 13 Feb 2016 23:01:05 +0100 Subject: [PATCH] FIX: Use fallback locales when searching for translations --- lib/i18n/backend/discourse_i18n.rb | 12 ++++++++++-- spec/components/discourse_i18n_spec.rb | 5 +++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/i18n/backend/discourse_i18n.rb b/lib/i18n/backend/discourse_i18n.rb index c4bccbf12..8b2ea2375 100644 --- a/lib/i18n/backend/discourse_i18n.rb +++ b/lib/i18n/backend/discourse_i18n.rb @@ -43,7 +43,13 @@ module I18n end def search(locale, query) - find_results(/#{query}/i, {}, translations[locale]) + results = {} + + fallbacks(locale).each do |fallback| + find_results(/#{query}/i, results, translations[fallback]) + end + + results end protected @@ -54,7 +60,9 @@ module I18n k = k_sym.to_s key_path = path ? "#{path}.#{k}" : k if v.is_a?(String) - results[key_path] = v if key_path =~ regexp || v =~ regexp + unless results.has_key?(key_path) + results[key_path] = v if key_path =~ regexp || v =~ regexp + end elsif v.is_a?(Hash) find_results(regexp, results, v, key_path) end diff --git a/spec/components/discourse_i18n_spec.rb b/spec/components/discourse_i18n_spec.rb index 40dbf4a2e..33722e92b 100644 --- a/spec/components/discourse_i18n_spec.rb +++ b/spec/components/discourse_i18n_spec.rb @@ -40,6 +40,11 @@ describe I18n::Backend::DiscourseI18n do expect(results['items.other']).to eq('%{count} items') end + it 'uses fallback locales for searching' do + expect(backend.search(:de, 'bar')).to eq({'bar' => 'Bar in :de'}) + expect(backend.search(:de, 'foo')).to eq({'foo' => 'Foo in :en'}) + end + describe '#exists?' do it 'returns true when a key is given that exists' do expect(backend.exists?(:de, :bar)).to eq(true)