2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-11-14 05:34:13 +08:00
|
|
|
class TranslationOverride < ActiveRecord::Base
|
2024-07-11 20:53:29 +08:00
|
|
|
# TODO: Remove once
|
|
|
|
# 20240711123755_drop_compiled_js_from_translation_overrides has been
|
|
|
|
# promoted to pre-deploy
|
|
|
|
self.ignored_columns = %w[compiled_js]
|
|
|
|
|
2020-07-27 08:23:54 +08:00
|
|
|
# Allowlist i18n interpolation keys that can be included when customizing translations
|
2020-09-28 11:52:05 +08:00
|
|
|
ALLOWED_CUSTOM_INTERPOLATION_KEYS = {
|
2021-03-25 03:58:24 +08:00
|
|
|
%w[
|
|
|
|
user_notifications.user_
|
|
|
|
user_notifications.only_reply_by_email
|
|
|
|
user_notifications.reply_by_email
|
|
|
|
user_notifications.visit_link_to_respond
|
2021-03-26 02:00:05 +08:00
|
|
|
user_notifications.header_instructions
|
|
|
|
user_notifications.pm_participants
|
|
|
|
unsubscribe_mailing_list
|
|
|
|
unsubscribe_link_and_mail
|
|
|
|
unsubscribe_link
|
2021-03-25 03:58:24 +08:00
|
|
|
] => %w[
|
2021-03-26 02:00:05 +08:00
|
|
|
topic_title
|
2017-11-20 16:10:06 +08:00
|
|
|
topic_title_url_encoded
|
2021-03-26 02:00:05 +08:00
|
|
|
message
|
|
|
|
url
|
|
|
|
post_id
|
|
|
|
topic_id
|
2017-11-20 16:10:06 +08:00
|
|
|
context
|
2021-03-26 02:00:05 +08:00
|
|
|
username
|
|
|
|
group_name
|
|
|
|
unsubscribe_url
|
|
|
|
subject_pm
|
|
|
|
participants
|
|
|
|
site_description
|
|
|
|
site_title
|
|
|
|
site_title_url_encoded
|
2020-11-03 12:00:11 +08:00
|
|
|
site_name
|
|
|
|
optional_re
|
|
|
|
optional_pm
|
|
|
|
optional_cat
|
|
|
|
optional_tags
|
2023-01-09 20:20:10 +08:00
|
|
|
],
|
2023-02-18 05:38:21 +08:00
|
|
|
%w[system_messages.welcome_user] => %w[username name name_or_username],
|
2017-11-20 16:10:06 +08:00
|
|
|
}
|
2021-03-25 03:58:24 +08:00
|
|
|
|
2021-10-27 22:33:07 +08:00
|
|
|
include HasSanitizableFields
|
2017-11-20 16:10:06 +08:00
|
|
|
|
2015-11-14 05:34:13 +08:00
|
|
|
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
|
2024-07-25 22:56:08 +08:00
|
|
|
validate :check_MF_string, if: :message_format?
|
2017-06-15 16:08:23 +08:00
|
|
|
|
2024-06-20 16:33:01 +08:00
|
|
|
attribute :status, :integer
|
2024-08-19 20:44:17 +08:00
|
|
|
enum :status, { up_to_date: 0, outdated: 1, invalid_interpolation_keys: 2, deprecated: 3 }
|
2023-07-10 10:06:40 +08:00
|
|
|
|
2024-07-29 15:21:25 +08:00
|
|
|
scope :mf_locales,
|
2024-07-30 09:56:46 +08:00
|
|
|
->(locale) { not_deprecated.where(locale: locale).where("translation_key LIKE '%_MF'") }
|
2024-06-18 00:21:04 +08:00
|
|
|
scope :client_locales,
|
|
|
|
->(locale) do
|
2024-07-30 09:56:46 +08:00
|
|
|
not_deprecated
|
|
|
|
.where(locale: locale)
|
2024-06-18 00:21:04 +08:00
|
|
|
.where("translation_key LIKE 'js.%' OR translation_key LIKE 'admin_js.%'")
|
|
|
|
.where.not("translation_key LIKE '%_MF'")
|
|
|
|
end
|
|
|
|
|
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
|
|
|
|
2021-10-27 22:33:07 +08:00
|
|
|
translation_override = find_or_initialize_by(params)
|
|
|
|
sanitized_value =
|
2024-11-01 07:48:07 +08:00
|
|
|
translation_override.sanitize_field(value, additional_attributes: %w[data-auto-route target])
|
2023-07-10 10:06:40 +08:00
|
|
|
original_translation =
|
|
|
|
I18n.overrides_disabled { I18n.t(transform_pluralized_key(key), locale: :en) }
|
2021-10-27 22:33:07 +08:00
|
|
|
|
2023-07-10 10:06:40 +08:00
|
|
|
data = { value: sanitized_value, original_translation: original_translation }
|
2016-04-09 02:49:50 +08:00
|
|
|
|
2017-06-15 16:08:23 +08:00
|
|
|
params.merge!(data) if translation_override.new_record?
|
2020-05-19 00:05:34 +08:00
|
|
|
i18n_changed(locale, [key]) if translation_override.update(data)
|
2017-06-15 16:08:23 +08:00
|
|
|
translation_override
|
2015-11-21 01:30:04 +08:00
|
|
|
end
|
2015-11-18 05:14:42 +08:00
|
|
|
|
2020-05-19 00:05:34 +08:00
|
|
|
def self.revert!(locale, keys)
|
|
|
|
keys = Array.wrap(keys)
|
2015-11-21 01:30:04 +08:00
|
|
|
TranslationOverride.where(locale: locale, translation_key: keys).delete_all
|
2020-05-19 00:05:34 +08:00
|
|
|
i18n_changed(locale, keys)
|
2015-11-14 05:34:13 +08:00
|
|
|
end
|
|
|
|
|
2020-05-19 00:45:47 +08:00
|
|
|
def self.reload_all_overrides!
|
|
|
|
reload_locale!
|
|
|
|
|
|
|
|
overrides = TranslationOverride.pluck(:locale, :translation_key)
|
|
|
|
overrides = overrides.group_by(&:first).map { |k, a| [k, a.map(&:last)] }
|
|
|
|
overrides.each { |locale, keys| clear_cached_keys!(locale, keys) }
|
|
|
|
end
|
|
|
|
|
2020-05-19 00:05:34 +08:00
|
|
|
def self.reload_locale!
|
2018-03-17 04:46:49 +08:00
|
|
|
I18n.reload!
|
2019-11-05 09:15:44 +08:00
|
|
|
ExtraLocalesController.clear_cache!
|
2018-03-17 04:46:49 +08:00
|
|
|
MessageBus.publish("/i18n-flush", refresh: true)
|
2020-05-19 00:05:34 +08:00
|
|
|
end
|
2015-11-21 01:30:04 +08:00
|
|
|
|
2020-05-19 00:05:34 +08:00
|
|
|
def self.clear_cached_keys!(locale, keys)
|
|
|
|
should_clear_anon_cache = false
|
|
|
|
keys.each { |key| should_clear_anon_cache |= expire_cache(locale, key) }
|
|
|
|
Site.clear_anon_cache! if should_clear_anon_cache
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.i18n_changed(locale, keys)
|
|
|
|
reload_locale!
|
|
|
|
clear_cached_keys!(locale, keys)
|
2018-03-17 04:46:49 +08:00
|
|
|
end
|
2015-11-21 01:30:04 +08:00
|
|
|
|
2020-05-19 00:05:34 +08:00
|
|
|
def self.expire_cache(locale, key)
|
FIX: serialize Flags instead of PostActionType (#28362)
### Why?
Before, all flags were static. Therefore, they were stored in class variables and serialized by SiteSerializer. Recently, we added an option for admins to add their own flags or disable existing flags. Therefore, the class variable had to be dropped because it was unsafe for a multisite environment. However, it started causing performance problems.
### Solution
When a new Flag system is used, instead of using PostActionType, we can serialize Flags and use fragment cache for performance reasons.
At the same time, we are still supporting deprecated `replace_flags` API call. When it is used, we fall back to the old solution and the admin cannot add custom flags. In a couple of months, we will be able to drop that API function and clean that code properly. However, because it may still be used, redis cache was introduced to improve performance.
To test backward compatibility you can add this code to any plugin
```ruby
replace_flags do |flag_settings|
flag_settings.add(
4,
:inappropriate,
topic_type: true,
notify_type: true,
auto_action_type: true,
)
flag_settings.add(1001, :trolling, topic_type: true, notify_type: true, auto_action_type: true)
end
```
2024-08-14 10:13:46 +08:00
|
|
|
if key.starts_with?("post_action_types.") || key.starts_with?("topic_flag_types.")
|
|
|
|
PostActionType.new.expire_cache
|
2018-03-17 04:46:49 +08:00
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
true
|
|
|
|
end
|
2017-06-15 16:08:23 +08:00
|
|
|
|
2023-06-28 19:03:04 +08:00
|
|
|
# We use English as the source of truth when extracting interpolation keys,
|
|
|
|
# but some languages, like Arabic, have plural forms (zero, two, few, many)
|
|
|
|
# which don't exist in English (one, other), so we map that here in order to
|
|
|
|
# find the correct, English translation key in which to look.
|
|
|
|
def self.transform_pluralized_key(key)
|
|
|
|
match = key.match(/(.*)\.(zero|two|few|many)\z/)
|
|
|
|
match ? match.to_a.second + ".other" : key
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.custom_interpolation_keys(translation_key)
|
|
|
|
ALLOWED_CUSTOM_INTERPOLATION_KEYS.find do |keys, value|
|
|
|
|
break value if keys.any? { |k| translation_key.start_with?(k) }
|
|
|
|
end || []
|
|
|
|
end
|
|
|
|
|
2020-05-19 00:05:34 +08:00
|
|
|
private_class_method :reload_locale!
|
|
|
|
private_class_method :clear_cached_keys!
|
2018-03-17 04:46:49 +08:00
|
|
|
private_class_method :i18n_changed
|
|
|
|
private_class_method :expire_cache
|
2017-11-20 16:10:06 +08:00
|
|
|
|
2023-07-14 16:52:39 +08:00
|
|
|
def original_translation_deleted?
|
|
|
|
!I18n.overrides_disabled { I18n.t!(transformed_key, locale: :en) }.is_a?(String)
|
|
|
|
rescue I18n::MissingTranslationData
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2023-07-10 10:06:40 +08:00
|
|
|
def original_translation_updated?
|
|
|
|
return false if original_translation.blank?
|
2017-11-20 16:10:06 +08:00
|
|
|
|
2023-07-19 23:06:13 +08:00
|
|
|
original_translation != current_default
|
2023-07-10 10:06:40 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def invalid_interpolation_keys
|
2023-07-19 23:06:13 +08:00
|
|
|
return [] if current_default.blank?
|
2023-07-10 10:06:40 +08:00
|
|
|
|
2023-07-19 23:06:13 +08:00
|
|
|
original_interpolation_keys = I18nInterpolationKeysFinder.find(current_default)
|
2023-07-10 10:06:40 +08:00
|
|
|
new_interpolation_keys = I18nInterpolationKeysFinder.find(value)
|
|
|
|
custom_interpolation_keys = []
|
|
|
|
|
|
|
|
ALLOWED_CUSTOM_INTERPOLATION_KEYS.select do |keys, value|
|
|
|
|
custom_interpolation_keys = value if keys.any? { |key| transformed_key.start_with?(key) }
|
2017-06-15 16:08:23 +08:00
|
|
|
end
|
2023-07-10 10:06:40 +08:00
|
|
|
|
|
|
|
(original_interpolation_keys | new_interpolation_keys) - original_interpolation_keys -
|
|
|
|
custom_interpolation_keys
|
|
|
|
end
|
|
|
|
|
2023-07-19 23:06:13 +08:00
|
|
|
def current_default
|
|
|
|
I18n.overrides_disabled { I18n.t(transformed_key, locale: :en) }
|
|
|
|
end
|
|
|
|
|
2024-07-25 22:56:08 +08:00
|
|
|
def message_format?
|
|
|
|
translation_key.to_s.end_with?("_MF")
|
|
|
|
end
|
|
|
|
|
2024-07-26 17:22:01 +08:00
|
|
|
def make_up_to_date!
|
|
|
|
return unless outdated?
|
|
|
|
self.original_translation = current_default
|
|
|
|
update_attribute!(:status, :up_to_date)
|
|
|
|
end
|
|
|
|
|
2023-07-10 10:06:40 +08:00
|
|
|
private
|
|
|
|
|
2023-07-14 16:52:39 +08:00
|
|
|
def transformed_key
|
|
|
|
@transformed_key ||= self.class.transform_pluralized_key(translation_key)
|
|
|
|
end
|
|
|
|
|
2023-07-10 10:06:40 +08:00
|
|
|
def check_interpolation_keys
|
|
|
|
invalid_keys = invalid_interpolation_keys
|
|
|
|
|
|
|
|
return if invalid_keys.blank?
|
|
|
|
|
|
|
|
self.errors.add(
|
|
|
|
:base,
|
|
|
|
I18n.t(
|
|
|
|
"activerecord.errors.models.translation_overrides.attributes.value.invalid_interpolation_keys",
|
|
|
|
keys: invalid_keys.join(I18n.t("word_connector.comma")),
|
|
|
|
count: invalid_keys.size,
|
|
|
|
),
|
|
|
|
)
|
2018-03-17 04:46:49 +08:00
|
|
|
end
|
2024-07-25 22:56:08 +08:00
|
|
|
|
|
|
|
def check_MF_string
|
|
|
|
require "messageformat"
|
|
|
|
|
|
|
|
MessageFormat.compile(locale, { key: value }, strict: true)
|
|
|
|
rescue MessageFormat::Compiler::CompileError => e
|
|
|
|
errors.add(:base, e.cause.message)
|
|
|
|
end
|
2015-11-14 05:34:13 +08:00
|
|
|
end
|
2016-01-11 14:30:56 +08:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: translation_overrides
|
|
|
|
#
|
2023-07-10 10:06:40 +08:00
|
|
|
# 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
|
|
|
|
# original_translation :text
|
|
|
|
# status :integer default("up_to_date"), not null
|
2016-01-11 14:30:56 +08:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_translation_overrides_on_locale_and_translation_key (locale,translation_key) UNIQUE
|
|
|
|
#
|