FIX: An overridden text of a non-existent plural key resulted in error
When there is an overridden text in the database that belongs to a pluralized key which doesn't exist in English anymore, the Customize Texts admin page was unusable. This stops those keys from ever being returned by a search.
This commit is contained in:
parent
ebb389ef8a
commit
d516e492a4
|
@ -112,7 +112,7 @@ class Admin::SiteTextsController < Admin::AdminController
|
||||||
value = override&.first
|
value = override&.first
|
||||||
end
|
end
|
||||||
|
|
||||||
value ||= I18n.t(key)
|
value ||= I18n.t(key, default: '')
|
||||||
{ id: key, value: value }
|
{ id: key, value: value }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -149,6 +149,8 @@ class Admin::SiteTextsController < Admin::AdminController
|
||||||
results = []
|
results = []
|
||||||
|
|
||||||
translations.each do |key, value|
|
translations.each do |key, value|
|
||||||
|
next unless I18n.exists?(key, :en)
|
||||||
|
|
||||||
if value&.is_a?(Hash)
|
if value&.is_a?(Hash)
|
||||||
value = fix_plural_keys(key, value)
|
value = fix_plural_keys(key, value)
|
||||||
value.each do |plural_key, plural_value|
|
value.each do |plural_key, plural_value|
|
||||||
|
@ -167,7 +169,7 @@ class Admin::SiteTextsController < Admin::AdminController
|
||||||
plural_keys = I18n.t('i18n.plural.keys')
|
plural_keys = I18n.t('i18n.plural.keys')
|
||||||
return value if value.keys.size == plural_keys.size && plural_keys.all? { |k| value.key?(k) }
|
return value if value.keys.size == plural_keys.size && plural_keys.all? { |k| value.key?(k) }
|
||||||
|
|
||||||
fallback_value = I18n.t(key, locale: :en)
|
fallback_value = I18n.t(key, locale: :en, default: {})
|
||||||
plural_keys.map do |k|
|
plural_keys.map do |k|
|
||||||
[k, value[k] || fallback_value[k] || fallback_value[:other]]
|
[k, value[k] || fallback_value[k] || fallback_value[:other]]
|
||||||
end.to_h
|
end.to_h
|
||||||
|
|
|
@ -91,6 +91,20 @@ RSpec.describe Admin::SiteTextsController do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'does not return overrides for keys that do not exist in English' do
|
||||||
|
SiteSetting.default_locale = :ru
|
||||||
|
TranslationOverride.create!(locale: :ru, translation_key: 'missing_plural_key.one', value: 'ONE')
|
||||||
|
TranslationOverride.create!(locale: :ru, translation_key: 'another_missing_key', value: 'foo')
|
||||||
|
|
||||||
|
get "/admin/customize/site_texts.json", params: { q: 'missing_plural_key' }
|
||||||
|
expect(response.status).to eq(200)
|
||||||
|
expect(JSON.parse(response.body)['site_texts']).to be_empty
|
||||||
|
|
||||||
|
get "/admin/customize/site_texts.json", params: { q: 'another_missing_key' }
|
||||||
|
expect(response.status).to eq(200)
|
||||||
|
expect(JSON.parse(response.body)['site_texts']).to be_empty
|
||||||
|
end
|
||||||
|
|
||||||
context 'plural keys' do
|
context 'plural keys' do
|
||||||
before do
|
before do
|
||||||
I18n.backend.store_translations(:en, colour: { one: '%{count} colour', other: '%{count} colours' })
|
I18n.backend.store_translations(:en, colour: { one: '%{count} colour', other: '%{count} colours' })
|
||||||
|
|
Loading…
Reference in New Issue