FIX: Display site text overrides for non '_MF' keys (#8189)

FIX: Transform pluralized keys to `.other`, to check valid interpolation
This commit is contained in:
Mark VanLandingham 2019-10-17 11:34:07 -05:00 committed by Gerhard Schlager
parent 11a9e0fc70
commit bd969332e0
4 changed files with 45 additions and 3 deletions

View File

@ -12,6 +12,7 @@ export default Ember.TextArea.extend({
@observes("value")
_updateAutosize() {
this.element.value = this.value;
const evt = document.createEvent("Event");
evt.initEvent("autosize:update", true, false);
this.element.dispatchEvent(evt);

View File

@ -109,7 +109,7 @@ class Admin::SiteTextsController < Admin::AdminController
value = override&.first
end
value ||= I18n.t(key, default: '')
value ||= I18n.t(key)
{ id: key, value: value }
end

View File

@ -65,8 +65,10 @@ class TranslationOverride < ActiveRecord::Base
private
def check_interpolation_keys
transformed_key = transform_pluralized_key(translation_key)
original_text = I18n.overrides_disabled do
I18n.t(translation_key, locale: :en)
I18n.t(transformed_key, locale: :en)
end
if original_text
@ -76,7 +78,7 @@ class TranslationOverride < ActiveRecord::Base
custom_interpolation_keys = []
CUSTOM_INTERPOLATION_KEYS_WHITELIST.select do |key, value|
if self.translation_key.start_with?(key)
if transformed_key.start_with?(key)
custom_interpolation_keys = value
end
end
@ -96,6 +98,10 @@ class TranslationOverride < ActiveRecord::Base
end
end
def transform_pluralized_key(key)
match = key.match(/(.*)\.(zero|two|few|many)$/)
match ? match.to_a.second + '.other' : key
end
end
# == Schema Information

View File

@ -7,6 +7,7 @@ describe TranslationOverride do
describe '#value' do
before do
I18n.backend.store_translations(I18n.locale, some_key: '%{first} %{second}')
I18n.backend.store_translations(:en, something: { one: '%{key1} %{key2}', other: '%{key3} %{key4}' })
end
describe 'when interpolation keys are missing' do
@ -36,6 +37,40 @@ describe TranslationOverride do
end
end
end
describe 'pluralized keys' do
describe 'valid keys' do
it 'converts zero to other' do
translation_override = TranslationOverride.upsert!(I18n.locale, 'something.zero', '%{key3} %{key4} hello')
expect(translation_override.errors.full_messages).to eq([])
end
it 'converts two to other' do
translation_override = TranslationOverride.upsert!(I18n.locale, 'something.two', '%{key3} %{key4} hello')
expect(translation_override.errors.full_messages).to eq([])
end
it 'converts few to other' do
translation_override = TranslationOverride.upsert!(I18n.locale, 'something.few', '%{key3} %{key4} hello')
expect(translation_override.errors.full_messages).to eq([])
end
it 'converts many to other' do
translation_override = TranslationOverride.upsert!(I18n.locale, 'something.many', '%{key3} %{key4} hello')
expect(translation_override.errors.full_messages).to eq([])
end
end
describe 'invalid keys' do
it "does not transform 'tonz'" do
translation_override = TranslationOverride.upsert!(I18n.locale, 'something.tonz', '%{key3} %{key4} hello')
expect(translation_override.errors.full_messages).to include(I18n.t(
'activerecord.errors.models.translation_overrides.attributes.value.invalid_interpolation_keys',
keys: 'key3, key4'
))
end
end
end
end
end