2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-08-07 13:05:08 -05:00
|
|
|
class Admin::SiteTextsController < Admin::AdminController
|
2015-11-23 15:45:05 -06:00
|
|
|
def self.preferred_keys
|
2023-01-09 06:20:10 -06:00
|
|
|
%w[
|
|
|
|
system_messages.usage_tips.text_body_template
|
|
|
|
education.new-topic
|
|
|
|
education.new-reply
|
|
|
|
login_required.welcome_message
|
|
|
|
]
|
2015-11-23 15:45:05 -06:00
|
|
|
end
|
|
|
|
|
2018-05-19 01:15:57 -05:00
|
|
|
def self.restricted_keys
|
2023-01-09 06:20:10 -06:00
|
|
|
%w[
|
|
|
|
user_notifications.confirm_old_email.title
|
|
|
|
user_notifications.confirm_old_email.subject_template
|
|
|
|
user_notifications.confirm_old_email.text_body_template
|
|
|
|
]
|
2018-05-19 01:15:57 -05:00
|
|
|
end
|
|
|
|
|
2015-11-23 15:45:05 -06:00
|
|
|
def index
|
2023-01-09 06:20:10 -06:00
|
|
|
overridden = params[:overridden] == "true"
|
2023-07-09 21:06:40 -05:00
|
|
|
outdated = params[:outdated] == "true"
|
2015-11-30 14:22:58 -06:00
|
|
|
extras = {}
|
|
|
|
|
2015-12-08 11:49:37 -06:00
|
|
|
query = params[:q] || ""
|
2019-11-30 07:16:36 -06:00
|
|
|
|
2021-01-18 11:53:45 -06:00
|
|
|
locale = fetch_locale(params[:locale])
|
2019-11-30 07:16:36 -06:00
|
|
|
|
2023-07-09 21:06:40 -05:00
|
|
|
if query.blank? && !overridden && !outdated
|
2015-11-30 14:22:58 -06:00
|
|
|
extras[:recommended] = true
|
2021-01-18 11:53:45 -06:00
|
|
|
results = self.class.preferred_keys.map { |k| record_for(key: k, locale: locale) }
|
2015-11-23 15:45:05 -06:00
|
|
|
else
|
2023-07-09 21:06:40 -05:00
|
|
|
results = find_translations(query, overridden, outdated, locale)
|
2015-11-30 14:31:30 -06:00
|
|
|
|
2019-05-10 18:52:27 -05:00
|
|
|
if results.any?
|
2018-11-09 18:17:07 -06:00
|
|
|
extras[:regex] = I18n::Backend::DiscourseI18n.create_search_regexp(query, as_string: true)
|
|
|
|
end
|
|
|
|
|
2015-11-23 15:45:05 -06:00
|
|
|
results.sort! do |x, y|
|
2015-12-08 11:49:37 -06:00
|
|
|
if x[:value].casecmp(query) == 0
|
2015-11-30 14:31:30 -06:00
|
|
|
-1
|
2015-12-08 11:49:37 -06:00
|
|
|
elsif y[:value].casecmp(query) == 0
|
2015-11-30 14:31:30 -06:00
|
|
|
1
|
|
|
|
else
|
|
|
|
(x[:id].size + x[:value].size) <=> (y[:id].size + y[:value].size)
|
|
|
|
end
|
2015-11-23 15:45:05 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-11-30 07:16:36 -06:00
|
|
|
page = params[:page].to_i
|
|
|
|
raise Discourse::InvalidParameters.new(:page) if page < 0
|
|
|
|
|
|
|
|
per_page = 50
|
|
|
|
first = page * per_page
|
|
|
|
last = first + per_page
|
|
|
|
|
|
|
|
extras[:has_more] = true if results.size > last
|
2021-01-18 11:53:45 -06:00
|
|
|
|
|
|
|
if LocaleSiteSetting.fallback_locale(locale).present?
|
|
|
|
extras[:fallback_locale] = LocaleSiteSetting.fallback_locale(locale)
|
|
|
|
end
|
|
|
|
|
|
|
|
overridden = overridden_keys(locale)
|
|
|
|
render_serialized(
|
|
|
|
results[first..last - 1],
|
|
|
|
SiteTextSerializer,
|
2023-01-09 06:20:10 -06:00
|
|
|
root: "site_texts",
|
2021-01-18 11:53:45 -06:00
|
|
|
rest_serializer: true,
|
|
|
|
extras: extras,
|
|
|
|
overridden_keys: overridden,
|
|
|
|
)
|
2015-11-23 15:45:05 -06:00
|
|
|
end
|
|
|
|
|
2015-08-07 13:05:08 -05:00
|
|
|
def show
|
2021-01-18 11:53:45 -06:00
|
|
|
locale = fetch_locale(params[:locale])
|
|
|
|
site_text = find_site_text(locale)
|
2023-01-09 06:20:10 -06:00
|
|
|
render_serialized(site_text, SiteTextSerializer, root: "site_text", rest_serializer: true)
|
2015-08-07 13:05:08 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2021-01-18 11:53:45 -06:00
|
|
|
locale = fetch_locale(params.dig(:site_text, :locale))
|
|
|
|
|
|
|
|
site_text = find_site_text(locale)
|
|
|
|
value = site_text[:value] = params.dig(:site_text, :value)
|
2017-06-15 03:08:23 -05:00
|
|
|
id = site_text[:id]
|
2021-01-20 14:43:00 -06:00
|
|
|
old_value = I18n.with_locale(locale) { I18n.t(id) }
|
2021-01-18 11:53:45 -06:00
|
|
|
|
|
|
|
translation_override = TranslationOverride.upsert!(locale, id, value)
|
2015-08-07 13:05:08 -05:00
|
|
|
|
2017-06-15 03:08:23 -05:00
|
|
|
if translation_override.errors.empty?
|
|
|
|
StaffActionLogger.new(current_user).log_site_text_change(id, value, old_value)
|
2019-11-07 23:34:24 -06:00
|
|
|
system_badge_id = Badge.find_system_badge_id_from_translation_key(id)
|
2020-01-21 20:01:40 -06:00
|
|
|
if system_badge_id.present? && is_badge_title?(id)
|
2019-11-07 23:34:24 -06:00
|
|
|
Jobs.enqueue(
|
|
|
|
:bulk_user_title_update,
|
|
|
|
new_title: value,
|
|
|
|
granted_badge_id: system_badge_id,
|
2023-01-09 06:20:10 -06:00
|
|
|
action: Jobs::BulkUserTitleUpdate::UPDATE_ACTION,
|
2019-11-07 23:34:24 -06:00
|
|
|
)
|
|
|
|
end
|
2023-01-09 06:20:10 -06:00
|
|
|
render_serialized(site_text, SiteTextSerializer, root: "site_text", rest_serializer: true)
|
2017-06-15 03:08:23 -05:00
|
|
|
else
|
2023-01-09 06:20:10 -06:00
|
|
|
render json:
|
|
|
|
failed_json.merge(message: translation_override.errors.full_messages.join("\n\n")),
|
|
|
|
status: 422
|
2017-06-15 03:08:23 -05:00
|
|
|
end
|
2015-11-23 15:45:05 -06:00
|
|
|
end
|
2015-08-07 13:05:08 -05:00
|
|
|
|
2015-11-23 15:45:05 -06:00
|
|
|
def revert
|
2021-01-18 11:53:45 -06:00
|
|
|
locale = fetch_locale(params[:locale])
|
|
|
|
|
|
|
|
site_text = find_site_text(locale)
|
2019-11-07 23:34:24 -06:00
|
|
|
id = site_text[:id]
|
2021-01-20 14:43:00 -06:00
|
|
|
old_text = I18n.with_locale(locale) { I18n.t(id) }
|
2021-01-18 11:53:45 -06:00
|
|
|
TranslationOverride.revert!(locale, id)
|
|
|
|
|
|
|
|
site_text = find_site_text(locale)
|
2019-11-07 23:34:24 -06:00
|
|
|
StaffActionLogger.new(current_user).log_site_text_change(id, site_text[:value], old_text)
|
|
|
|
system_badge_id = Badge.find_system_badge_id_from_translation_key(id)
|
|
|
|
if system_badge_id.present?
|
|
|
|
Jobs.enqueue(
|
|
|
|
:bulk_user_title_update,
|
|
|
|
granted_badge_id: system_badge_id,
|
2023-01-09 06:20:10 -06:00
|
|
|
action: Jobs::BulkUserTitleUpdate::RESET_ACTION,
|
2019-11-07 23:34:24 -06:00
|
|
|
)
|
|
|
|
end
|
2023-01-09 06:20:10 -06:00
|
|
|
render_serialized(site_text, SiteTextSerializer, root: "site_text", rest_serializer: true)
|
2015-08-07 13:05:08 -05:00
|
|
|
end
|
2015-11-23 15:45:05 -06:00
|
|
|
|
2023-07-19 10:06:13 -05:00
|
|
|
def dismiss_outdated
|
|
|
|
locale = fetch_locale(params[:locale])
|
|
|
|
override = TranslationOverride.find_by(locale: locale, translation_key: params[:id])
|
|
|
|
|
|
|
|
raise Discourse::NotFound if override.blank?
|
|
|
|
|
|
|
|
if override.outdated?
|
|
|
|
override.update!(
|
|
|
|
status: "up_to_date",
|
|
|
|
original_translation:
|
|
|
|
I18n.overrides_disabled do
|
|
|
|
I18n.t(TranslationOverride.transform_pluralized_key(params[:id]), locale: :en)
|
|
|
|
end,
|
|
|
|
)
|
|
|
|
render json: success_json
|
|
|
|
else
|
|
|
|
render json: failed_json.merge(message: "Can only dismiss outdated translations"), status: 422
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-18 15:09:13 -05:00
|
|
|
def get_reseed_options
|
|
|
|
render_json_dump(
|
|
|
|
categories: SeedData::Categories.with_default_locale.reseed_options,
|
2023-01-09 06:20:10 -06:00
|
|
|
topics: SeedData::Topics.with_default_locale.reseed_options,
|
2019-03-18 15:09:13 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def reseed
|
|
|
|
hijack do
|
|
|
|
if params[:category_ids].present?
|
2023-01-09 06:20:10 -06:00
|
|
|
SeedData::Categories.with_default_locale.update(site_setting_names: params[:category_ids])
|
2019-03-18 15:09:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
if params[:topic_ids].present?
|
2023-01-09 06:20:10 -06:00
|
|
|
SeedData::Topics.with_default_locale.update(site_setting_names: params[:topic_ids])
|
2019-03-18 15:09:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
render json: success_json
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-11-23 15:45:05 -06:00
|
|
|
protected
|
|
|
|
|
2020-01-21 20:01:40 -06:00
|
|
|
def is_badge_title?(id = "")
|
2023-01-09 06:20:10 -06:00
|
|
|
badge_parts = id.split(".")
|
|
|
|
badge_parts[0] == "badges" && badge_parts[2] == "name"
|
2020-01-21 20:01:40 -06:00
|
|
|
end
|
|
|
|
|
2021-01-18 11:53:45 -06:00
|
|
|
def record_for(key:, value: nil, locale:)
|
2023-06-28 06:03:04 -05:00
|
|
|
en_key = TranslationOverride.transform_pluralized_key(key)
|
2021-01-20 14:43:00 -06:00
|
|
|
value ||= I18n.with_locale(locale) { I18n.t(key) }
|
2023-06-28 06:03:04 -05:00
|
|
|
interpolation_keys =
|
|
|
|
I18nInterpolationKeysFinder.find(I18n.overrides_disabled { I18n.t(en_key, locale: :en) })
|
|
|
|
custom_keys = TranslationOverride.custom_interpolation_keys(en_key)
|
|
|
|
{ id: key, value: value, locale: locale, interpolation_keys: interpolation_keys + custom_keys }
|
2018-06-07 00:28:18 -05:00
|
|
|
end
|
|
|
|
|
2023-01-20 12:52:49 -06:00
|
|
|
PLURALIZED_REGEX = /(.*)\.(zero|one|two|few|many|other)\z/
|
2019-05-10 18:52:27 -05:00
|
|
|
|
2021-01-18 11:53:45 -06:00
|
|
|
def find_site_text(locale)
|
2019-05-10 18:52:27 -05:00
|
|
|
if self.class.restricted_keys.include?(params[:id])
|
2023-01-09 06:20:10 -06:00
|
|
|
raise Discourse::InvalidAccess.new(
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
custom_message: "email_template_cant_be_modified",
|
|
|
|
)
|
2019-05-10 18:52:27 -05:00
|
|
|
end
|
|
|
|
|
2023-01-09 06:20:10 -06:00
|
|
|
if I18n.exists?(params[:id], locale) ||
|
|
|
|
TranslationOverride.exists?(locale: locale, translation_key: params[:id])
|
2021-01-18 11:53:45 -06:00
|
|
|
return record_for(key: params[:id], locale: locale)
|
2019-05-10 18:52:27 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
if PLURALIZED_REGEX.match(params[:id])
|
2021-01-18 11:53:45 -06:00
|
|
|
value = fix_plural_keys($1, {}, locale).detect { |plural| plural[0] == $2.to_sym }
|
|
|
|
return record_for(key: params[:id], value: value[1], locale: value[2]) if value
|
2019-05-10 18:52:27 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
raise Discourse::NotFound
|
2018-06-07 00:28:18 -05:00
|
|
|
end
|
2015-11-23 15:45:05 -06:00
|
|
|
|
2023-07-09 21:06:40 -05:00
|
|
|
def find_translations(query, overridden, outdated, locale)
|
2019-05-10 18:52:27 -05:00
|
|
|
translations = Hash.new { |hash, key| hash[key] = {} }
|
2023-01-09 06:20:10 -06:00
|
|
|
search_results = I18n.with_locale(locale) { I18n.search(query, only_overridden: overridden) }
|
2019-05-10 18:52:27 -05:00
|
|
|
|
2023-07-09 21:06:40 -05:00
|
|
|
if outdated
|
|
|
|
outdated_keys =
|
|
|
|
TranslationOverride.where(status: %i[outdated invalid_interpolation_keys]).pluck(
|
|
|
|
:translation_key,
|
|
|
|
)
|
|
|
|
search_results.select! { |k, _| outdated_keys.include?(k) }
|
|
|
|
end
|
|
|
|
|
2021-01-20 14:43:00 -06:00
|
|
|
search_results.each do |key, value|
|
2019-05-10 18:52:27 -05:00
|
|
|
if PLURALIZED_REGEX.match(key)
|
|
|
|
translations[$1][$2] = value
|
|
|
|
else
|
|
|
|
translations[key] = value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
results = []
|
|
|
|
|
|
|
|
translations.each do |key, value|
|
2019-08-29 10:37:04 -05:00
|
|
|
next unless I18n.exists?(key, :en)
|
|
|
|
|
2023-12-06 16:25:00 -06:00
|
|
|
if value.is_a?(Hash)
|
2021-01-18 11:53:45 -06:00
|
|
|
fix_plural_keys(key, value, locale).each do |plural|
|
|
|
|
plural_key = plural[0]
|
|
|
|
plural_value = plural[1]
|
|
|
|
|
|
|
|
results << record_for(
|
2023-01-09 06:20:10 -06:00
|
|
|
key: "#{key}.#{plural_key}",
|
|
|
|
value: plural_value,
|
|
|
|
locale: plural.last,
|
2021-01-18 11:53:45 -06:00
|
|
|
)
|
2019-05-10 18:52:27 -05:00
|
|
|
end
|
|
|
|
else
|
2021-01-18 11:53:45 -06:00
|
|
|
results << record_for(key: key, value: value, locale: locale)
|
2019-05-10 18:52:27 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
results
|
|
|
|
end
|
|
|
|
|
2021-01-18 11:53:45 -06:00
|
|
|
def fix_plural_keys(key, value, locale)
|
2019-05-10 18:52:27 -05:00
|
|
|
value = value.with_indifferent_access
|
2023-01-09 06:20:10 -06:00
|
|
|
plural_keys = I18n.with_locale(locale) { I18n.t("i18n.plural.keys") }
|
2019-05-10 18:52:27 -05:00
|
|
|
return value if value.keys.size == plural_keys.size && plural_keys.all? { |k| value.key?(k) }
|
|
|
|
|
2019-08-29 10:37:04 -05:00
|
|
|
fallback_value = I18n.t(key, locale: :en, default: {})
|
2019-05-10 18:52:27 -05:00
|
|
|
plural_keys.map do |k|
|
2021-01-18 11:53:45 -06:00
|
|
|
if value[k]
|
|
|
|
[k, value[k], locale]
|
|
|
|
else
|
|
|
|
[k, fallback_value[k] || fallback_value[:other], :en]
|
|
|
|
end
|
|
|
|
end
|
2019-05-10 18:52:27 -05:00
|
|
|
end
|
2019-07-01 20:53:16 -05:00
|
|
|
|
2021-01-18 11:53:45 -06:00
|
|
|
def overridden_keys(locale)
|
|
|
|
TranslationOverride.where(locale: locale).pluck(:translation_key)
|
|
|
|
end
|
|
|
|
|
|
|
|
def fetch_locale(locale_from_params)
|
|
|
|
locale_from_params.tap do |locale|
|
|
|
|
if locale.blank? || !I18n.locale_available?(locale)
|
|
|
|
raise Discourse::InvalidParameters.new(:locale)
|
|
|
|
end
|
|
|
|
end
|
2019-07-01 20:53:16 -05:00
|
|
|
end
|
2015-08-07 13:05:08 -05:00
|
|
|
end
|