mirror of
https://github.com/discourse/discourse.git
synced 2025-05-23 10:01:11 +08:00
FIX: "Customize Text" showed compiled MessageFormat string for overridden _MF
translations
This commit is contained in:

committed by
Gerhard Schlager

parent
e19a7a7c8d
commit
4cd5158974
@ -157,11 +157,6 @@ class Admin::SiteTextsController < Admin::AdminController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def record_for(key:, value: nil, locale:)
|
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) }
|
value ||= I18n.with_locale(locale) { I18n.t(key) }
|
||||||
{ id: key, value: value, locale: locale }
|
{ id: key, value: value, locale: locale }
|
||||||
end
|
end
|
||||||
|
@ -169,14 +169,14 @@ module I18n
|
|||||||
|
|
||||||
if !by_site.has_key?(locale)
|
if !by_site.has_key?(locale)
|
||||||
# Load overrides
|
# 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?
|
if translations_overrides.empty?
|
||||||
by_site[locale] = {}
|
by_site[locale] = {}
|
||||||
else
|
else
|
||||||
translations_overrides.each do |tuple|
|
translations_overrides.each do |tuple|
|
||||||
by_locale = by_site[locale] ||= {}
|
by_locale = by_site[locale] ||= {}
|
||||||
by_locale[tuple[0]] = tuple[2] || tuple[1]
|
by_locale[tuple[0]] = tuple[1]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -161,18 +161,25 @@ module JsLocaleHelper
|
|||||||
end
|
end
|
||||||
|
|
||||||
def self.output_client_overrides(locale)
|
def self.output_client_overrides(locale)
|
||||||
translations = (I18n.overrides_by_locale(locale) || {}).select { |k, _| k[/^(admin_js|js)\./] }
|
overrides = TranslationOverride
|
||||||
return "" if translations.blank?
|
.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|
|
message_formats = []
|
||||||
if key.to_s.end_with?("_MF")
|
translations = {}
|
||||||
message_formats[key] = value
|
|
||||||
|
overrides.each do |key, value, compiled_js|
|
||||||
|
if key.end_with?("_MF")
|
||||||
|
message_formats << "#{key.inspect}: #{compiled_js}"
|
||||||
|
else
|
||||||
|
translations[key] = value
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
message_formats = message_formats.map { |k, v| "#{k.inspect}: #{v}" }.join(", ")
|
message_formats = message_formats.join(", ")
|
||||||
|
|
||||||
<<~JS
|
<<~JS
|
||||||
I18n._mfOverrides = {#{message_formats}};
|
I18n._mfOverrides = {#{message_formats}};
|
||||||
|
@ -138,6 +138,21 @@ RSpec.describe Admin::SiteTextsController do
|
|||||||
expect(response.parsed_body['site_texts']).to be_empty
|
expect(response.parsed_body['site_texts']).to be_empty
|
||||||
end
|
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
|
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' })
|
||||||
@ -272,6 +287,29 @@ RSpec.describe Admin::SiteTextsController do
|
|||||||
expect(response.status).to eq(400)
|
expect(response.status).to eq(400)
|
||||||
end
|
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
|
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' })
|
||||||
|
Reference in New Issue
Block a user