discourse/app/models/translation_override.rb

96 lines
2.8 KiB
Ruby
Raw Normal View History

require 'js_locale_helper'
require "i18n/i18n_interpolation_keys_finder"
class TranslationOverride < ActiveRecord::Base
# Whitelist i18n interpolation keys that can be included when customizing translations
CUSTOM_INTERPOLATION_KEYS_WHITELIST = {
"user_notifications.user_" => %w{
topic_title_url_encoded
site_title_url_encoded
context
}
}
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 }
data = { value: value }
if key.end_with?('_MF')
_, filename = JsLocaleHelper.find_message_format_locale([locale], false)
data[:compiled_js] = JsLocaleHelper.compile_message_format(filename, locale, value)
end
translation_override = find_or_initialize_by(params)
params.merge!(data) if translation_override.new_record?
i18n_changed if translation_override.update(data)
translation_override
2015-11-20 12:30:04 -05:00
end
2015-11-17 16:14:42 -05:00
2015-11-20 12:30:04 -05:00
def self.revert!(locale, *keys)
TranslationOverride.where(locale: locale, translation_key: keys).delete_all
i18n_changed
end
private
2015-11-20 12:30:04 -05:00
def self.i18n_changed
I18n.reload!
2017-07-27 21:20:09 -04:00
MessageBus.publish('/i18n-flush', refresh: true)
2015-11-20 12:30:04 -05:00
end
def check_interpolation_keys
original_text = I18n.overrides_disabled do
I18n.backend.send(:lookup, self.locale, self.translation_key)
end
if original_text
original_interpolation_keys = I18nInterpolationKeysFinder.find(original_text)
new_interpolation_keys = I18nInterpolationKeysFinder.find(value)
custom_interpolation_keys = []
CUSTOM_INTERPOLATION_KEYS_WHITELIST.select do |key, value|
if self.translation_key.start_with?(key)
custom_interpolation_keys = value
end
end
invalid_keys = (original_interpolation_keys | new_interpolation_keys) -
original_interpolation_keys -
custom_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
2016-01-11 01:30:56 -05:00
# == Schema Information
#
# Table name: translation_overrides
#
# id :integer not null, primary key
# locale :string not null
# translation_key :string not null
# value :string not null
# created_at :datetime not null
# updated_at :datetime not null
# compiled_js :text
2016-01-11 01:30:56 -05:00
#
# Indexes
#
# index_translation_overrides_on_locale_and_translation_key (locale,translation_key) UNIQUE
#