Revert "FEATURE: Add post language on creating a new post (#33001)" (#33157)

This reverts commit b55af383f779d01d983a3ccf1149438aee9c6725.
This commit is contained in:
Keegan George
2025-06-11 08:01:56 -07:00
committed by GitHub
parent b55af383f7
commit d5b72a54ae
19 changed files with 65 additions and 228 deletions

View File

@ -1,10 +1,8 @@
import Component from "@ember/component";
import { hash } from "@ember/helper";
import { alias } from "@ember/object/computed";
import { service } from "@ember/service";
import { htmlSafe } from "@ember/template";
import { classNames } from "@ember-decorators/component";
import PostLanguageSelector from "discourse/components/post-language-selector";
import discourseComputed from "discourse/lib/decorators";
import escape from "discourse/lib/escape";
import { iconHTML } from "discourse/lib/icon-library";
@ -30,9 +28,6 @@ const TITLES = {
@classNames("composer-action-title")
export default class ComposerActionTitle extends Component {
@service currentUser;
@service siteSettings;
@alias("model.replyOptions") options;
@alias("model.action") action;
@ -69,20 +64,6 @@ export default class ComposerActionTitle extends Component {
}
}
get showPostLanguageSelector() {
const allowedActions = [CREATE_TOPIC, EDIT, REPLY];
if (
this.currentUser &&
this.siteSettings.experimental_content_localization &&
this.currentUser.can_localize_content &&
allowedActions.includes(this.model.action)
) {
return true;
}
return false;
}
_formatEditUserPost(userAvatar, userLink, postLink, originalUser) {
let editTitle = `
<a class="post-link" href="${postLink.href}">${postLink.anchor}</a>
@ -133,12 +114,5 @@ export default class ComposerActionTitle extends Component {
<span class="action-title" role="heading" aria-level="1">
{{this.actionTitle}}
</span>
{{#if this.showPostLanguageSelector}}
<PostLanguageSelector
@composerModel={{this.model}}
@selectedLanguage={{this.model.locale}}
/>
{{/if}}
</template>
}

View File

@ -10,7 +10,7 @@ export default class EditCategoryLocalizations extends buildCategoryPanel(
@service siteSettings;
get availableLocales() {
return this.siteSettings.available_content_localization_locales;
return JSON.parse(this.siteSettings.available_locales);
}
<template>

View File

@ -12,6 +12,21 @@ export default class LanguageSwitcher extends Component {
@service siteSettings;
@service router;
get localeOptions() {
const targetLanguages = (
this.siteSettings.experimental_content_localization_supported_locales ||
""
).split("|");
return JSON.parse(this.siteSettings.available_locales)
.filter(({ value }) => targetLanguages.includes(value))
.map(({ name, value }) => {
return {
label: name,
value,
};
});
}
@action
async changeLocale(locale) {
cookie("locale", locale, { path: "/" });
@ -36,16 +51,13 @@ export default class LanguageSwitcher extends Component {
>
<:content>
<DropdownMenu as |dropdown|>
{{#each
this.siteSettings.available_content_localization_locales
as |option|
}}
{{#each this.localeOptions as |option|}}
<dropdown.item
class="locale-options"
data-menu-option-id={{option.value}}
>
<DButton
@translatedLabel={{option.name}}
@translatedLabel={{option.label}}
@action={{fn this.changeLocale option.value}}
/>
</dropdown.item>

View File

@ -62,20 +62,14 @@ export default class PostTranslationsModal extends Component {
this.args.closeModal();
const composerOpts = {
await this.composer.open({
action: Composer.ADD_TRANSLATION,
draftKey: "translation",
warningsDisabled: true,
hijackPreview: this.originalPostContent,
post: this.args.model.post,
selectedTranslationLocale: locale.locale,
};
if (locale?.topic_localization) {
composerOpts.topicTitle = locale.topic_localization?.title;
}
await this.composer.open(composerOpts);
});
this.composer.model.set("reply", locale.raw);
}

View File

@ -1,54 +0,0 @@
import Component from "@glimmer/component";
import { fn } from "@ember/helper";
import { action } from "@ember/object";
import { service } from "@ember/service";
import DButton from "discourse/components/d-button";
import DropdownMenu from "discourse/components/dropdown-menu";
import DMenu from "float-kit/components/d-menu";
export default class PostLanguageSelector extends Component {
@service siteSettings;
@action
selectPostLanguage(locale) {
this.args.composerModel.locale = locale;
this.dMenu.close();
}
@action
onRegisterApi(api) {
this.dMenu = api;
}
<template>
<DMenu
@identifier="post-language-selector"
@title="Post Language"
@icon="globe"
@label={{@selectedLanguage}}
@modalForMobile={{true}}
@onRegisterApi={{this.onRegisterApi}}
@class="btn-transparent btn-small post-language-selector"
>
<:content>
<DropdownMenu as |dropdown|>
{{#each
this.siteSettings.available_content_localization_locales
as |locale|
}}
<dropdown.item
class="locale=options"
data-menu-option-id={{locale.value}}
>
<DButton
@translatedLabel={{locale.name}}
@title={{locale.value}}
@action={{fn this.selectPostLanguage locale.value}}
/>
</dropdown.item>
{{/each}}
</DropdownMenu>
</:content>
</DMenu>
</template>
}

View File

@ -5,8 +5,6 @@ import { service } from "@ember/service";
import DEditor from "discourse/components/d-editor";
import TextField from "discourse/components/text-field";
import lazyHash from "discourse/helpers/lazy-hash";
import { popupAjaxError } from "discourse/lib/ajax-error";
import PostLocalization from "discourse/models/post-localization";
import { i18n } from "discourse-i18n";
import DropdownSelectBox from "select-kit/components/dropdown-select-box";
@ -14,19 +12,29 @@ export default class PostTranslationEditor extends Component {
@service composer;
@service siteSettings;
async findCurrentLocalization() {
try {
const { post_localizations } = await PostLocalization.find(
this.composer.model.post.id
get availableLocales() {
const allAvailableLocales = JSON.parse(this.siteSettings.available_locales);
const supportedLocales =
this.siteSettings.experimental_content_localization_supported_locales.split(
"|"
);
return post_localizations.find(
(localization) =>
localization.locale === this.composer.selectedTranslationLocale
);
} catch (error) {
popupAjaxError(error);
if (!supportedLocales.includes(this.siteSettings.default_locale)) {
supportedLocales.push(this.siteSettings.default_locale);
}
const filtered = allAvailableLocales.filter((locale) => {
return supportedLocales.includes(locale.value);
});
return filtered;
}
findCurrentLocalization() {
return this.composer.model.post.post_localizations.find(
(localization) =>
localization.locale === this.composer.selectedTranslationLocale
);
}
@action
@ -35,25 +43,13 @@ export default class PostTranslationEditor extends Component {
}
@action
async updateSelectedLocale(locale) {
updateSelectedLocale(locale) {
this.composer.selectedTranslationLocale = locale;
const currentLocalization = await this.findCurrentLocalization();
const currentLocalization = this.findCurrentLocalization();
if (currentLocalization) {
this.composer.model.set("reply", currentLocalization.raw);
if (currentLocalization?.topic_localization) {
this.composer.model.set(
"title",
currentLocalization.topic_localization.title
);
}
} else {
this.composer.model.setProperties({
reply: "",
title: "",
});
}
}
@ -63,7 +59,7 @@ export default class PostTranslationEditor extends Component {
@nameProperty="name"
@valueProperty="value"
@value={{this.composer.selectedTranslationLocale}}
@content={{this.siteSettings.available_content_localization_locales}}
@content={{this.availableLocales}}
@onChange={{this.updateSelectedLocale}}
@options={{hash
icon="globe"

View File

@ -73,13 +73,11 @@ const CLOSED = "closed",
shared_draft: "sharedDraft",
no_bump: "noBump",
draft_key: "draftKey",
locale: "locale",
},
_update_serializer = {
raw: "reply",
topic_id: "topic.id",
original_text: "originalText",
locale: "locale",
},
_edit_topic_serializer = {
title: "topic.title",
@ -88,7 +86,6 @@ const CLOSED = "closed",
featuredLink: "topic.featured_link",
original_title: "originalTitle",
original_tags: "originalTags",
locale: "locale",
},
_draft_serializer = {
reply: "reply",
@ -106,7 +103,6 @@ const CLOSED = "closed",
original_text: "originalText",
original_title: "originalTitle",
original_tags: "originalTags",
locale: "locale",
},
_add_draft_fields = {},
FAST_REPLY_LENGTH_THRESHOLD = 10000;
@ -118,7 +114,6 @@ export const SAVE_LABELS = {
[PRIVATE_MESSAGE]: "composer.create_pm",
[CREATE_SHARED_DRAFT]: "composer.create_shared_draft",
[EDIT_SHARED_DRAFT]: "composer.save_edit",
[ADD_TRANSLATION]: "composer.translations.save",
};
export const SAVE_ICONS = {
@ -209,7 +204,6 @@ export default class Composer extends RestModel {
@tracked post;
@tracked reply;
@tracked whisper;
@tracked locale = this.post?.locale || this.siteSettings.default_locale;
unlistTopic = false;
noBump = false;
@ -1182,7 +1176,6 @@ export default class Composer extends RestModel {
typingTime: this.typingTime,
composerTime: this.composerTime,
metaData: this.metaData,
locale: this.locale,
});
this.serialize(_create_serializer, createdPost);

View File

@ -367,6 +367,8 @@ export default class ComposerService extends Service {
return "composer.create_whisper";
} else if (privateMessage && modelAction === Composer.REPLY) {
return "composer.create_pm";
} else if (modelAction === Composer.ADD_TRANSLATION) {
return "composer.translations.save";
}
return SAVE_LABELS[modelAction];
@ -1385,7 +1387,6 @@ export default class ComposerService extends Service {
this.set("hijackPreview", opts.hijackPreview);
}
// TODO: fix this not working anymore? need to use `opts.locale` instead?
if (opts.selectedTranslationLocale) {
this.selectedTranslationLocale = opts.selectedTranslationLocale;
}
@ -1475,7 +1476,6 @@ export default class ComposerService extends Service {
action: CREATE_TOPIC,
draftKey: this.topicDraftKey,
draftSequence: 0,
locale: this.siteSettings.default_locale,
});
}
@ -1518,8 +1518,6 @@ export default class ComposerService extends Service {
isWarning: false,
hasTargetGroups: opts.hasGroups,
warningsDisabled: opts.warningsDisabled,
locale:
opts?.locale || opts?.post?.locale || this.siteSettings.default_locale,
});
if (!this.model.targetRecipients) {

View File

@ -9,15 +9,3 @@
color: var(--quaternary);
}
}
.post-language-selector-content {
z-index: z("composer", "dropdown");
}
.post-language-selector-trigger {
margin-left: 1rem;
.d-button-label {
text-transform: uppercase;
}
}

View File

@ -6,33 +6,20 @@ class PostLocalizationsController < ApplicationController
def show
guardian.ensure_can_localize_content!
params.require(:post_id)
params.require(%i[post_id])
localizations = PostLocalization.where(post_id: params[:post_id])
post = Post.find_by(id: params[:post_id])
return render json_error(I18n.t("not_found"), status: :not_found) if post.blank?
post_localizations = PostLocalization.where(post_id: post.id)
topic_localizations_by_locale = {}
if post.is_first_post?
TopicLocalization
.where(topic_id: post.topic_id)
.each { |tl| topic_localizations_by_locale[tl.locale] = tl }
end
post_localizations.each do |pl|
pl.define_singleton_method(:topic_localization) { topic_localizations_by_locale[pl.locale] }
end
render json: {
post_localizations:
if localizations
render json:
ActiveModel::ArraySerializer.new(
post_localizations,
localizations,
each_serializer: PostLocalizationSerializer,
root: false,
).as_json,
},
status: :ok
status: :ok
else
render json_error I18n.t("not_found"), status: :not_found
end
end
def create_or_update

View File

@ -244,11 +244,7 @@ class PostsController < ApplicationController
guardian.ensure_can_edit!(post)
changes = {
raw: params[:post][:raw],
edit_reason: params[:post][:edit_reason],
locale: params[:post][:locale],
}
changes = { raw: params[:post][:raw], edit_reason: params[:post][:edit_reason] }
Post.plugin_permitted_update_params.keys.each { |param| changes[param] = params[:post][param] }
@ -857,7 +853,6 @@ class PostsController < ApplicationController
visible
draft_key
composer_version
locale
]
Post.plugin_permitted_create_params.each do |key, value|

View File

@ -115,20 +115,6 @@ class SiteSetting < ActiveRecord::Base
LocaleSiteSetting.values.to_json
end
if SiteSetting.experimental_content_localization?
client_settings << :available_content_localization_locales
end
def self.available_content_localization_locales
supported_locales = SiteSetting.experimental_content_localization_supported_locales.split("|")
default_locale = SiteSetting.default_locale
if default_locale.present? && !supported_locales.include?(default_locale)
supported_locales << default_locale
end
LocaleSiteSetting.values.select { |locale| supported_locales.include?(locale[:value]) }
end
def self.topic_title_length
min_topic_title_length..max_topic_title_length
end

View File

@ -1,13 +1,5 @@
# frozen_string_literal: true
class PostLocalizationSerializer < ApplicationSerializer
attributes :id, :post_id, :post_version, :locale, :raw, :topic_localization
def topic_localization
TopicLocalizationSerializer.new(object.topic_localization, root: false).as_json
end
def include_topic_localization?
object.respond_to?(:topic_localization) && object.topic_localization.present?
end
attributes :id, :post_id, :locale, :raw
end

View File

@ -1,5 +0,0 @@
# frozen_string_literal: true
class TopicLocalizationSerializer < ApplicationSerializer
attributes :id, :topic_id, :locale, :title, :fancy_title
end

View File

@ -555,7 +555,6 @@ class PostCreator
via_email
raw_email
action_code
locale
].each { |a| post.public_send("#{a}=", @opts[a]) if @opts[a].present? }
post.extract_quoted_post_numbers

View File

@ -40,7 +40,7 @@ class PostRevisor
end
end
POST_TRACKED_FIELDS = %w[raw cooked edit_reason user_id wiki post_type locale]
POST_TRACKED_FIELDS = %w[raw cooked edit_reason user_id wiki post_type]
attr_reader :category_changed, :post_revision

View File

@ -14,18 +14,13 @@ RSpec.describe "Anonymous user language switcher", type: :system do
end
before do
SiteSetting.default_locale = "en"
SiteSetting.experimental_content_localization_supported_locales = "es|ja"
SiteSetting.experimental_content_localization = true
SiteSetting.allow_user_locale = true
SiteSetting.set_locale_from_cookie = true
if SiteSetting.client_settings.exclude?(:available_content_localization_locales)
SiteSetting.client_settings << :available_content_localization_locales
end
end
it "only shows the language switcher based on what is in target languages" do
SiteSetting.experimental_content_localization_supported_locales = "es|ja"
SiteSetting.experimental_anon_language_switcher = false
visit("/")
@ -35,7 +30,6 @@ RSpec.describe "Anonymous user language switcher", type: :system do
visit("/")
switcher.expand
expect(switcher).to have_content("English (US)")
expect(switcher).to have_content("日本語")
expect(switcher).to have_content("Español")

View File

@ -20,16 +20,7 @@ describe "Edit Category Localizations", type: :system do
end
context "when content localization setting is enabled" do
before do
SiteSetting.default_locale = "en"
SiteSetting.experimental_content_localization = true
SiteSetting.experimental_content_localization_supported_locales = "es|fr"
SiteSetting.experimental_content_localization_allowed_groups = Group::AUTO_GROUPS[:everyone]
if SiteSetting.client_settings.exclude?(:available_content_localization_locales)
SiteSetting.client_settings << :available_content_localization_locales
end
end
before { SiteSetting.experimental_content_localization = true }
it "should show the localization tab" do
category_page.visit_settings(category)

View File

@ -13,19 +13,15 @@ describe "Post translations", type: :system do
before do
sign_in(user)
SiteSetting.default_locale = "en"
SiteSetting.experimental_content_localization_supported_locales = "fr|es|pt_BR"
SiteSetting.experimental_content_localization_supported_locales = "en|fr|es|pt_BR"
SiteSetting.experimental_content_localization = true
SiteSetting.experimental_content_localization_allowed_groups = Group::AUTO_GROUPS[:everyone]
if SiteSetting.client_settings.exclude?(:available_content_localization_locales)
SiteSetting.client_settings << :available_content_localization_locales
end
SiteSetting.post_menu =
"read|like|copyLink|flag|edit|bookmark|delete|admin|reply|addTranslation"
end
context "when a post does not have translations" do
it "should only show the languages listed in the site setting and the default locale" do
it "should only show the languages listed in the site setting" do
topic_page.visit_topic(topic)
find("#post_#{post.post_number} .post-action-menu__add-translation").click
translation_selector.expand
@ -38,11 +34,12 @@ describe "Post translations", type: :system do
end
it "always includes the site's default locale in the list of available languages" do
SiteSetting.default_locale = "de"
topic_page.visit_topic(topic)
find("#post_#{post.post_number} .post-action-menu__add-translation").click
translation_selector.expand
expect(all(".translation-selector-dropdown .select-kit-collection li").count).to eq(4)
expect(translation_selector).to have_option_value("en")
expect(all(".translation-selector-dropdown .select-kit-collection li").count).to eq(5)
expect(translation_selector).to have_option_value("de")
end
it "allows a user to translate a post" do