From ce060e2b86a8940f1f3bb57818dd36e79ae34cdf Mon Sep 17 00:00:00 2001 From: Gerhard Schlager Date: Mon, 22 Jan 2018 14:42:12 +0100 Subject: [PATCH] FIX: Server didn't use default_locale as fallback locale --- config/initializers/100-i18n.rb | 3 +++ lib/i18n/backend/discourse_i18n.rb | 2 +- lib/i18n/backend/fallback_locale_list.rb | 10 ++++++++ spec/components/discourse_i18n_spec.rb | 30 +++++++++++++++++------- 4 files changed, 36 insertions(+), 9 deletions(-) create mode 100644 lib/i18n/backend/fallback_locale_list.rb diff --git a/config/initializers/100-i18n.rb b/config/initializers/100-i18n.rb index 37a0dd050cc..0f5df274eeb 100644 --- a/config/initializers/100-i18n.rb +++ b/config/initializers/100-i18n.rb @@ -1,7 +1,10 @@ # order: after 02-freedom_patches.rb require 'i18n/backend/discourse_i18n' +require 'i18n/backend/fallback_locale_list' + I18n.backend = I18n::Backend::DiscourseI18n.new +I18n.fallbacks = I18n::Backend::FallbackLocaleList.new I18n.config.missing_interpolation_argument_handler = proc { throw(:exception) } I18n.init_accelerator! diff --git a/lib/i18n/backend/discourse_i18n.rb b/lib/i18n/backend/discourse_i18n.rb index 375b72b8cc2..1f33dd60e0d 100644 --- a/lib/i18n/backend/discourse_i18n.rb +++ b/lib/i18n/backend/discourse_i18n.rb @@ -25,7 +25,7 @@ module I18n end def fallbacks(locale) - [locale, SiteSetting.default_locale.to_sym, :en].uniq.compact + I18n.fallbacks[locale] end def exists?(locale, key) diff --git a/lib/i18n/backend/fallback_locale_list.rb b/lib/i18n/backend/fallback_locale_list.rb new file mode 100644 index 00000000000..29d9700dbaa --- /dev/null +++ b/lib/i18n/backend/fallback_locale_list.rb @@ -0,0 +1,10 @@ +module I18n + module Backend + # Configure custom fallback order + class FallbackLocaleList < Hash + def [](locale) + [locale, SiteSetting.default_locale.to_sym, :en].uniq.compact + end + end + end +end diff --git a/spec/components/discourse_i18n_spec.rb b/spec/components/discourse_i18n_spec.rb index 0074b2e464c..72dfc368ce5 100644 --- a/spec/components/discourse_i18n_spec.rb +++ b/spec/components/discourse_i18n_spec.rb @@ -11,7 +11,7 @@ describe I18n::Backend::DiscourseI18n do backend.store_translations(:en, foo: 'Foo in :en', bar: 'Bar in :en', wat: "Hello %{count}") backend.store_translations(:en, items: { one: 'one item', other: "%{count} items" }) backend.store_translations(:de, bar: 'Bar in :de') - backend.store_translations(:'de-AT', baz: 'Baz in :de-AT') + backend.store_translations(:ru, baz: 'Baz in :ru') end after do @@ -41,9 +41,23 @@ 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') + describe 'fallbacks' do + 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 + + it 'uses fallback locales for translating' do + expect(backend.translate(:de, 'bar')).to eq('Bar in :de') + expect(backend.translate(:de, 'foo')).to eq('Foo in :en') + end + + it 'uses default_locale as fallback when key exists' do + SiteSetting.default_locale = 'ru' + expect(backend.translate(:de, 'bar')).to eq('Bar in :de') + expect(backend.translate(:de, 'baz')).to eq('Baz in :ru') + expect(backend.translate(:de, 'foo')).to eq('Foo in :en') + end end describe '#exists?' do @@ -58,23 +72,23 @@ describe I18n::Backend::DiscourseI18n do it 'returns true when an existing key and an existing locale is given' do expect(backend.exists?(:en, :foo)).to eq(true) expect(backend.exists?(:de, :bar)).to eq(true) - expect(backend.exists?(:'de-AT', :baz)).to eq(true) + expect(backend.exists?(:ru, :baz)).to eq(true) end it 'returns false when a non-existing key and an existing locale is given' do expect(backend.exists?(:en, :bogus)).to eq(false) expect(backend.exists?(:de, :bogus)).to eq(false) - expect(backend.exists?(:'de-AT', :bogus)).to eq(false) + expect(backend.exists?(:ru, :bogus)).to eq(false) end it 'returns true when a key is given which is missing from the given locale and exists in a fallback locale' do expect(backend.exists?(:de, :foo)).to eq(true) - expect(backend.exists?(:'de-AT', :foo)).to eq(true) + expect(backend.exists?(:ru, :foo)).to eq(true) end it 'returns true when a key is given which is missing from the given locale and all its fallback locales' do expect(backend.exists?(:de, :baz)).to eq(false) - expect(backend.exists?(:'de-AT', :bogus)).to eq(false) + expect(backend.exists?(:ru, :bogus)).to eq(false) end end