mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-11-27 17:46:05 -05:00
Merge pull request #4011 from gschlager/i18n-overrides
Fixes for I18n and translation overrides
This commit is contained in:
commit
841065db2f
3 changed files with 28 additions and 6 deletions
|
@ -28,6 +28,7 @@ module I18n
|
||||||
@overrides_by_site = {}
|
@overrides_by_site = {}
|
||||||
|
|
||||||
reload_no_cache!
|
reload_no_cache!
|
||||||
|
ensure_all_loaded!
|
||||||
end
|
end
|
||||||
|
|
||||||
LOAD_MUTEX = Mutex.new
|
LOAD_MUTEX = Mutex.new
|
||||||
|
@ -105,8 +106,7 @@ module I18n
|
||||||
|
|
||||||
by_site = @overrides_by_site[site]
|
by_site = @overrides_by_site[site]
|
||||||
|
|
||||||
by_locale = nil
|
unless by_site && by_site.has_key?(locale)
|
||||||
unless by_site
|
|
||||||
by_site = @overrides_by_site[site] = {}
|
by_site = @overrides_by_site[site] = {}
|
||||||
|
|
||||||
# Load overrides
|
# Load overrides
|
||||||
|
|
|
@ -43,7 +43,13 @@ module I18n
|
||||||
end
|
end
|
||||||
|
|
||||||
def search(locale, query)
|
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
|
end
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
@ -54,7 +60,9 @@ module I18n
|
||||||
k = k_sym.to_s
|
k = k_sym.to_s
|
||||||
key_path = path ? "#{path}.#{k}" : k
|
key_path = path ? "#{path}.#{k}" : k
|
||||||
if v.is_a?(String)
|
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)
|
elsif v.is_a?(Hash)
|
||||||
find_results(regexp, results, v, key_path)
|
find_results(regexp, results, v, key_path)
|
||||||
end
|
end
|
||||||
|
|
|
@ -15,6 +15,7 @@ describe I18n::Backend::DiscourseI18n do
|
||||||
end
|
end
|
||||||
|
|
||||||
after do
|
after do
|
||||||
|
I18n.locale = :en
|
||||||
I18n.reload!
|
I18n.reload!
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -40,6 +41,11 @@ describe I18n::Backend::DiscourseI18n do
|
||||||
expect(results['items.other']).to eq('%{count} items')
|
expect(results['items.other']).to eq('%{count} items')
|
||||||
end
|
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
|
describe '#exists?' do
|
||||||
it 'returns true when a key is given that exists' do
|
it 'returns true when a key is given that exists' do
|
||||||
expect(backend.exists?(:de, :bar)).to eq(true)
|
expect(backend.exists?(:de, :bar)).to eq(true)
|
||||||
|
@ -73,15 +79,23 @@ describe I18n::Backend::DiscourseI18n do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'with overrides' do
|
describe 'with overrides' do
|
||||||
it 'returns the overriden key' do
|
it 'returns the overridden key' do
|
||||||
TranslationOverride.upsert!('en', 'foo', 'Overwritten foo')
|
TranslationOverride.upsert!('en', 'foo', 'Overwritten foo')
|
||||||
expect(I18n.translate('foo')).to eq('Overwritten foo')
|
expect(I18n.translate('foo')).to eq('Overwritten foo')
|
||||||
|
|
||||||
TranslationOverride.upsert!('en', 'foo', 'new value')
|
TranslationOverride.upsert!('en', 'foo', 'new value')
|
||||||
I18n.reload!
|
|
||||||
expect(I18n.translate('foo')).to eq('new value')
|
expect(I18n.translate('foo')).to eq('new value')
|
||||||
end
|
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
|
it "can be searched" do
|
||||||
TranslationOverride.upsert!('en', 'wat', 'Overwritten value')
|
TranslationOverride.upsert!('en', 'wat', 'Overwritten value')
|
||||||
expect(I18n.search('wat', backend: backend)).to eq({'wat' => 'Overwritten value'})
|
expect(I18n.search('wat', backend: backend)).to eq({'wat' => 'Overwritten value'})
|
||||||
|
|
Loading…
Reference in a new issue