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:
parent
11a9e0fc70
commit
bd969332e0
|
@ -12,6 +12,7 @@ export default Ember.TextArea.extend({
|
||||||
|
|
||||||
@observes("value")
|
@observes("value")
|
||||||
_updateAutosize() {
|
_updateAutosize() {
|
||||||
|
this.element.value = this.value;
|
||||||
const evt = document.createEvent("Event");
|
const evt = document.createEvent("Event");
|
||||||
evt.initEvent("autosize:update", true, false);
|
evt.initEvent("autosize:update", true, false);
|
||||||
this.element.dispatchEvent(evt);
|
this.element.dispatchEvent(evt);
|
||||||
|
|
|
@ -109,7 +109,7 @@ class Admin::SiteTextsController < Admin::AdminController
|
||||||
value = override&.first
|
value = override&.first
|
||||||
end
|
end
|
||||||
|
|
||||||
value ||= I18n.t(key, default: '')
|
value ||= I18n.t(key)
|
||||||
{ id: key, value: value }
|
{ id: key, value: value }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -65,8 +65,10 @@ class TranslationOverride < ActiveRecord::Base
|
||||||
private
|
private
|
||||||
|
|
||||||
def check_interpolation_keys
|
def check_interpolation_keys
|
||||||
|
transformed_key = transform_pluralized_key(translation_key)
|
||||||
|
|
||||||
original_text = I18n.overrides_disabled do
|
original_text = I18n.overrides_disabled do
|
||||||
I18n.t(translation_key, locale: :en)
|
I18n.t(transformed_key, locale: :en)
|
||||||
end
|
end
|
||||||
|
|
||||||
if original_text
|
if original_text
|
||||||
|
@ -76,7 +78,7 @@ class TranslationOverride < ActiveRecord::Base
|
||||||
custom_interpolation_keys = []
|
custom_interpolation_keys = []
|
||||||
|
|
||||||
CUSTOM_INTERPOLATION_KEYS_WHITELIST.select do |key, value|
|
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
|
custom_interpolation_keys = value
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -96,6 +98,10 @@ class TranslationOverride < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def transform_pluralized_key(key)
|
||||||
|
match = key.match(/(.*)\.(zero|two|few|many)$/)
|
||||||
|
match ? match.to_a.second + '.other' : key
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# == Schema Information
|
# == Schema Information
|
||||||
|
|
|
@ -7,6 +7,7 @@ describe TranslationOverride do
|
||||||
describe '#value' do
|
describe '#value' do
|
||||||
before do
|
before do
|
||||||
I18n.backend.store_translations(I18n.locale, some_key: '%{first} %{second}')
|
I18n.backend.store_translations(I18n.locale, some_key: '%{first} %{second}')
|
||||||
|
I18n.backend.store_translations(:en, something: { one: '%{key1} %{key2}', other: '%{key3} %{key4}' })
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'when interpolation keys are missing' do
|
describe 'when interpolation keys are missing' do
|
||||||
|
@ -36,6 +37,40 @@ describe TranslationOverride do
|
||||||
end
|
end
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue