Merge pull request #4011 from gschlager/i18n-overrides

Fixes for I18n and translation overrides
This commit is contained in:
Robin Ward 2016-02-22 18:34:45 -05:00
commit 841065db2f
3 changed files with 28 additions and 6 deletions

View file

@ -28,6 +28,7 @@ module I18n
@overrides_by_site = {}
reload_no_cache!
ensure_all_loaded!
end
LOAD_MUTEX = Mutex.new
@ -105,8 +106,7 @@ module I18n
by_site = @overrides_by_site[site]
by_locale = nil
unless by_site
unless by_site && by_site.has_key?(locale)
by_site = @overrides_by_site[site] = {}
# Load overrides

View file

@ -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

View file

@ -15,6 +15,7 @@ describe I18n::Backend::DiscourseI18n do
end
after do
I18n.locale = :en
I18n.reload!
end
@ -40,6 +41,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)
@ -73,15 +79,23 @@ describe I18n::Backend::DiscourseI18n do
end
describe 'with overrides' do
it 'returns the overriden key' do
it 'returns the overridden key' do
TranslationOverride.upsert!('en', 'foo', 'Overwritten foo')
expect(I18n.translate('foo')).to eq('Overwritten foo')
TranslationOverride.upsert!('en', 'foo', 'new value')
I18n.reload!
expect(I18n.translate('foo')).to eq('new value')
end
it 'returns the overridden key after switching the locale' do
TranslationOverride.upsert!('en', 'foo', 'Overwritten foo in EN')
TranslationOverride.upsert!('de', 'foo', 'Overwritten foo in DE')
expect(I18n.translate('foo')).to eq('Overwritten foo in EN')
I18n.locale = :de
expect(I18n.translate('foo')).to eq('Overwritten foo in DE')
end
it "can be searched" do
TranslationOverride.upsert!('en', 'wat', 'Overwritten value')
expect(I18n.search('wat', backend: backend)).to eq({'wat' => 'Overwritten value'})