mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
FIX: Validate interpolation keys used in translation overrides.
https://meta.discourse.org/t/discobot-translation-missing-error/64429/6?u=tgxworld
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
require 'js_locale_helper'
|
||||
require "i18n/i18n_interpolation_keys_finder"
|
||||
|
||||
class TranslationOverride < ActiveRecord::Base
|
||||
validates_uniqueness_of :translation_key, scope: :locale
|
||||
validates_presence_of :locale, :translation_key, :value
|
||||
|
||||
validate :check_interpolation_keys
|
||||
|
||||
def self.upsert!(locale, key, value)
|
||||
params = { locale: locale, translation_key: key }
|
||||
|
||||
@@ -12,9 +15,10 @@ class TranslationOverride < ActiveRecord::Base
|
||||
data[:compiled_js] = JsLocaleHelper.compile_message_format(locale, value)
|
||||
end
|
||||
|
||||
row_count = where(params).update_all(data)
|
||||
create!(params.merge(data)) if row_count == 0
|
||||
i18n_changed
|
||||
translation_override = find_or_initialize_by(params)
|
||||
params.merge!(data) if translation_override.new_record?
|
||||
i18n_changed if translation_override.update(data)
|
||||
translation_override
|
||||
end
|
||||
|
||||
def self.revert!(locale, *keys)
|
||||
@@ -22,13 +26,48 @@ class TranslationOverride < ActiveRecord::Base
|
||||
i18n_changed
|
||||
end
|
||||
|
||||
protected
|
||||
private
|
||||
|
||||
def self.i18n_changed
|
||||
I18n.reload!
|
||||
MessageBus.publish('/i18n-flush', { refresh: true })
|
||||
end
|
||||
|
||||
def lookup_original_text
|
||||
I18n::Backend::Simple.new.send(
|
||||
:lookup, self.locale, self.translation_key
|
||||
)
|
||||
end
|
||||
|
||||
def check_interpolation_keys
|
||||
if original_text = lookup_original_text
|
||||
original_interpolation_keys = I18nInterpolationKeysFinder.find(original_text)
|
||||
new_interpolation_keys = I18nInterpolationKeysFinder.find(value)
|
||||
missing_keys = (original_interpolation_keys - new_interpolation_keys)
|
||||
invalid_keys = (original_interpolation_keys | new_interpolation_keys) - original_interpolation_keys
|
||||
|
||||
if missing_keys.present?
|
||||
self.errors.add(:base, I18n.t(
|
||||
'activerecord.errors.models.translation_overrides.attributes.value.missing_interpolation_keys',
|
||||
keys: missing_keys.join(', ')
|
||||
))
|
||||
|
||||
return false
|
||||
end
|
||||
|
||||
invalid_keys = (original_interpolation_keys | new_interpolation_keys) - original_interpolation_keys
|
||||
|
||||
if invalid_keys.present?
|
||||
self.errors.add(:base, I18n.t(
|
||||
'activerecord.errors.models.translation_overrides.attributes.value.invalid_interpolation_keys',
|
||||
keys: invalid_keys.join(', ')
|
||||
))
|
||||
|
||||
return false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
# == Schema Information
|
||||
|
||||
Reference in New Issue
Block a user