FIX: "Customize Text" showed compiled MessageFormat string for overridden `_MF` translations

This commit is contained in:
Gerhard Schlager 2021-12-16 16:54:45 +01:00 committed by Gerhard Schlager
parent e19a7a7c8d
commit 4cd5158974
4 changed files with 54 additions and 14 deletions

View File

@ -157,11 +157,6 @@ class Admin::SiteTextsController < Admin::AdminController
end
def record_for(key:, value: nil, locale:)
if key.ends_with?("_MF")
override = TranslationOverride.where(translation_key: key, locale: locale).pluck(:value)
value = override&.first
end
value ||= I18n.with_locale(locale) { I18n.t(key) }
{ id: key, value: value, locale: locale }
end

View File

@ -169,14 +169,14 @@ module I18n
if !by_site.has_key?(locale)
# Load overrides
translations_overrides = TranslationOverride.where(locale: locale).pluck(:translation_key, :value, :compiled_js)
translations_overrides = TranslationOverride.where(locale: locale).pluck(:translation_key, :value)
if translations_overrides.empty?
by_site[locale] = {}
else
translations_overrides.each do |tuple|
by_locale = by_site[locale] ||= {}
by_locale[tuple[0]] = tuple[2] || tuple[1]
by_locale[tuple[0]] = tuple[1]
end
end

View File

@ -161,18 +161,25 @@ module JsLocaleHelper
end
def self.output_client_overrides(locale)
translations = (I18n.overrides_by_locale(locale) || {}).select { |k, _| k[/^(admin_js|js)\./] }
return "" if translations.blank?
overrides = TranslationOverride
.where(locale: locale)
.where("translation_key LIKE 'js.%' OR translation_key LIKE 'admin_js.%'")
.pluck(:translation_key, :value, :compiled_js)
message_formats = {}
return "" if overrides.blank?
translations.delete_if do |key, value|
if key.to_s.end_with?("_MF")
message_formats[key] = value
message_formats = []
translations = {}
overrides.each do |key, value, compiled_js|
if key.end_with?("_MF")
message_formats << "#{key.inspect}: #{compiled_js}"
else
translations[key] = value
end
end
message_formats = message_formats.map { |k, v| "#{k.inspect}: #{v}" }.join(", ")
message_formats = message_formats.join(", ")
<<~JS
I18n._mfOverrides = {#{message_formats}};

View File

@ -138,6 +138,21 @@ RSpec.describe Admin::SiteTextsController do
expect(response.parsed_body['site_texts']).to be_empty
end
it "returns site text from fallback locale if current locale doesn't have a translation" do
TranslationOverride.upsert!(:en, 'js.summary.description_time_MF', 'description_time_MF override')
TranslationOverride.upsert!(:en, 'education.new-topic', 'education.new-topic override')
get "/admin/customize/site_texts.json", params: { q: 'js.summary.description_time_MF', locale: 'en_GB' }
expect(response.status).to eq(200)
value = response.parsed_body['site_texts'].find { |text| text['id'] == 'js.summary.description_time_MF' }['value']
expect(value).to eq('description_time_MF override')
get "/admin/customize/site_texts.json", params: { q: 'education.new-topic', locale: 'en_GB' }
expect(response.status).to eq(200)
value = response.parsed_body['site_texts'].find { |text| text['id'] == 'education.new-topic' }['value']
expect(value).to eq('education.new-topic override')
end
context 'plural keys' do
before do
I18n.backend.store_translations(:en, colour: { one: '%{count} colour', other: '%{count} colours' })
@ -272,6 +287,29 @@ RSpec.describe Admin::SiteTextsController do
expect(response.status).to eq(400)
end
it "returns site text from fallback locale if current locale doesn't have a translation" do
TranslationOverride.upsert!(:en, 'js.summary.description_time_MF', 'description_time_MF override')
TranslationOverride.upsert!(:en, 'education.new-topic', 'education.new-topic override')
get "/admin/customize/site_texts/js.summary.description_time_MF.json", params: { locale: 'en_GB' }
expect(response.status).to eq(200)
json = response.parsed_body
site_text = json['site_text']
expect(site_text['id']).to eq('js.summary.description_time_MF')
expect(site_text['value']).to eq('description_time_MF override')
get "/admin/customize/site_texts/education.new-topic.json", params: { locale: 'en_GB' }
expect(response.status).to eq(200)
json = response.parsed_body
site_text = json['site_text']
expect(site_text['id']).to eq('education.new-topic')
expect(site_text['value']).to eq('education.new-topic override')
end
context 'plural keys' do
before do
I18n.backend.store_translations(:en, colour: { one: '%{count} colour', other: '%{count} colours' })