FIX: Translation overrides were not cached by locale properly.

This commit is contained in:
Guo Xiang Tan 2017-07-07 12:28:00 +09:00
parent 198f308f7b
commit a4399c2eab
2 changed files with 22 additions and 3 deletions

View File

@ -104,10 +104,9 @@ module I18n
site = RailsMultisite::ConnectionManagement.current_db
by_site = @overrides_by_site[site]
by_site ||= {}
if by_site.nil? || !by_site.has_key?(locale)
by_site = @overrides_by_site[site] = {}
if !by_site.has_key?(locale)
# Load overrides
translations_overrides = TranslationOverride.where(locale: locale).pluck(:translation_key, :value, :compiled_js)
@ -119,6 +118,8 @@ module I18n
by_locale[tuple[0]] = tuple[2] || tuple[1]
end
end
@overrides_by_site[site] = by_site
end
by_site[locale].with_indifferent_access

View File

@ -14,4 +14,22 @@ describe "translate accelerator" do
expect(I18n.t(key.to_sym)).to eq(text_overriden)
end
describe '.overrides_by_locale' do
it 'should cache overrides for each locale' do
TranslationOverride.upsert!('en', 'got', "summer")
TranslationOverride.upsert!('zh_TW', 'got', "冬季")
I18n.backend.store_translations(:en, got: 'winter')
I18n.overrides_by_locale('en')
I18n.overrides_by_locale('zh_TW')
expect(I18n.instance_variable_get(:@overrides_by_site)).to eq(
"default" => {
"en" => { "got" => "summer" },
"zh_TW" => { "got" => "冬季" }
}
)
end
end
end