2016-04-09 02:49:50 +08:00
|
|
|
require 'js_locale_helper'
|
2017-06-15 16:08:23 +08:00
|
|
|
require "i18n/i18n_interpolation_keys_finder"
|
2016-04-09 02:49:50 +08:00
|
|
|
|
2015-11-14 05:34:13 +08:00
|
|
|
class TranslationOverride < ActiveRecord::Base
|
|
|
|
validates_uniqueness_of :translation_key, scope: :locale
|
|
|
|
validates_presence_of :locale, :translation_key, :value
|
|
|
|
|
2017-06-15 16:08:23 +08:00
|
|
|
validate :check_interpolation_keys
|
|
|
|
|
2015-11-14 05:34:13 +08:00
|
|
|
def self.upsert!(locale, key, value)
|
|
|
|
params = { locale: locale, translation_key: key }
|
2016-04-09 02:49:50 +08:00
|
|
|
|
|
|
|
data = { value: value }
|
|
|
|
if key.end_with?('_MF')
|
|
|
|
data[:compiled_js] = JsLocaleHelper.compile_message_format(locale, value)
|
|
|
|
end
|
|
|
|
|
2017-06-15 16:08:23 +08:00
|
|
|
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-21 01:30:04 +08:00
|
|
|
end
|
2015-11-18 05:14:42 +08:00
|
|
|
|
2015-11-21 01:30:04 +08:00
|
|
|
def self.revert!(locale, *keys)
|
|
|
|
TranslationOverride.where(locale: locale, translation_key: keys).delete_all
|
|
|
|
i18n_changed
|
2015-11-14 05:34:13 +08:00
|
|
|
end
|
|
|
|
|
2017-06-15 16:08:23 +08:00
|
|
|
private
|
2015-11-21 01:30:04 +08:00
|
|
|
|
|
|
|
def self.i18n_changed
|
|
|
|
I18n.reload!
|
2017-07-28 09:20:09 +08:00
|
|
|
MessageBus.publish('/i18n-flush', refresh: true)
|
2015-11-21 01:30:04 +08:00
|
|
|
end
|
|
|
|
|
2017-06-15 16:08:23 +08:00
|
|
|
def check_interpolation_keys
|
2017-06-22 10:23:31 +08:00
|
|
|
original_text = I18n.overrides_disabled do
|
|
|
|
I18n.backend.send(:lookup, self.locale, self.translation_key)
|
|
|
|
end
|
|
|
|
|
|
|
|
if original_text
|
2017-06-15 16:08:23 +08:00
|
|
|
original_interpolation_keys = I18nInterpolationKeysFinder.find(original_text)
|
|
|
|
new_interpolation_keys = I18nInterpolationKeysFinder.find(value)
|
|
|
|
missing_keys = (original_interpolation_keys - new_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
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-11-14 05:34:13 +08:00
|
|
|
end
|
2016-01-11 14:30:56 +08: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
|
2016-05-30 08:45:32 +08:00
|
|
|
# compiled_js :text
|
2016-01-11 14:30:56 +08:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_translation_overrides_on_locale_and_translation_key (locale,translation_key) UNIQUE
|
|
|
|
#
|