PERF: bypass slow locale lookups in various cases

Previously as soon as any override was defined we would regress to the slow
path for locale lookups. Additionally if `raise: true` was specified which
rails likes to add in views we would bypass the cache

The new design manages to use the fast path for many more cases
This commit is contained in:
Sam Saffron
2019-06-05 14:30:25 +10:00
parent 78509eacb7
commit 6d8eb9c144
2 changed files with 52 additions and 11 deletions

View File

@ -24,6 +24,15 @@ describe "translate accelerator" do
expect(override.persisted?).to eq(true)
end
it "supports raising if requested, and cache bypasses" do
expect { I18n.t('i_am_an_unknown_key99', raise: true) }.to raise_error(I18n::MissingTranslationData)
orig = I18n.t('i_am_an_unknown_key99')
expect(I18n.t('i_am_an_unknown_key99').object_id).to eq(orig.object_id)
expect(I18n.t('i_am_an_unknown_key99')).to eq("translation missing: en_US.i_am_an_unknown_key99")
end
it "overrides for both string and symbol keys" do
key = 'user.email.not_allowed'
text_overriden = 'foobar'
@ -124,6 +133,7 @@ describe "translate accelerator" do
I18n.overrides_disabled do
expect(I18n.t('title')).to eq(orig_title)
end
expect(I18n.t('title')).to eq('overridden title')
end