Skip to content

A platform for community discussion. Free, open, simple.

FIX: Allow user to choose if they want to edit localized or original title (#36099)

Natalie Tay91ceb9c2df97
diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rb
index 369fb59bbf6..a05420fb8d3 100644
--- a/app/controllers/topics_controller.rb
+++ b/app/controllers/topics_controller.rb
@@ -1375,7 +1375,9 @@ class TopicsController < ApplicationController
format.html do
@tags = SiteSetting.tagging_enabled ? @topic_view.topic.tags.visible(guardian) : []
- helpers.localize_topic_view_content(@topic_view) if SiteSetting.content_localization_enabled
+ if SiteSetting.content_localization_enabled && use_crawler_layout?
+ helpers.localize_topic_view_content(@topic_view)
+ end
@breadcrumbs = helpers.categories_breadcrumb(@topic_view.topic) || []
@description_meta = (@topic_view.topic.excerpt.presence || @topic_view.summary)
store_preloaded("topic_#{@topic_view.topic.id}", MultiJson.dump(topic_view_serializer))
diff --git a/app/serializers/concerns/localized_fancy_topic_title_mixin.rb b/app/serializers/concerns/localized_fancy_topic_title_mixin.rb
index 0301073a33b..5dbb5422479 100644
--- a/app/serializers/concerns/localized_fancy_topic_title_mixin.rb
+++ b/app/serializers/concerns/localized_fancy_topic_title_mixin.rb
@@ -3,27 +3,49 @@
module LocalizedFancyTopicTitleMixin
def self.included(klass)
klass.attributes :fancy_title
+ klass.attributes :fancy_title_localized
+ klass.attributes :locale
end
def fancy_title
- f = _topic.fancy_title
-
- if (ContentLocalization.show_translated_topic?(_topic, scope))
- _topic.get_localization&.fancy_title.presence || f
- else
- f
- end
+ translated_title || _topic.fancy_title
end
def include_fancy_title?
_topic.present? && _topic&.fancy_title.present?
end
+ def fancy_title_localized
+ translated_title.present?
+ end
+
+ def include_fancy_title_localized?
+ SiteSetting.content_localization_enabled && include_fancy_title?
+ end
+
+ def locale
+ _topic.locale
+ end
+
+ def include_locale?
+ SiteSetting.content_localization_enabled
+ end
+
private
def _topic
- return object if object.class == Topic
- return topic if defined?(topic) && topic.class == Topic
- object.topic if defined?(object.topic) && object.topic.class == Topic
+ @_topic ||=
+ if object.class == Topic
+ object
+ elsif defined?(topic) && topic.class == Topic
+ topic
+ elsif defined?(object.topic) && object.topic.class == Topic
+ object.topic
+ end
+ end
+
+ def translated_title
+ ContentLocalization.show_translated_topic?(_topic, scope) &&
+ _topic.get_localization&.fancy_title.presence
end
end
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index 939a4603f20..1a44757c7f8 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -4110,6 +4110,11 @@ en:
one: You have selected <b>%{count}</b> post.
other: "You have selected <b>%{count}</b> posts."
deleted_by_author_simple: "(topic deleted by author)"
+ localizations:
+ title_edit_warning:
+ message: "This is a translated topic title.<br><br>Do you want to edit the original title in %{language} or edit this specific translation?"
+ action_original: "Edit original topic title"
+ action_translation: "Edit translated topic title"
post_list:
title: "Latest posts"
@@ -4229,7 +4234,7 @@ en:
post_language_selector:
none: "None"
edit_warning:
- message: "This is a translated post.<br><br> Do you want to edit the original post in %{language} or edit this specific translation?"
+ message: "This is a translated post.<br><br>Do you want to edit the original post in %{language} or edit this specific translation?"
action_original: "Edit original post"
action_translation: "Edit this translation"
diff --git a/frontend/discourse/app/controllers/topic.js b/frontend/discourse/app/controllers/topic.js
index 7e130643b75..95a849adafa 100644
--- a/frontend/discourse/app/controllers/topic.js
+++ b/frontend/discourse/app/controllers/topic.js
@@ -377,10 +377,65 @@ export default class TopicController extends Controller {
}
@action
- editTopic(event) {
+ async handleTitleClick(event) {
+ this.editTopic?.(event);
+ this.jumpTop?.(event);
+ }
+
+ @action
+ async editTopic(event) {
event?.preventDefault();
- if (this.get("model.details.can_edit")) {
- this.set("editingTopic", true);
+ const canEditTitle = this.get("model.details.can_edit");
+ const canLocalize = this.currentUser?.can_localize_content;
+
+ if (!canEditTitle && !canLocalize) {
+ return;
+ }
+
+ const titleLocalized = this.model?.fancy_title_localized;
+ if (!titleLocalized && canEditTitle) {
+ return this.set("editingTopic", true);
+ }
+
+ if (this.composer.isOpen) {
+ return;
+ }
+
+ if (canEditTitle && !canLocalize) {
+ return this._openComposerForEdit(topic, firstPost);
+ }
+
+ const topic = this.model;
+ const firstPost = await topic.firstPost();
+ if (titleLocalized && !canEditTitle) {
+ return this._openComposerForEditTranslation(topic, firstPost);
+ }
+
+ if (titleLocalized) {
+ const topicLocale = topic.locale;
+ const language = this.languageNameLookup.getLanguageName(topicLocale);
+ return this.dialog.alert({
+ message: i18n("topic.localizations.title_edit_warning.message", {
+ language,
+ }),
+ buttons: [
+ {
+ label: i18n(
+ "topic.localizations.title_edit_warning.action_original"
+ ),
+ class: "btn-primary",
+ action: () => this._openComposerForEdit(topic, firstPost),
+ },
+ {
+ label: i18n(
+ "topic.localizations.title_edit_warning.action_translation"
+ ),
+ class: "btn-default",
+ action: () =>
+ this._openComposerForEditTranslation(topic, firstPost),
+ },
+ ],
+ });
}
}
diff --git a/frontend/discourse/app/templates/topic.gjs b/frontend/discourse/app/templates/topic.gjs
index 844eab2f58e..f82e10a4a97 100644
--- a/frontend/discourse/app/templates/topic.gjs
+++ b/frontend/discourse/app/templates/topic.gjs
@@ -232,14 +232,7 @@ export default <template>
<TopicStatus @topic={{@controller.model}} />
<a
href={{@controller.model.url}}
- {{on
- "click"
- (if
- @controller.model.details.can_edit
- @controller.editTopic
- @controller.jumpTop
- )
- }}
+ {{on "click" @controller.handleTitleClick}}
class="fancy-title"
>
{{htmlSafe @controller.model.fancyTitle}}
diff --git a/spec/serializers/topic_view_serializer_spec.rb b/spec/serializers/topic_view_serializer_spec.rb
index 61277074e45..f4a1a082c7c 100644
--- a/spec/serializers/topic_view_serializer_spec.rb
+++ b/spec/serializers/topic_view_serializer_spec.rb
@@ -647,21 +647,57 @@ RSpec.describe TopicViewSerializer do
end
describe "#fancy_title" do
+ before { topic.update!(title: "Hur dur this is a title") }
+
it "returns the fancy title" do
- topic.update!(title: "Hur dur this is a title")
json = serialize_topic(topic, user)
expect(json[:fancy_title]).to eq("Hur dur this is a title")
end
- it "returns the localized fancy_title" do
- SiteSetting.content_localization_enabled = true
- Fabricate(:topic_localization, topic:, fancy_title: "X", locale: "ja")
- I18n.locale = "ja"
- topic.update!(locale: "en")
+ describe "with localizations" do
+ before do
+ Fabricate(:topic_localization, topic:, fancy_title: "X", locale: "ja")
+ I18n.locale = "ja"
+ end
- json = serialize_topic(topic, user)
- expect(json[:fancy_title]).to eq("X")
+ it "returns the localized fancy_title" do
+ SiteSetting.content_localization_enabled = false
+ json = serialize_topic(topic, user)
+ expect(json[:fancy_title]).to eq("Hur dur this is a title")
+
+ SiteSetting.content_localization_enabled = true
+ topic.update!(locale: "en")
+
+ json = serialize_topic(topic, user)
+ expect(json[:fancy_title]).to eq("X")
+ end
+
+ it "returns the fancy_title_localized if localized title returned" do
+ SiteSetting.content_localization_enabled = false
+ json = serialize_topic(topic, user)
+ expect(json[:fancy_title_localized]).to eq(nil)
+
+ SiteSetting.content_localization_enabled = true
+ json = serialize_topic(topic, user)
+ expect(json[:fancy_title_localized]).to eq(false)
+
+ topic.update!(locale: "en")
+ json = serialize_topic(topic, user)
+ expect(json[:fancy_title_localized]).to eq(true)
+ end
+
+ it "returns the locale" do
+ topic.update(locale: "ja")
+
+ SiteSetting.content_localization_enabled = false
+ json = serialize_topic(topic, user)
+ expect(json[:locale]).to eq(nil)
+
+ SiteSetting.content_localization_enabled = true
+ json = serialize_topic(topic, user)
+ expect(json[:locale]).to eq("ja")
+ end
end
end
diff --git a/spec/system/content_localization_spec.rb b/spec/system/content_localization_spec.rb
index 26278ac22dc..fb7c16dee28 100644
--- a/spec/system/content_localization_spec.rb
+++ b/spec/system/content_localization_spec.rb
@@ -33,10 +33,15 @@ describe "Content Localization" do
let(:topic_page) { PageObjects::Pages::Topic.new }
let(:topic_list) { PageObjects::Components::TopicList.new }
let(:composer) { PageObjects::Components::Composer.new }
+ let(:translation_composer) { PageObjects::Components::TranslationComposer.new }
let(:post_1_obj) { PageObjects::Components::Post.new(1) }
let(:post_3_obj) { PageObjects::Components::Post.new(3) }
let(:post_4_obj) { PageObjects::Components::Post.new(4) }
+ fab!(:topic_ja_localization) do
+ Fabricate(:topic_localization, topic:, locale: "ja", fancy_title: "孫子兵法からの人生戦略")
+ end
+
def scroll_to_post(post_number)
5.times do
break if page.has_css?("#post_#{post_number} .cooked", visible: :all, wait: 0)
@@ -45,7 +50,6 @@ describe "Content Localization" do
end
before do
- Fabricate(:topic_localization, topic:, locale: "ja", fancy_title: "孫子兵法からの人生戦略")
Fabricate(:topic_localization, topic:, locale: "es", fancy_title: "Estrategias de vida de ...")
Fabricate(:post_localization, post: post_1, locale: "ja", cooked: "傑作は単なる軍事戦略についてではありません")
@@ -200,6 +204,55 @@ describe "Content Localization" do
expect(page).to have_no_css("#fast-edit-input")
expect(edit_localized_post_dialog).to be_open
end
+
+ context "for topic titles" do
+ fab!(:untranslated_topic) { Fabricate(:post).topic }
+
+ it "shows title editor in composer when user can edit localizations" do
+ sign_in(japanese_user)
+
+ topic_page.visit_topic(untranslated_topic)
+ # not privileged therefore cannot edit topic title via title container
+ topic_page.click_topic_edit_title
+ expect(topic_page).to have_no_topic_title_editor
+
+ topic_page.visit_topic(topic)
+ # does not see regular title editor
+ # sent directly to translation composer to edit translation
+ topic_page.click_topic_edit_title
+ expect(topic_page).to have_no_topic_title_editor
+ expect(translation_composer).to have_translation_title(topic_ja_localization.title)
+ end
+
+ it "opens a dialog for choosing which title to edit for admins" do
+ admin.update(locale: "ja") # force the admin to view the jap localizations
+
+ sign_in(admin)
+
+ topic_page.visit_topic(untranslated_topic)
+ topic_page.click_topic_edit_title
+ expect(topic_page).to have_topic_title_editor
+
+ topic_page.visit_topic(topic)
+ expect(topic_page).to have_topic_title(topic_ja_localization.fancy_title)
+ topic_page.click_topic_edit_title
+ # does not see regular title editor
+ expect(topic_page).to have_no_topic_title_editor
+ # asked to use composer to edit original title
+ expect(edit_localized_post_dialog).to be_open
+
+ edit_localized_post_dialog.click_yes
+ expect(composer).to have_title(topic.title)
+
+ composer.close
+
+ # use translation composer to edit localized title
+ topic_page.click_topic_edit_title
+ expect(edit_localized_post_dialog).to be_open
+ edit_localized_post_dialog.click_no
+ expect(translation_composer).to have_translation_title(topic_ja_localization.title)
+ end
+ end
end
context "for post edit histories" do
diff --git a/spec/system/page_objects/components/composer.rb b/spec/system/page_objects/components/composer.rb
index 01be8feaa0b..9584874e525 100644
--- a/spec/system/page_objects/components/composer.rb
+++ b/spec/system/page_objects/components/composer.rb
@@ -62,6 +62,10 @@ module PageObjects
self
end
+ def has_title?(text)
+ find(".title-input input#reply-title", text:)
+ end
+
def fill_content(content)
find("#{@composer_id} .d-editor .d-editor-input").fill_in(with: content)
self
diff --git a/spec/system/page_objects/components/translation_composer.rb b/spec/system/page_objects/components/translation_composer.rb
new file mode 100644
index 00000000000..a51e1973dd3
--- /dev/null
+++ b/spec/system/page_objects/components/translation_composer.rb
@@ -0,0 +1,11 @@
+# frozen_string_literal: true
+
+module PageObjects
+ module Components
+ class TranslationComposer < PageObjects::Components::Composer
+ def has_translation_title?(value)
+ expect(find("#reply-control input#translated-topic-title").value).to eq(value)
+ end
+ end
+ end
+end
diff --git a/spec/system/page_objects/pages/topic.rb b/spec/system/page_objects/pages/topic.rb
index f4a76b46626..d8edd9e3607 100644
--- a/spec/system/page_objects/pages/topic.rb
+++ b/spec/system/page_objects/pages/topic.rb
@@ -47,6 +47,14 @@ module PageObjects
has_css?("h1 .fancy-title", text: text)
end
+ def has_topic_title_editor?
+ has_css?("#topic-title input#edit-title")
+ end
+
+ def has_no_topic_title_editor?
+ has_no_css?("#topic-title input#edit-title")
+ end
+
def has_post_content?(post_number:, content:)
find(post_by_number_selector(post_number)).has_content?(content)
end
@@ -186,6 +194,10 @@ module PageObjects
within_topic_footer_buttons { find(".bookmark-menu-trigger").click }
end
+ def click_topic_edit_title
+ find("#topic-title .fancy-title").click
+ end
+
def has_topic_bookmarked?(topic)
within_topic_footer_buttons do
has_css?(".bookmark-menu-trigger.bookmarked", text: "Edit Bookmark")