From bd969332e0b4914809e824450d565483e6d69f75 Mon Sep 17 00:00:00 2001 From: Mark VanLandingham Date: Thu, 17 Oct 2019 11:34:07 -0500 Subject: [PATCH] FIX: Display site text overrides for non '_MF' keys (#8189) FIX: Transform pluralized keys to `.other`, to check valid interpolation --- .../components/expanding-text-area.js.es6 | 1 + .../admin/site_texts_controller.rb | 2 +- app/models/translation_override.rb | 10 ++++-- spec/models/translation_override_spec.rb | 35 +++++++++++++++++++ 4 files changed, 45 insertions(+), 3 deletions(-) diff --git a/app/assets/javascripts/discourse/components/expanding-text-area.js.es6 b/app/assets/javascripts/discourse/components/expanding-text-area.js.es6 index 064764713fc..7008eb1d013 100644 --- a/app/assets/javascripts/discourse/components/expanding-text-area.js.es6 +++ b/app/assets/javascripts/discourse/components/expanding-text-area.js.es6 @@ -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); diff --git a/app/controllers/admin/site_texts_controller.rb b/app/controllers/admin/site_texts_controller.rb index 756974c109e..ef594471d9f 100644 --- a/app/controllers/admin/site_texts_controller.rb +++ b/app/controllers/admin/site_texts_controller.rb @@ -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 diff --git a/app/models/translation_override.rb b/app/models/translation_override.rb index 9dcf7189192..95e4183589b 100644 --- a/app/models/translation_override.rb +++ b/app/models/translation_override.rb @@ -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 diff --git a/spec/models/translation_override_spec.rb b/spec/models/translation_override_spec.rb index cbbf1d8a873..bdb61989bf9 100644 --- a/spec/models/translation_override_spec.rb +++ b/spec/models/translation_override_spec.rb @@ -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