A platform for community discussion. Free, open, simple.
FEATURE: Allow selection of multiple content languages when viewing topics and posts (#42128)
Natalie Taycff8f6ea4d30
diff --git a/.skills/discourse-content-localization/SKILL.md b/.skills/discourse-content-localization/SKILL.mdindex d5a680c70a4..e9d1acf8fa2 100644--- a/.skills/discourse-content-localization/SKILL.md+++ b/.skills/discourse-content-localization/SKILL.md@@ -19,7 +19,7 @@ Use this before localizing dynamic Discourse content such as Sidebar sections, G- Add `include Localizable` to the source model and a nullable `locale` column with limit 20.- Add `<Model>Localization` with `include LocaleMatchable`, `belongs_to :model`, a `locale` string limit 20, translated fields with source-model length limits, and a unique `(model_id, locale)` index.- Use `get_localization` for exact, normalized, and optional default-locale fallback behavior. Do not reimplement locale fallback in serializers.-- Add `ContentLocalization.show_translated_<model>?` using `SiteSetting.content_localization_enabled`, source `locale.present?`, and `!model.in_user_locale?`. Match Post/Topic only when show-original behavior is relevant.+- Add `ContentLocalization.show_translated_<model>?` using `SiteSetting.content_localization_enabled`, source `locale.present?`, and `!model.in_user_locale?`. Preserve existing model-specific presentation gates; do not copy them to unrelated model types without explicit product intent.- Preload in every list, boot, site JSON, admin list, and controller path that serializes localized fields. Use `includes(:localizations)` or `includes(:localizations, children: :localizations)` as needed.- Serializer methods should return `localization.field.presence || original_field` and expose localization rows only to users who can edit them.@@ -44,6 +44,7 @@ Use this before localizing dynamic Discourse content such as Sidebar sections, G- Model specs: validations, uniqueness, dependency cleanup, fallback via `get_localization`.- Serializer/request specs: localized value when enabled, original value when disabled/source locale missing/same locale, editor-only `localizations`, and no N+1 on hot routes.+- When changing model-specific presentation gates, test authenticated and anonymous behavior and verify unrelated localizable model types remain unaffected.- UI/system or QUnit specs: authorized editor can view/add/remove localization rows on the chosen edit surface; unauthorized users cannot.- discourse-ai specs: detector text, localizer writes, scheduled job enqueue/credit gates, regular job limits/skips/errors, and candidate completion if the model appears in translation-progress UI.diff --git a/app/assets/stylesheets/common/base/embed-mode.scss b/app/assets/stylesheets/common/base/embed-mode.scssindex b47e078caac..b2725b785a5 100644--- a/app/assets/stylesheets/common/base/embed-mode.scss+++ b/app/assets/stylesheets/common/base/embed-mode.scss@@ -133,7 +133,8 @@ body.embed-mode {}// Hide admin menu in the progress bar for embed context- #topic-progress-wrapper .topic-admin-menu-trigger {+ #topic-progress-wrapper .topic-admin-menu-trigger,+ #topic-progress-wrapper .topic-content-language-preferences {display: none;}diff --git a/app/assets/stylesheets/common/base/topic-footer.scss b/app/assets/stylesheets/common/base/topic-footer.scssindex 2ce0311e39c..1c3df6bf20f 100644--- a/app/assets/stylesheets/common/base/topic-footer.scss+++ b/app/assets/stylesheets/common/base/topic-footer.scss@@ -135,11 +135,16 @@width: auto;margin-top: var(--border-offset);- .topic-admin-menu-trigger {+ .topic-admin-menu-trigger,+ .topic-content-language-preferences {border-radius: 0;background: var(--secondary);border: 1px solid var(--primary-low);}++ .topic-admin-menu-trigger + .topic-content-language-preferences {+ margin-inline-start: var(--border-offset);+ }}}diff --git a/app/assets/stylesheets/common/base/user.scss b/app/assets/stylesheets/common/base/user.scssindex 248c15f7ced..358a9786f5c 100644--- a/app/assets/stylesheets/common/base/user.scss+++ b/app/assets/stylesheets/common/base/user.scss@@ -1081,7 +1081,11 @@margin-top: 0.5em;}- .pref-show-original-content {+ .pref-understood-languages {+ margin-top: var(--space-2);+ }++ .pref-automatically-translate {margin-top: var(--space-4);margin-bottom: var(--space-4);}diff --git a/app/assets/stylesheets/common/components/_index.scss b/app/assets/stylesheets/common/components/_index.scssindex a2812432b0e..882c9106c8a 100644--- a/app/assets/stylesheets/common/components/_index.scss+++ b/app/assets/stylesheets/common/components/_index.scss@@ -19,6 +19,7 @@@import "calendar-date-time-input";@import "composer-toggle-switch";@import "convert-to-public-topic-modal";+@import "content-language-preferences-modal";@import "d-autocomplete";@import "d-filter-controls";@import "docked-composer";diff --git a/app/assets/stylesheets/common/components/content-language-preferences-modal.scss b/app/assets/stylesheets/common/components/content-language-preferences-modal.scssnew file mode 100644index 00000000000..8d090f57557--- /dev/null+++ b/app/assets/stylesheets/common/components/content-language-preferences-modal.scss@@ -0,0 +1,13 @@+.content-language-preferences-modal {+ &__understood {+ display: grid;+ gap: var(--space-1);+ width: 100%;++ > span {+ color: var(--primary);+ font-size: var(--font-down-1-rem);+ font-weight: 600;+ }+ }+}diff --git a/app/controllers/about_controller.rb b/app/controllers/about_controller.rbindex 97a16c7c8d6..a7df68e311f 100644--- a/app/controllers/about_controller.rb+++ b/app/controllers/about_controller.rb@@ -8,12 +8,7 @@ class AboutController < ApplicationControllerdef indexreturn redirect_to path("/login") if SiteSetting.login_required? && current_user.nil?- @about =- About.new(- current_user,- locale: I18n.locale,- show_original: ContentLocalization.show_original?(guardian),- )+ @about = About.new(current_user, locale: I18n.locale)@title = "#{I18n.t("js.about.simple_title")} - #{@about.title}"@description_meta = @about.descriptionrespond_to do |format|diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rbindex f5e15ca41a2..823f1f6a589 100644--- a/app/controllers/users_controller.rb+++ b/app/controllers/users_controller.rb@@ -2208,7 +2208,9 @@ class UsersController < ApplicationControllereditable_custom_fields = User.editable_user_custom_fields(by_staff: current_user.try(:staff?))permitted << { custom_fields: editable_custom_fields } if editable_custom_fields.present?- permitted.concat UserUpdater::OPTION_ATTR+ permitted.concat(UserUpdater::OPTION_ATTR - [:understood_languages])+ permitted << UserUpdater::LEGACY_SHOW_ORIGINAL_CONTENT_ATTR+ permitted << { understood_languages: [] }permitted.concat UserUpdater::CATEGORY_IDS.keys.map { |k| { k => [] } }permitted.concat UserUpdater::TAG_NAMES.keyspermitted << UserUpdater::NOTIFICATION_SCHEDULE_ATTRSdiff --git a/app/helpers/topics_helper.rb b/app/helpers/topics_helper.rbindex 7819136987f..9fff9f19f3e 100644--- a/app/helpers/topics_helper.rb+++ b/app/helpers/topics_helper.rb@@ -22,16 +22,18 @@ module TopicsHelperenddef localize_topic_view_content(topic_view)- return if cookies.key?(ContentLocalization::SHOW_ORIGINAL_COOKIE)- return if current_user&.user_option&.show_original_content-# locale param is appropriately set in the application controller# depending on site settings and presence of userlocale = I18n.locale- LocalizationAttributesReplacer.replace_topic_attributes(topic_view.topic, locale)+ if ContentLocalization.show_translated_topic?(topic_view.topic, guardian)+ LocalizationAttributesReplacer.replace_topic_attributes(topic_view.topic, locale)+ end+topic_view.posts.each do |post|- LocalizationAttributesReplacer.replace_post_attributes(post, locale)+ if ContentLocalization.show_translated_post?(post, guardian)+ LocalizationAttributesReplacer.replace_post_attributes(post, locale)+ endendendenddiff --git a/app/models/tag.rb b/app/models/tag.rbindex 031030fe4a2..af1508198fe 100644--- a/app/models/tag.rb+++ b/app/models/tag.rb@@ -176,13 +176,11 @@ class Tag < ActiveRecord::Baseendtags_by_id = Tag.where(id: tag_data.map(&:tag_id)).includes(:localizations).index_by(&:id)- show_localized = !ContentLocalization.show_original?(guardian)-tag_data.filter_map do |row|tag = tags_by_id[row.tag_id]next unless tag- name = show_localized ? (tag.get_localization&.name || tag.name) : tag.name+ name = tag.get_localization&.name || tag.nameslug = row.tag_slug.presence || "#{row.tag_id}-tag"{ id: tag.id, name:, slug: }enddiff --git a/app/models/user_option.rb b/app/models/user_option.rbindex ce9dd513b9a..c75922af66e 100644--- a/app/models/user_option.rb+++ b/app/models/user_option.rb@@ -75,6 +75,7 @@ class UserOption < ActiveRecord::Basevalidates :email_level, inclusion: { in: UserOption.email_level_types.values }validates :email_messages_level, inclusion: { in: UserOption.email_level_types.values }validates :timezone, timezone: true+ validate :understood_languages_are_supported, if: :will_save_change_to_understood_languages?def set_defaultsself.mailing_list_mode = SiteSetting.default_email_mailing_list_mode@@ -248,6 +249,14 @@ class UserOption < ActiveRecord::Baseprivate+ def understood_languages_are_supported+ if understood_languages.all? { |locale| LocaleSiteSetting.supported_locales.include?(locale) }+ return+ end++ errors.add(:understood_languages, :invalid)+ end+def update_hide_profile_and_presenceif hide_profile_changed? || hide_presence_changed?self.hide_profile_and_presence = hide_profile || hide_presence@@ -271,6 +280,7 @@ end# allow_private_messages :boolean default(TRUE), not null# auto_image_caption :boolean default(FALSE), not null# auto_track_topics_after_msecs :integer+# automatically_translate :boolean default(TRUE), not null# automatically_unpin_topics :boolean default(TRUE), not null# bookmark_auto_delete_preference :integer default(3), not null# chat_announce_new_messages :boolean default(TRUE), not null@@ -335,6 +345,7 @@ end# timezone :string# title_count_mode_key :integer default(0), not null# topics_unread_when_closed :boolean default(TRUE), not null+# understood_languages :string default([]), not null, is an Array# watched_precedence_over_muted :boolean default(FALSE), not null# color_scheme_id :integer# dark_scheme_id :integerdiff --git a/app/serializers/current_user_option_serializer.rb b/app/serializers/current_user_option_serializer.rbindex eb42dbe7cd6..6201069ab33 100644--- a/app/serializers/current_user_option_serializer.rb+++ b/app/serializers/current_user_option_serializer.rb@@ -28,12 +28,18 @@ class CurrentUserOptionSerializer < ApplicationSerializer:composition_mode,:interface_color_mode,:show_original_content,- :send_shortcut+ :send_shortcut,+ :automatically_translate,+ :understood_languagesdef likes_notifications_disabledobject.likes_notifications_disabled?end+ def show_original_content+ !object.automatically_translate+ end+def include_redirected_to_top?object.redirected_to_top.present?enddiff --git a/app/serializers/post_serializer.rb b/app/serializers/post_serializer.rbindex 2d84edb86d7..e71336ed0fd 100644--- a/app/serializers/post_serializer.rb+++ b/app/serializers/post_serializer.rb@@ -284,7 +284,7 @@ class PostSerializer < BasicPostSerializerdef include_localized_oneboxes?SiteSetting.content_localization_enabled && @topic_view.present? &&- !ContentLocalization.show_original?(scope) &&+ ContentLocalization.automatically_translate?(scope) &&@topic_view.localized_oneboxes[object.id].present?enddiff --git a/app/serializers/topic_view_serializer.rb b/app/serializers/topic_view_serializer.rbindex a88afe6bd8c..fce8c9af2c7 100644--- a/app/serializers/topic_view_serializer.rb+++ b/app/serializers/topic_view_serializer.rb@@ -78,6 +78,7 @@ class TopicViewSerializer < ApplicationSerializer:user_last_posted_at,:is_shared_draft,:slow_mode_enabled_until,+ :has_localized_content,:can_localize_topic,:is_nested_view,)@@ -320,6 +321,14 @@ class TopicViewSerializer < ApplicationSerializerobject.topic.visibility_reason_id.present?end+ def has_localized_content+ object.has_localized_content?+ end++ def include_has_localized_content?+ SiteSetting.content_localization_enabled+ end+def can_localize_topictrueenddiff --git a/app/serializers/user_option_serializer.rb b/app/serializers/user_option_serializer.rbindex 37803ea4209..cf49004e599 100644--- a/app/serializers/user_option_serializer.rb+++ b/app/serializers/user_option_serializer.rb@@ -51,7 +51,9 @@ class UserOptionSerializer < ApplicationSerializer:composition_mode,:interface_color_mode,:show_original_content,- :send_shortcut+ :send_shortcut,+ :automatically_translate,+ :understood_languagesdef auto_track_topics_after_msecsobject.auto_track_topics_after_msecs || SiteSetting.default_other_auto_track_topics_after_msecs@@ -69,4 +71,8 @@ class UserOptionSerializer < ApplicationSerializerdef theme_idsobject.theme_ids.presence || [SiteSetting.default_theme_id]end++ def show_original_content+ !object.automatically_translate+ endenddiff --git a/app/services/user_updater.rb b/app/services/user_updater.rbindex fa636622955..c8e538753af 100644--- a/app/services/user_updater.rb+++ b/app/services/user_updater.rb@@ -1,6 +1,8 @@# frozen_string_literal: trueclass UserUpdater+ LEGACY_SHOW_ORIGINAL_CONTENT_ATTR = :show_original_content+CATEGORY_IDS = {watched_first_post_category_ids: :watching_first_post,watched_category_ids: :watching,@@ -61,8 +63,9 @@ class UserUpdaterwatched_precedence_over_mutedtopics_unread_when_closedcomposition_mode- show_original_contentsend_shortcut+ automatically_translate+ understood_languages]NOTIFICATION_SCHEDULE_ATTRS = -> do@@ -82,6 +85,12 @@ class UserUpdaterenddef update(attributes = {})+ if attributes.key?(LEGACY_SHOW_ORIGINAL_CONTENT_ATTR)+ attributes[:automatically_translate] = attributes[LEGACY_SHOW_ORIGINAL_CONTENT_ATTR].to_s !=+ "true" unless attributes.key?(:automatically_translate)+ attributes.delete(LEGACY_SHOW_ORIGINAL_CONTENT_ATTR)+ end+user_profile = user.user_profileuser_profile.dismissed_banner_key = attributes[:dismissed_banner_key] if attributes[:dismissed_banner_key@@ -182,6 +191,13 @@ class UserUpdaterattributes[:text_size]end+ if attributes.key?(:locale) || attributes.key?(:understood_languages)+ understood_languages =+ attributes.fetch(:understood_languages) { user.user_option.understood_languages }+ interface_locale = user.effective_locale+ attributes[:understood_languages] = [interface_locale, *understood_languages].compact.uniq+ end+OPTION_ATTR.each do |attribute|if attributes.key?(attribute)save_options = truediff --git a/config/locales/client.en.yml b/config/locales/client.en.ymlindex 434b8782218..e8ff54b7de9 100644--- a/config/locales/client.en.yml+++ b/config/locales/client.en.yml@@ -1818,7 +1818,11 @@ en:enable_smart_lists: "Enable smart lists when writing in the composer"enable_defer: "Enable mark topics as unread"enable_markdown_monospace_font: "Use monospace font in composer's Markdown mode"- show_original_content: "Show original content instead of translated content."+ automatically_translate: "Automatically translate topics and posts in other languages"+ content_languages:+ title: "Content languages"+ understood: "Languages I understand"+ understood_description: "Topics and posts in these languages won’t be translated. Your interface language is always included."experimental_sidebar:enable: "Enable sidebar"options: "Options"@@ -5206,6 +5210,12 @@ en:tap_to_show_translation: "Tap here to show translation"ai_translation_disclaimer: "AI-generated translations may be inaccurate."+ content_localization:+ preferences:+ title: "Content language preferences"+ login_to_set_languages: "Log in to set more languages"+ interface_language_mandatory: "Your interface language is always included."+category:none: "(no category)"all: "All categories"diff --git a/db/migrate/20260728134532_add_content_language_preferences_to_user_options.rb b/db/migrate/20260728134532_add_content_language_preferences_to_user_options.rbnew file mode 100644index 00000000000..de9a5afdb5a--- /dev/null+++ b/db/migrate/20260728134532_add_content_language_preferences_to_user_options.rb@@ -0,0 +1,18 @@+# frozen_string_literal: true++class AddContentLanguagePreferencesToUserOptions < ActiveRecord::Migration[8.0]+ def up+ add_column :user_options, :automatically_translate, :boolean, default: true, null: false+ add_column :user_options, :understood_languages, :string, array: true, default: [], null: false++ execute <<~SQL+ UPDATE user_options+ SET automatically_translate = FALSE+ WHERE show_original_content = TRUE+ SQL+ end++ def down+ raise ActiveRecord::IrreversibleMigration+ end+enddiff --git a/db/structure.sql b/db/structure.sqlindex 232f7c5ed16..e0e40b18c19 100644--- a/db/structure.sql+++ b/db/structure.sql@@ -11767,6 +11767,8 @@ CREATE TABLE public.user_options (chat_announce_new_messages boolean DEFAULT true NOT NULL,chat_new_message_sound boolean DEFAULT false NOT NULL,push_notification_level integer DEFAULT 1 NOT NULL,+ automatically_translate boolean DEFAULT true NOT NULL,+ understood_languages character varying[] DEFAULT '{}'::character varying[] NOT NULL,send_shortcut integer DEFAULT 0 NOT NULL);@@ -23072,6 +23074,7 @@ SET search_path TO "$user", public;INSERT INTO "schema_migrations" (version) VALUES('20260728162521'),('20260728162516'),+('20260728134532'),('20260728071552'),('20260728045008'),('20260727085824'),diff --git a/frontend/discourse/app/components/language-switcher.gjs b/frontend/discourse/app/components/language-switcher.gjsindex 8c02120e882..5186996e909 100644--- a/frontend/discourse/app/components/language-switcher.gjs+++ b/frontend/discourse/app/components/language-switcher.gjs@@ -4,14 +4,12 @@ import { action } from "@ember/object";import { service } from "@ember/service";import DMenu from "discourse/float-kit/components/d-menu";import { ajax } from "discourse/lib/ajax";-import cookie, { removeCookie } from "discourse/lib/cookie";+import cookie from "discourse/lib/cookie";import DButton from "discourse/ui-kit/d-button";import DDropdownMenu from "discourse/ui-kit/d-dropdown-menu";import dIcon from "discourse/ui-kit/helpers/d-icon";import I18n, { i18n } from "discourse-i18n";-const SHOW_ORIGINAL_COOKIE = "content-localization-show-original";-export default class LanguageSwitcher extends Component {@service siteSettings;@service languageNameLookup;@@ -21,17 +19,14 @@ export default class LanguageSwitcher extends Component {async changeLocale(locale) {if (this.currentUser) {this.currentUser.set("locale", locale);- this.currentUser.set("user_option.show_original_content", false);await ajax(`/u/${this.currentUser.username}.json`, {type: "PUT",- data: { locale, show_original_content: false },+ data: { locale },});} else {cookie("locale", locale, { path: "/" });}- removeCookie(SHOW_ORIGINAL_COOKIE, { path: "/" });-this.dMenu.close();// content should switch immediately,// but we need a hard refresh here for controls to switch to the new localediff --git a/frontend/discourse/app/components/modal/content-language-preferences.gjs b/frontend/discourse/app/components/modal/content-language-preferences.gjsnew file mode 100644index 00000000000..432d08a5c5b--- /dev/null+++ b/frontend/discourse/app/components/modal/content-language-preferences.gjs@@ -0,0 +1,257 @@+import Component from "@glimmer/component";+import { cached, tracked } from "@glimmer/tracking";+import { hash } from "@ember/helper";+import { action } from "@ember/object";+import { service } from "@ember/service";+import Form from "discourse/components/form";+import { ajax } from "discourse/lib/ajax";+import { popupAjaxError } from "discourse/lib/ajax-error";+import {+ AUTOMATICALLY_TRANSLATE_COOKIE,+ AUTOMATICALLY_TRANSLATE_COOKIE_EXPIRY,+ automaticallyTranslate,+ contentLanguageOptions,+ normalizeUnderstoodLanguages,+} from "discourse/lib/content-localization";+import cookie from "discourse/lib/cookie";+import getURL from "discourse/lib/get-url";+import MultiSelect from "discourse/select-kit/components/multi-select";+import DButton from "discourse/ui-kit/d-button";+import DModal from "discourse/ui-kit/d-modal";+import DModalCancel from "discourse/ui-kit/d-modal-cancel";+import I18n, { i18n } from "discourse-i18n";++export default class ContentLanguagePreferencesModal extends Component {+ @service currentUser;+ @service siteSettings;+ @service languageNameLookup;++ @tracked formApi;+ @tracked saving = false;++ @cached+ get data() {+ const interfaceLanguage = this.currentUser?.effective_locale ?? I18n.locale;++ return {+ interfaceLanguage,+ understoodLanguages: normalizeUnderstoodLanguages(+ this.currentUser?.user_option?.understood_languages,+ interfaceLanguage+ ),+ automaticallyTranslate: automaticallyTranslate(this.currentUser),+ };+ }++ get interfaceLanguageOptions() {+ return this.siteSettings.available_locales.map(({ value }) => ({+ name: this.languageNameLookup.getLanguageName(value),+ value,+ id: value,+ }));+ }++ get understoodLanguageOptions() {+ const interfaceLanguage =+ this.formApi?.get("interfaceLanguage") ?? this.data.interfaceLanguage;+ const understoodLanguages =+ this.formApi?.get("understoodLanguages") ?? this.data.understoodLanguages;++ return contentLanguageOptions(+ this.interfaceLanguageOptions,+ this.siteSettings.available_content_localization_locales,+ [interfaceLanguage, ...understoodLanguages]+ );+ }++ get canChangeInterfaceLanguage() {+ return (+ this.siteSettings.allow_user_locale &&+ (this.currentUser || this.siteSettings.set_locale_from_cookie)+ );+ }++ get interfaceLanguageReadOnly() {+ return !this.canChangeInterfaceLanguage;+ }++ get loginPath() {+ return getURL("/login");+ }++ @action+ registerFormApi(formApi) {+ this.formApi = formApi;+ }++ @action+ submit() {+ this.formApi.submit();+ }++ @action+ async setInterfaceLanguage(value, { set }) {+ await set("interfaceLanguage", value);+ await set(+ "understoodLanguages",+ normalizeUnderstoodLanguages(+ this.formApi.get("understoodLanguages"),+ value+ )+ );+ }++ @action+ setUnderstoodLanguages(value, { set }) {+ return set(+ "understoodLanguages",+ normalizeUnderstoodLanguages(value, this.formApi.get("interfaceLanguage"))+ );+ }++ @action+ async save(data) {+ this.saving = true;++ try {+ if (this.currentUser) {+ const understoodLanguages = normalizeUnderstoodLanguages(+ data.understoodLanguages,+ data.interfaceLanguage+ );++ const preferences = {+ automatically_translate: data.automaticallyTranslate,+ understood_languages: understoodLanguages,+ };+ if (this.canChangeInterfaceLanguage) {+ preferences.locale = data.interfaceLanguage;+ }++ await ajax(`/u/${this.currentUser.username}.json`, {+ type: "PUT",+ data: preferences,+ });+ if (this.canChangeInterfaceLanguage) {+ this.currentUser.set("locale", data.interfaceLanguage);+ this.currentUser.set("effective_locale", data.interfaceLanguage);+ }+ this.currentUser.set(+ "user_option.automatically_translate",+ data.automaticallyTranslate+ );+ this.currentUser.set(+ "user_option.understood_languages",+ understoodLanguages+ );+ } else {+ if (this.canChangeInterfaceLanguage) {+ cookie("locale", data.interfaceLanguage, { path: "/" });+ }+ cookie(AUTOMATICALLY_TRANSLATE_COOKIE, data.automaticallyTranslate, {+ path: "/",+ expires: AUTOMATICALLY_TRANSLATE_COOKIE_EXPIRY,+ });+ }++ this.args.closeModal();+ window.location.reload();+ } catch (error) {+ popupAjaxError(error);+ this.saving = false;+ }+ }++ <template>+ <DModal+ @title={{i18n "content_localization.preferences.title"}}+ @closeModal={{@closeModal}}+ class="content-language-preferences-modal"+ >+ <:body>+ <Form+ @data={{this.data}}+ @onSubmit={{this.save}}+ @onRegisterApi={{this.registerFormApi}}+ as |form transientData|+ >+ <form.Field+ @name="interfaceLanguage"+ @type="select"+ @title={{i18n "user.locale.title"}}+ @validation="required"+ @format="full"+ @onSet={{this.setInterfaceLanguage}}+ @disabled={{this.interfaceLanguageReadOnly}}+ as |field|+ >+ <field.Control as |select|>+ {{#each this.interfaceLanguageOptions as |language|}}+ <select.Option @value={{language.value}}>+ {{language.name}}+ </select.Option>+ {{/each}}+ </field.Control>+ </form.Field>++ {{#if this.currentUser}}+ <form.Field+ @name="understoodLanguages"+ @type="custom"+ @title={{i18n "user.content_languages.understood"}}+ @description={{i18n+ "user.content_languages.understood_description"+ }}+ @showOptional={{false}}+ @format="full"+ @onSet={{this.setUnderstoodLanguages}}+ as |field|+ >+ <field.Control>+ <MultiSelect+ @valueProperty="value"+ @langProperty="value"+ @content={{this.understoodLanguageOptions}}+ @value={{field.value}}+ @onChange={{field.set}}+ @mandatoryValues={{transientData.interfaceLanguage}}+ @mandatoryValueTitle={{i18n+ "content_localization.preferences.interface_language_mandatory"+ }}+ @options={{hash filterable=true}}+ />+ </field.Control>+ </form.Field>+ {{else}}+ <div class="content-language-preferences-modal__understood">+ <span>{{i18n "user.content_languages.understood"}}</span>+ <a href={{this.loginPath}}>{{i18n+ "content_localization.preferences.login_to_set_languages"+ }}</a>+ </div>+ {{/if}}++ <form.Field+ @name="automaticallyTranslate"+ @type="checkbox"+ @title={{i18n "user.automatically_translate"}}+ @format="full"+ as |field|+ >+ <field.Control data-test-automatically-translate />+ </form.Field>+ </Form>+ </:body>++ <:footer>+ <DButton+ @label="save"+ @action={{this.submit}}+ @disabled={{this.saving}}+ class="btn-primary"+ />+ <DModalCancel @close={{@closeModal}} />+ </:footer>+ </DModal>+ </template>+}diff --git a/frontend/discourse/app/components/topic-content-language-preferences.gjs b/frontend/discourse/app/components/topic-content-language-preferences.gjsnew file mode 100644index 00000000000..4a7461d30bc--- /dev/null+++ b/frontend/discourse/app/components/topic-content-language-preferences.gjs@@ -0,0 +1,25 @@+import Component from "@glimmer/component";+import { action } from "@ember/object";+import { service } from "@ember/service";+import ContentLanguagePreferencesModal from "discourse/components/modal/content-language-preferences";+import DButton from "discourse/ui-kit/d-button";++export default class TopicContentLanguagePreferences extends Component {+ @service modal;++ @action+ openPreferences() {+ this.modal.show(ContentLanguagePreferencesModal);+ }++ <template>+ <DButton+ @icon="language"+ @title="content_localization.preferences.title"+ @action={{this.openPreferences}}+ class="btn-default topic-content-language-preferences no-text"+ data-test-content-language-preferences+ ...attributes+ />+ </template>+}diff --git a/frontend/discourse/app/components/topic-timeline/container.gjs b/frontend/discourse/app/components/topic-timeline/container.gjsindex cc690e9f9e1..bbc86d57fe2 100644--- a/frontend/discourse/app/components/topic-timeline/container.gjs+++ b/frontend/discourse/app/components/topic-timeline/container.gjs@@ -9,6 +9,7 @@ import { trustHTML } from "@ember/template";import PluginOutlet from "discourse/components/plugin-outlet";import { actionDescriptionHtml } from "discourse/components/post-action-description";import TopicAdminMenu from "discourse/components/topic-admin-menu";+import TopicContentLanguagePreferences from "discourse/components/topic-content-language-preferences";import UserTip from "discourse/components/user-tip";import lazyHash from "discourse/helpers/lazy-hash";import topicFeaturedLink from "discourse/helpers/topic-featured-link";@@ -167,7 +168,10 @@ export default class TopicTimelineScrollArea extends Component {}get showTimelineControls() {- return !this.args.fullscreen && this.currentUser;+ return (+ !this.args.fullscreen &&+ (this.currentUser || this.args.model.has_localized_content)+ );}get topicTitle() {@@ -566,6 +570,10 @@ export default class TopicTimelineScrollArea extends Component {@convertToPublicTopic={{@convertToPublicTopic}}@convertToPrivateMessage={{@convertToPrivateMessage}}/>++ {{#if @model.has_localized_content}}+ <TopicContentLanguagePreferences />+ {{/if}}</div>{{/if}}diff --git a/frontend/discourse/app/controllers/preferences/interface.js b/frontend/discourse/app/controllers/preferences/interface.jsindex f37f392dc25..a27258a7bee 100644--- a/frontend/discourse/app/controllers/preferences/interface.js+++ b/frontend/discourse/app/controllers/preferences/interface.js@@ -14,6 +14,10 @@ import {SEND_SHORTCUT_ENTER,SEND_SHORTCUT_META_ENTER,} from "discourse/lib/constants";+import {+ contentLanguageOptions,+ normalizeUnderstoodLanguages,+} from "discourse/lib/content-localization";import { deepEqual } from "discourse/lib/object";import {currentThemeId,@@ -28,7 +32,7 @@ import {} from "discourse/lib/utilities";import { AUTO_DELETE_PREFERENCES } from "discourse/models/bookmark";import { PLATFORM_KEY_MODIFIER } from "discourse/services/keyboard-shortcuts";-import { i18n } from "discourse-i18n";+import I18n, { i18n } from "discourse-i18n";// same as UserOption::HOMEPAGESconst USER_HOMES = {@@ -70,7 +74,6 @@ export default class InterfaceController extends Controller {@computed("makeThemeDefault")get saveAttrNames() {let attrs = [- "locale","external_links_in_new_tab","dynamic_favicon","enable_quoting",@@ -90,10 +93,15 @@ export default class InterfaceController extends Controller {"bookmark_auto_delete_preference","interface_color_mode","enable_markdown_monospace_font",- "show_original_content","send_shortcut",+ "automatically_translate",+ "understood_languages",];+ if (this.siteSettings.allow_user_locale) {+ attrs.push("locale");+ }+if (this.makeThemeDefault) {attrs.push("theme_ids");}@@ -103,9 +111,56 @@ export default class InterfaceController extends Controller {});}+ @computed("model.locale")+ get interfaceLanguage() {+ if (!this.siteSettings.allow_user_locale) {+ return this.siteSettings.default_locale ?? I18n.locale;+ }++ return this.model.locale ?? this.siteSettings.default_locale ?? I18n.locale;+ }++ @computed("model.locale", "model.user_option.understood_languages.[]")+ get understoodLanguages() {+ return normalizeUnderstoodLanguages(+ this.model.user_option.understood_languages,+ this.interfaceLanguage+ );+ }+@computed()get availableLocales() {- return this.siteSettings.available_locales;+ return (this.siteSettings.available_locales ?? []).map((locale) => ({+ ...locale,+ id: locale.value,+ }));+ }++ @computed(+ "model.locale",+ "model.user_option.understood_languages.[]",+ "siteSettings.available_content_localization_locales"+ )+ get understoodLanguageOptions() {+ return contentLanguageOptions(+ this.availableLocales,+ this.siteSettings.available_content_localization_locales,+ this.understoodLanguages+ );+ }++ @action+ setInterfaceLanguage(locale) {+ this.model.set("locale", locale);+ this.setUnderstoodLanguages(this.model.user_option.understood_languages);+ }++ @action+ setUnderstoodLanguages(locales) {+ this.model.set(+ "user_option.understood_languages",+ normalizeUnderstoodLanguages(locales, this.interfaceLanguage)+ );}@computed("currentThemeId")diff --git a/frontend/discourse/app/lib/content-localization.js b/frontend/discourse/app/lib/content-localization.jsnew file mode 100644index 00000000000..5538388d1e0--- /dev/null+++ b/frontend/discourse/app/lib/content-localization.js@@ -0,0 +1,31 @@+import cookie from "discourse/lib/cookie";++export const AUTOMATICALLY_TRANSLATE_COOKIE = "automatically_translate";+export const AUTOMATICALLY_TRANSLATE_COOKIE_EXPIRY = 30;++export function normalizeUnderstoodLanguages(languages, interfaceLanguage) {+ return [+ ...new Set([interfaceLanguage, ...(languages ?? [])].filter(Boolean)),+ ];+}++export function contentLanguageOptions(+ availableLocales,+ supportedLocales,+ retainedLocales = []+) {+ const allowedLocales = new Set([+ ...(supportedLocales ?? []).map((locale) => locale.value ?? locale),+ ...retainedLocales,+ ]);++ return availableLocales.filter((locale) => allowedLocales.has(locale.value));+}++export function automaticallyTranslate(currentUser) {+ if (currentUser) {+ return currentUser.user_option?.automatically_translate ?? true;+ }++ return cookie(AUTOMATICALLY_TRANSLATE_COOKIE) !== "false";+}diff --git a/frontend/discourse/app/models/user.js b/frontend/discourse/app/models/user.jsindex fd8c810945a..91df6c988a7 100644--- a/frontend/discourse/app/models/user.js+++ b/frontend/discourse/app/models/user.js@@ -146,6 +146,7 @@ let userOptionFields = ["push_notification_level","seen_popups","send_shortcut",+ "automatically_translate","show_original_content","sidebar_link_to_filtered_list","sidebar_show_count_of_new_items",@@ -155,6 +156,7 @@ let userOptionFields = ["timezone","title_count_mode","topics_unread_when_closed",+ "understood_languages","watched_precedence_over_muted",];diff --git a/frontend/discourse/app/templates/preferences/interface.gjs b/frontend/discourse/app/templates/preferences/interface.gjsindex a780c925ee7..fbae8b12792 100644--- a/frontend/discourse/app/templates/preferences/interface.gjs+++ b/frontend/discourse/app/templates/preferences/interface.gjs@@ -4,6 +4,7 @@ import PreferenceCheckbox from "discourse/components/preference-checkbox";import lazyHash from "discourse/helpers/lazy-hash";import ColorPalettePicker from "discourse/select-kit/components/color-palette-picker";import ComboBox from "discourse/select-kit/components/combo-box";+import MultiSelect from "discourse/select-kit/components/multi-select";import DButton from "discourse/ui-kit/d-button";import DSaveControls from "discourse/ui-kit/d-save-controls";import { i18n } from "discourse-i18n";@@ -143,14 +144,6 @@ export default <template><label for="locale-selector" class="control-label">{{i18n"user.locale.title"}}</label>- {{#if @controller.siteSettings.content_localization_enabled}}- <PreferenceCheckbox- @labelKey="user.show_original_content"- @checked={{@controller.model.user_option.show_original_content}}- data-setting-name="user-show-original-content"- class="pref-show-original-content"- />- {{/if}}<div class="controls"><ComboBox@id="locale-selector"@@ -158,7 +151,7 @@ export default <template>@langProperty="value"@content={{@controller.availableLocales}}@value={{@controller.model.locale}}- @onChange={{fn (mut @controller.model.locale)}}+ @onChange={{@controller.setInterfaceLanguage}}@options={{hash filterable=true none="user.locale.default"}}/></div>@@ -168,6 +161,44 @@ export default <template></div>{{/if}}+ {{#if @controller.siteSettings.content_localization_enabled}}+ <fieldset+ class="control-group pref-content-languages"+ data-setting-name="user-content-languages"+ >+ <legend class="control-label">{{i18n+ "user.content_languages.title"+ }}</legend>+ <div class="controls controls-dropdown pref-understood-languages">+ <label for="understood-languages-selector">{{i18n+ "user.content_languages.understood"+ }}</label>+ <MultiSelect+ @id="understood-languages-selector"+ @valueProperty="value"+ @langProperty="value"+ @content={{@controller.understoodLanguageOptions}}+ @value={{@controller.understoodLanguages}}+ @onChange={{@controller.setUnderstoodLanguages}}+ @mandatoryValues={{@controller.interfaceLanguage}}+ @mandatoryValueTitle={{i18n+ "content_localization.preferences.interface_language_mandatory"+ }}+ @options={{hash filterable=true}}+ />+ <div class="instructions">+ {{i18n "user.content_languages.understood_description"}}+ </div>+ </div>+ <PreferenceCheckbox+ @labelKey="user.automatically_translate"+ @checked={{@controller.model.user_option.automatically_translate}}+ data-setting-name="user-automatically-translate"+ class="pref-automatically-translate"+ />+ </fieldset>+ {{/if}}+<div class="control-group home" data-setting-name="user-home"><label for="home-selector" class="control-label">{{i18n"user.home"diff --git a/frontend/discourse/app/templates/topic.gjs b/frontend/discourse/app/templates/topic.gjsindex 33c2244b578..446f5fb50ab 100644--- a/frontend/discourse/app/templates/topic.gjs+++ b/frontend/discourse/app/templates/topic.gjs@@ -23,6 +23,7 @@ import SignupCta from "discourse/components/signup-cta";import SlowModeInfo from "discourse/components/slow-mode-info";import TopicAdminMenu from "discourse/components/topic-admin-menu";import TopicCategory from "discourse/components/topic-category";+import TopicContentLanguagePreferences from "discourse/components/topic-content-language-preferences";import TopicFooterButtons from "discourse/components/topic-footer-buttons";import TopicMap from "discourse/components/topic-map/index";import TopicMetadata from "discourse/components/topic-metadata";@@ -451,6 +452,9 @@ export default <template>@convertToPublicTopic={{@controller.convertToPublicTopic}}@convertToPrivateMessage={{@controller.convertToPrivateMessage}}/>+ {{#if @controller.model.has_localized_content}}+ <TopicContentLanguagePreferences />+ {{/if}}</TopicProgress>{{/if}}diff --git a/frontend/discourse/select-kit/components/multi-select.gjs b/frontend/discourse/select-kit/components/multi-select.gjsindex 0651ebd3f33..cf706e5feb4 100644--- a/frontend/discourse/select-kit/components/multi-select.gjs+++ b/frontend/discourse/select-kit/components/multi-select.gjs@@ -250,6 +250,7 @@ export default class MultiSelect extends SelectKitComponent {@item={{item}}@selectKit={{this.selectKit}}@mandatoryValues={{@mandatoryValues}}+ @mandatoryValueTitle={{@mandatoryValueTitle}}/>{{/each}}{{/let}}diff --git a/frontend/discourse/select-kit/components/selected-choice.gjs b/frontend/discourse/select-kit/components/selected-choice.gjsindex 3237fd7836f..8a16c290c05 100644--- a/frontend/discourse/select-kit/components/selected-choice.gjs+++ b/frontend/discourse/select-kit/components/selected-choice.gjs@@ -46,11 +46,18 @@ export default class SelectedChoice extends Component {return this.mandatoryValuesArray.includes(this.item.id);}+ @computed("mandatoryValueTitle")+ get readOnlyTitle() {+ return (+ this.mandatoryValueTitle || i18n("admin.site_settings.mandatory_group")+ );+ }+<template>{{#if this.readOnly}}<buttonclass="btn btn-default disabled tag-choice"- title={{i18n "admin.site_settings.mandatory_group"}}+ title={{this.readOnlyTitle}}>{{this.itemName}}</button>{{else}}<buttondiff --git a/frontend/discourse/tests/unit/lib/content-localization-test.js b/frontend/discourse/tests/unit/lib/content-localization-test.jsnew file mode 100644index 00000000000..f7aded585b2--- /dev/null+++ b/frontend/discourse/tests/unit/lib/content-localization-test.js@@ -0,0 +1,95 @@+import { setupTest } from "ember-qunit";+import { module, test } from "qunit";+import {+ AUTOMATICALLY_TRANSLATE_COOKIE,+ automaticallyTranslate,+ contentLanguageOptions,+ normalizeUnderstoodLanguages,+} from "discourse/lib/content-localization";+import cookie, { removeCookie } from "discourse/lib/cookie";++module("Unit | Lib | content-localization", function (hooks) {+ setupTest(hooks);++ hooks.afterEach(function () {+ removeCookie(AUTOMATICALLY_TRANSLATE_COOKIE);+ });++ test("resolves the anonymous preference from the positive cookie", function (assert) {+ assert.true(+ automaticallyTranslate(),+ "automatic translation is enabled when no cookie exists"+ );++ cookie(AUTOMATICALLY_TRANSLATE_COOKIE, true);+ assert.true(+ automaticallyTranslate(),+ "the positive preference enables automatic translation"+ );++ cookie(AUTOMATICALLY_TRANSLATE_COOKIE, false);+ assert.false(+ automaticallyTranslate(),+ "the new false preference disables automatic translation"+ );+ });++ test("always resolves a logged-in user's preference without cookies", function (assert) {+ cookie(AUTOMATICALLY_TRANSLATE_COOKIE, false);++ assert.true(+ automaticallyTranslate({+ user_option: { automatically_translate: true },+ }),+ "the positive user preference wins over both cookies"+ );+ assert.false(+ automaticallyTranslate({+ user_option: { automatically_translate: false },+ }),+ "a disabled user preference wins over both cookies"+ );+ });++ test("keeps the interface language first in understood languages", function (assert) {+ assert.deepEqual(+ normalizeUnderstoodLanguages(["en", "de", "de"], "en"),+ ["en", "de"],+ "it removes duplicate languages"+ );+ assert.deepEqual(+ normalizeUnderstoodLanguages(["en"], "ja"),+ ["ja", "en"],+ "it inserts a missing interface language"+ );+ });++ test("limits understood language choices without hiding retained values", function (assert) {+ const availableLocales = [+ { name: "English", value: "en", id: "en" },+ { name: "Deutsch", value: "de", id: "de" },+ { name: "Français", value: "fr", id: "fr" },+ { name: "日本語", value: "ja", id: "ja" },+ ];++ assert.deepEqual(+ contentLanguageOptions(+ availableLocales,+ [{ value: "en" }, { value: "ja" }],+ ["de", "fr"]+ ),+ availableLocales,+ "it includes the interface language and saved values outside the current site setting"+ );++ assert.deepEqual(+ contentLanguageOptions(+ availableLocales,+ [{ value: "en" }, { value: "ja" }],+ ["de"]+ ).map(({ value }) => value),+ ["en", "de", "ja"],+ "a removed saved value is no longer offered"+ );+ });+});diff --git a/lib/content_localization.rb b/lib/content_localization.rbindex dfa7f8071d8..8e087547ff1 100644--- a/lib/content_localization.rb+++ b/lib/content_localization.rb@@ -1,14 +1,37 @@# frozen_string_literal: trueclass ContentLocalization- SHOW_ORIGINAL_COOKIE = "content-localization-show-original"+ AUTOMATICALLY_TRANSLATE_COOKIE = "automatically_translate"# @param scope [Object] The serializer scope from which the method is called- # @return [Boolean] if the cookie is set, false otherwise+ # @return [Boolean]+ def self.automatically_translate?(scope)+ return scope.user.user_option.automatically_translate if scope&.user++ automatically_translate_anonymously?(scope&.request&.cookies)+ end+def self.show_original?(scope)- return true if scope&.user&.user_option&.show_original_content- return false if scope&.user- scope&.request&.cookies&.key?(SHOW_ORIGINAL_COOKIE)+ !automatically_translate?(scope)+ end++ # @param cookies [ActionDispatch::Cookies::CookieJar, Hash]+ # @return [Boolean]+ def self.automatically_translate_anonymously?(cookies)+ return true if cookies.blank?++ cookies[AUTOMATICALLY_TRANSLATE_COOKIE].to_s != "false"+ end++ # @param locale [String] The source locale of a topic or post+ # @param scope [Object] The serializer scope from which the method is called+ # @return [Boolean]+ def self.understands?(locale, scope)+ return false if locale.blank? || !scope&.user++ scope.user.user_option.understood_languages.any? do |understood_locale|+ LocaleNormalizer.is_same?(locale, understood_locale)+ endend# This method returns true when we should try to show the translated post.@@ -17,7 +40,7 @@ class ContentLocalization# @return [Boolean]def self.show_translated_post?(post, scope)SiteSetting.content_localization_enabled && post.raw.present? && post.locale.present? &&- !post.in_user_locale? && !show_original?(scope)+ !post.in_user_locale? && automatically_translate?(scope) && !understands?(post.locale, scope)end# This method returns true when we should try to show the translated topic.@@ -26,7 +49,7 @@ class ContentLocalization# @return [Boolean]def self.show_translated_topic?(topic, scope)SiteSetting.content_localization_enabled && topic.locale.present? && !topic.in_user_locale? &&- !show_original?(scope)+ automatically_translate?(scope) && !understands?(topic.locale, scope)end# This method returns true when we should try to show the translated category.diff --git a/lib/content_localization/onebox_localizer.rb b/lib/content_localization/onebox_localizer.rbindex be25a929dfe..051f41d92e7 100644--- a/lib/content_localization/onebox_localizer.rb+++ b/lib/content_localization/onebox_localizer.rb@@ -13,11 +13,10 @@ class ContentLocalization# title and excerpt are each present only when that part has a translation, so# a translated title can sit above an untranslated preview.#- # NOTE: this deliberately does not honour the "show original" preference — the- # guardian passed in (from TopicView) has no request, so it cannot read the- # anonymous show-original cookie. PostSerializer#include_localized_oneboxes? is- # the authoritative gate for that (it has the request-aware scope). Any new- # caller must apply the same gate.+ # NOTE: the guardian passed in (from TopicView) has no request, so it cannot+ # read the anonymous automatic-translation cookie.+ # PostSerializer#include_localized_oneboxes? is the request-aware gate. Any+ # new caller must apply the same gate.class OneboxLocalizerdef self.build(...)new(...).builddiff --git a/lib/middleware/anonymous_cache.rb b/lib/middleware/anonymous_cache.rbindex 536d8744bd0..d2f442495ff 100644--- a/lib/middleware/anonymous_cache.rb+++ b/lib/middleware/anonymous_cache.rb@@ -20,7 +20,7 @@ module Middlewaret: "key_cache_theme_ids",ca: "key_compress_anon",l: "key_locale",- lso: "key_show_original_content",+ lat: "key_automatically_translate",cm: "key_forced_color_mode",cs: "key_color_scheme_id",ds: "key_dark_scheme_id",@@ -194,8 +194,8 @@ module MiddlewareGlobalSetting.compress_anon_cacheend- def key_show_original_content- @request.cookies.key?(ContentLocalization::SHOW_ORIGINAL_COOKIE)+ def key_automatically_translate+ ContentLocalization.automatically_translate_anonymously?(@request.cookies)enddef theme_idsdiff --git a/lib/topic_list_responder.rb b/lib/topic_list_responder.rbindex 4331bb72086..eeb8a393bf2 100644--- a/lib/topic_list_responder.rb+++ b/lib/topic_list_responder.rb@@ -23,12 +23,12 @@ module TopicListResponderdef localize_topic_list_content(list)return if list.topics.blank? || !SiteSetting.content_localization_enabled- return if cookies.key?(ContentLocalization::SHOW_ORIGINAL_COOKIE)- return if current_user&.user_option&.show_original_contentcrawl_locale = I18n.localelist.topics.each do |topic|- LocalizationAttributesReplacer.replace_topic_attributes(topic, crawl_locale)+ if ContentLocalization.show_translated_topic?(topic, guardian)+ LocalizationAttributesReplacer.replace_topic_attributes(topic, crawl_locale)+ endendendenddiff --git a/lib/topic_view.rb b/lib/topic_view.rbindex 13893fabe65..9de343fe715 100644--- a/lib/topic_view.rb+++ b/lib/topic_view.rb@@ -191,6 +191,42 @@ class TopicView@personal_message = @topic.private_message?end+ def has_localized_content?+ return @has_localized_content if defined?(@has_localized_content)++ @has_localized_content =+ if !SiteSetting.content_localization_enabled+ false+ elsif !topic.in_user_locale? && topic.has_localization?+ true+ elsif skip_post_loading+ false+ else+ source_language = I18n.locale.to_s.tr("-", "_").split("_").first.downcase+ localization_languages = [source_language]+ if SiteSetting.content_localization_use_default_locale_when_unsupported+ localization_languages << SiteSetting.default_locale.to_s+ end+ localization_locale = PostLocalization.arel_table[:locale]+ localization_condition =+ localization_languages+ .map { |locale| locale.tr("-", "_").split("_").first.downcase }+ .uniq+ .map { |language| localization_locale.matches("#{language}%") }+ .reduce(&:or)++ filtered_posts+ .joins(:localizations)+ .where(localization_condition)+ .where(+ "posts.raw <> '' AND posts.locale IS NOT NULL AND posts.locale <> '' AND " \+ "SPLIT_PART(REPLACE(LOWER(posts.locale), '-', '_'), '_', 1) <> ?",+ source_language,+ )+ .exists?+ end+ end+def user_badges(badge_names)return if !badge_names.present?@@ -780,7 +816,7 @@ class TopicView# Per-post localized title/preview for internal topic oneboxes the reader sees# in their own language. See ContentLocalization::OneboxLocalizer for the full- # contract (including why "show original" is gated in the serializer, not here).+ # contract (including the request-aware preference gate in the serializer).def localized_oneboxesreturn @localized_oneboxes if defined?(@localized_oneboxes)@@ -1021,7 +1057,18 @@ class TopicViewenddef find_topic(topic_or_topic_id)- return topic_or_topic_id if topic_or_topic_id.is_a?(Topic)+ if topic_or_topic_id.is_a?(Topic)+ if SiteSetting.content_localization_enabled &&+ !topic_or_topic_id.association(:localizations).loaded?+ ActiveRecord::Associations::Preloader.new(+ records: [topic_or_topic_id],+ associations: :localizations,+ ).call+ end++ return topic_or_topic_id+ end+# with_deleted covered in #check_and_raise_exceptionstags_include =if SiteSetting.tagging_enabled && SiteSetting.content_localization_enabled@@ -1030,9 +1077,10 @@ class TopicView:tagsendnested_topic_include = :nested_topic if SiteSetting.nested_replies_enabled+ localizations_include = :localizations if SiteSetting.content_localization_enabledTopic.with_deleted- .includes(:category, nested_topic_include, tags_include)+ .includes(:category, nested_topic_include, tags_include, localizations_include).find_by(id: topic_or_topic_id)enddiff --git a/plugins/discourse-ai/assets/javascripts/discourse/initializers/translation.js b/plugins/discourse-ai/assets/javascripts/discourse/initializers/translation.jsindex 6aa0d9c7cb4..afa0ff9a6d3 100644--- a/plugins/discourse-ai/assets/javascripts/discourse/initializers/translation.js+++ b/plugins/discourse-ai/assets/javascripts/discourse/initializers/translation.js@@ -1,5 +1,5 @@import { apiInitializer } from "discourse/lib/api";-import cookie from "discourse/lib/cookie";+import { automaticallyTranslate } from "discourse/lib/content-localization";export default apiInitializer((api) => {const settings = api.container.lookup("service:site-settings");@@ -15,11 +15,8 @@ export default apiInitializer((api) => {"localized",(topicController, data) => {const currentUser = api.getCurrentUser();- const showOriginal = currentUser- ? currentUser.user_option?.show_original_content- : cookie("content-localization-show-original");- if (!showOriginal) {+ if (automaticallyTranslate(currentUser)) {const postStream = topicController.get("model.postStream");postStream.triggerChangedPost(data.id, data.updated_at);}diff --git a/plugins/discourse-ai/lib/summarization.rb b/plugins/discourse-ai/lib/summarization.rbindex c036e437733..7d42caedcb3 100644--- a/plugins/discourse-ai/lib/summarization.rb+++ b/plugins/discourse-ai/lib/summarization.rb@@ -81,11 +81,9 @@ module DiscourseAidef display_locale(topic, scope:)source_locale = raw_source_locale(topic)- should_show_localized_content =- SiteSetting.content_localization_enabled && !ContentLocalization.show_original?(scope)locale =- if should_show_localized_content+ if ContentLocalization.show_translated_topic?(topic, scope)SiteSetting.content_localization_locales.find do |supported_locale|LocaleNormalizer.is_same?(supported_locale, I18n.locale)end || source_localediff --git a/plugins/discourse-ai/spec/lib/modules/summarization/entry_point_spec.rb b/plugins/discourse-ai/spec/lib/modules/summarization/entry_point_spec.rbindex b4bd40533cb..7f6be186597 100644--- a/plugins/discourse-ai/spec/lib/modules/summarization/entry_point_spec.rb+++ b/plugins/discourse-ai/spec/lib/modules/summarization/entry_point_spec.rb@@ -82,7 +82,7 @@ RSpec.describe DiscourseAi::Summarization::EntryPoint doexpect(serialized[:ai_topic_gist]).to be_presentend- it "selects the localized gist and respects the show-original preference" do+ it "selects the localized gist and respects the automatic-translation preference" dotopic_ai_gist.target.update!(locale: "en")english_gist =topic_ai_gist.tap { |gist| gist.update!(summarized_text: "English gist") }@@ -109,7 +109,7 @@ RSpec.describe DiscourseAi::Summarization::EntryPoint doexpect(serialized[:ai_topic_gist]).to eq(japanese_gist.summarized_text)- user.user_option.update!(show_original_content: true)+ user.user_option.update!(automatically_translate: false)original_serialized =TopicListItemSerializer.new(gist_topic,diff --git a/plugins/discourse-ai/spec/lib/modules/summarization/summarization_spec.rb b/plugins/discourse-ai/spec/lib/modules/summarization/summarization_spec.rbindex 9aede2f571c..676003f1790 100644--- a/plugins/discourse-ai/spec/lib/modules/summarization/summarization_spec.rb+++ b/plugins/discourse-ai/spec/lib/modules/summarization/summarization_spec.rb@@ -96,7 +96,7 @@ RSpec.describe DiscourseAi::Summarization doexpect(described_class.gist_for(topic, scope: user.guardian)).to eq(german_gist)end- it "returns the source-locale gist when the user requests original content" do+ it "returns the source-locale gist when the user disables automatic translation" dotopic.update!(locale: "fr")source_gist =Fabricate(:topic_ai_gist, target: topic, locale: "fr", summarized_text: "Résumé français")@@ -108,7 +108,7 @@ RSpec.describe DiscourseAi::Summarization do)SiteSetting.content_localization_enabled = trueI18n.locale = :de- user.user_option.update!(show_original_content: true)+ user.user_option.update!(automatically_translate: false)expect(described_class.gist_for(topic, scope: user.guardian)).to eq(source_gist)enddiff --git a/plugins/discourse-ai/spec/services/discourse_ai/topic_summarization_spec.rb b/plugins/discourse-ai/spec/services/discourse_ai/topic_summarization_spec.rbindex 7212af8270b..2b25551d639 100644--- a/plugins/discourse-ai/spec/services/discourse_ai/topic_summarization_spec.rb+++ b/plugins/discourse-ai/spec/services/discourse_ai/topic_summarization_spec.rb@@ -30,7 +30,7 @@ describe DiscourseAi::TopicSummarization dolet(:summary) { "This is the final summary" }describe ".for" do- it "selects the displayed locale and respects the show-original preference" do+ it "selects the displayed locale and respects the automatic-translation preference" doSiteSetting.content_localization_enabled = trueSiteSetting.content_localization_supported_locales = "he"topic.update!(locale: "en")@@ -43,7 +43,7 @@ describe DiscourseAi::TopicSummarization doI18n.with_locale(:he) { described_class.for(topic, user, scope: user.guardian) }expect(localized_service.cached_summary).to eq(hebrew_summary)- user.user_option.update!(show_original_content: true)+ user.user_option.update!(automatically_translate: false)original_service =I18n.with_locale(:he) { described_class.for(topic, user, scope: user.guardian) }expect(original_service.cached_summary).to eq(english_summary)diff --git a/plugins/discourse-ai/spec/system/edit_post_image_captions_spec.rb b/plugins/discourse-ai/spec/system/edit_post_image_captions_spec.rbindex e532d86845d..a5125be6495 100644--- a/plugins/discourse-ai/spec/system/edit_post_image_captions_spec.rb+++ b/plugins/discourse-ai/spec/system/edit_post_image_captions_spec.rb@@ -126,7 +126,7 @@ describe "Edit AI post image captions" doSiteSetting.content_localization_allowed_groups = Group::AUTO_GROUPS[:admins]SiteSetting.content_localization_supported_locales = "en|ja"admin.update!(locale: "ja")- admin.user_option.update!(show_original_content: false)+ admin.user_option.update!(automatically_translate: true)Fabricate(:topic_localization, topic: cat_topic, locale: "ja", fancy_title: "猫についての話題")create_japanese_localization(second_post)seed_english_descriptions@@ -143,7 +143,7 @@ describe "Edit AI post image captions" dodescription: japanese_second_cat_caption,)- admin.user_option.update!(show_original_content: true)+ admin.user_option.update!(automatically_translate: false)topic_page.visit_topic(cat_topic)expect(captions).to have_image_caption(second_post, image: 2, description: second_cat_caption)@@ -155,7 +155,7 @@ describe "Edit AI post image captions" doSiteSetting.content_localization_allowed_groups = Group::AUTO_GROUPS[:admins]SiteSetting.content_localization_supported_locales = "en|ja"admin.update!(locale: "ja")- admin.user_option.update!(show_original_content: false)+ admin.user_option.update!(automatically_translate: true)Fabricate(:topic_localization, topic: cat_topic, locale: "ja", fancy_title: "猫についての話題")create_japanese_localization(second_post)seed_english_descriptions@@ -191,7 +191,7 @@ describe "Edit AI post image captions" doexpect(captions).to have_image_caption(second_post, image: 2, description: "翻訳された二匹目の猫だけの新しい説明")- admin.user_option.update!(show_original_content: true)+ admin.user_option.update!(automatically_translate: false)topic_page.visit_topic(cat_topic)expect(captions).to have_image_caption(second_post, image: 2, description: second_cat_caption)diff --git a/spec/lib/content_localization_spec.rb b/spec/lib/content_localization_spec.rbindex 78585be8450..3e1c860558b 100644--- a/spec/lib/content_localization_spec.rb+++ b/spec/lib/content_localization_spec.rb@@ -9,45 +9,45 @@ describe ContentLocalization doendend- describe ".show_original?" do- it "returns true when cookie is present" do- scope = create_scope(cookie: ContentLocalization::SHOW_ORIGINAL_COOKIE)-- expect(ContentLocalization.show_original?(scope)).to be true- end-- it "returns false when cookie is absent" do- scope = create_scope-- expect(ContentLocalization.show_original?(scope)).to be false- end-- it "returns true when user preference is set" do+ describe ".automatically_translate?" do+ it "defaults to true for anonymous users and supports the positive cookie" do+ expect(ContentLocalization.automatically_translate?(create_scope)).to be true+ expect(+ ContentLocalization.automatically_translate?(+ create_scope(cookie: "#{ContentLocalization::AUTOMATICALLY_TRANSLATE_COOKIE}=true"),+ ),+ ).to be true+ expect(+ ContentLocalization.automatically_translate?(+ create_scope(cookie: "#{ContentLocalization::AUTOMATICALLY_TRANSLATE_COOKIE}=false"),+ ),+ ).to be false+ end++ it "always uses the logged-in user's preference instead of cookies" douser = Fabricate(:user)- user.user_option.update!(show_original_content: true)- scope = create_scope(user: user)+ user.user_option.update!(automatically_translate: false)- expect(ContentLocalization.show_original?(scope)).to be true- end+ expect(ContentLocalization.automatically_translate?(create_scope(user: user))).to be false- it "returns false when user preference is not set and no cookie" do- user = Fabricate(:user)- scope = create_scope(user: user)+ user.user_option.update!(automatically_translate: true)+ scope =+ create_scope(user:, cookie: "#{ContentLocalization::AUTOMATICALLY_TRANSLATE_COOKIE}=false")- expect(ContentLocalization.show_original?(scope)).to be false+ expect(ContentLocalization.automatically_translate?(scope)).to be trueend+ end- it "returns false when user preference is not set but cookie is present for a logged-in user" do+ describe ".understands?" do+ it "matches the logged-in user's languages using normalized locales" douser = Fabricate(:user)- scope = create_scope(user: user, cookie: ContentLocalization::SHOW_ORIGINAL_COOKIE)+ user.user_option.update!(understood_languages: %w[en_GB ja])+ scope = create_scope(user:)- expect(ContentLocalization.show_original?(scope)).to be false- end-- it "returns true when cookie is present for an anonymous user" do- scope = create_scope(cookie: ContentLocalization::SHOW_ORIGINAL_COOKIE)-- expect(ContentLocalization.show_original?(scope)).to be true+ expect(ContentLocalization.understands?("en", scope)).to be true+ expect(ContentLocalization.understands?("ja-JP", scope)).to be true+ expect(ContentLocalization.understands?("de", scope)).to be false+ expect(ContentLocalization.understands?("en", create_scope)).to be falseendend@@ -98,8 +98,16 @@ describe ContentLocalization doexpect(ContentLocalization.show_translated_post?(post, scope)).to be falseend- it "returns false when show_original? is true" do- scope = create_scope(cookie: ContentLocalization::SHOW_ORIGINAL_COOKIE)+ it "returns false when automatic translation is disabled" do+ scope = create_scope(cookie: "#{ContentLocalization::AUTOMATICALLY_TRANSLATE_COOKIE}=false")++ expect(ContentLocalization.show_translated_post?(post, scope)).to be false+ end++ it "returns false when the user understands the post's language" do+ user = Fabricate(:user)+ user.user_option.update!(understood_languages: ["ja"])+ scope = create_scope(user:)expect(ContentLocalization.show_translated_post?(post, scope)).to be falseend@@ -146,8 +154,16 @@ describe ContentLocalization doexpect(ContentLocalization.show_translated_topic?(topic, scope)).to be falseend- it "returns false when show_original? is true" do- scope = create_scope(cookie: ContentLocalization::SHOW_ORIGINAL_COOKIE)+ it "returns false when automatic translation is disabled" do+ scope = create_scope(cookie: "#{ContentLocalization::AUTOMATICALLY_TRANSLATE_COOKIE}=false")++ expect(ContentLocalization.show_translated_topic?(topic, scope)).to be false+ end++ it "returns false when the user understands the topic's language" do+ user = Fabricate(:user)+ user.user_option.update!(understood_languages: ["ja"])+ scope = create_scope(user:)expect(ContentLocalization.show_translated_topic?(topic, scope)).to be falseenddiff --git a/spec/lib/middleware/anonymous_cache_spec.rb b/spec/lib/middleware/anonymous_cache_spec.rbindex 71cc3e7fef4..83da8517d77 100644--- a/spec/lib/middleware/anonymous_cache_spec.rb+++ b/spec/lib/middleware/anonymous_cache_spec.rb@@ -145,12 +145,19 @@ RSpec.describe Middleware::AnonymousCache do}.not_to raise_errorend- it "handles showing original content" do- show_orig_key =- new_helper("HTTP_COOKIE" => ContentLocalization::SHOW_ORIGINAL_COOKIE).cache_key+ it "keys only on the resolved automatic translation preference" doregular_key = new_helper.cache_key+ enabled_key =+ new_helper(+ "HTTP_COOKIE" => "#{ContentLocalization::AUTOMATICALLY_TRANSLATE_COOKIE}=true",+ ).cache_key+ disabled_key =+ new_helper(+ "HTTP_COOKIE" => "#{ContentLocalization::AUTOMATICALLY_TRANSLATE_COOKIE}=false",+ ).cache_key- expect(show_orig_key).not_to eq(regular_key)+ expect(enabled_key).to eq(regular_key)+ expect(disabled_key).not_to eq(regular_key)endcontext "when cached" dodiff --git a/spec/lib/topic_view_spec.rb b/spec/lib/topic_view_spec.rbindex dece3f6c70a..8fa49f165fc 100644--- a/spec/lib/topic_view_spec.rb+++ b/spec/lib/topic_view_spec.rb@@ -28,6 +28,30 @@ RSpec.describe TopicView doendend+ describe "#has_localized_content?" do+ before { SiteSetting.content_localization_enabled = true }++ it "ignores localizations on posts that are not eligible for translation" do+ localized_topic = Fabricate(:topic, locale: "en")+ post = Fabricate(:post, topic: localized_topic, locale: nil)+ Fabricate(:post_localization, post:, locale: "en")++ expect(TopicView.new(localized_topic.id, user).has_localized_content?).to eq(false)+ end++ it "detects a site-default post localization when default fallback is enabled" do+ SiteSetting.default_locale = "en"+ SiteSetting.content_localization_use_default_locale_when_unsupported = true+ localized_topic = Fabricate(:topic, locale: "de")+ post = Fabricate(:post, topic: localized_topic, locale: "ja")+ Fabricate(:post_localization, post:, locale: "en")++ I18n.with_locale(:de) do+ expect(TopicView.new(localized_topic.id, user).has_localized_content?).to eq(true)+ end+ end+ end+describe "#reset_post_collection" dofab!(:post1) { Fabricate(:post, topic: topic) }fab!(:post2) { Fabricate(:post, topic: topic) }diff --git a/spec/models/tag_spec.rb b/spec/models/tag_spec.rbindex cb8c6f89f8b..aff240d080c 100644--- a/spec/models/tag_spec.rb+++ b/spec/models/tag_spec.rb@@ -227,11 +227,13 @@ RSpec.describe Tag do)end- it "returns localized names when localization enabled and user not in tag locale" do+ it "returns localized tag names independently of topic and post translation preferences" doSiteSetting.content_localization_enabled = trueI18n.locale = "ja"+ user = Fabricate(:user, locale: "ja")+ user.user_option.update!(automatically_translate: false)- expect(Tag.top_tags).to include(+ expect(Tag.top_tags(guardian: Guardian.new(user))).to include({ id: localized_tag.id, name: "猫", slug: localized_tag.slug },)enddiff --git a/spec/models/user_option_spec.rb b/spec/models/user_option_spec.rbindex b6f8c84138b..ee2951e2a58 100644--- a/spec/models/user_option_spec.rb+++ b/spec/models/user_option_spec.rb@@ -32,6 +32,11 @@ RSpec.describe UserOption doexpect(user.user_option.hide_presence).to eq(false)end+ it "enables automatic translation without assuming understood languages" do+ expect(user.user_option.automatically_translate).to eq(true)+ expect(user.user_option.understood_languages).to eq([])+ end+it "should correctly set digest frequency" doSiteSetting.default_email_digest_frequency = 1440user = Fabricate(:user)@@ -77,6 +82,27 @@ RSpec.describe UserOption doendend+ describe "understood languages" do+ fab!(:user)++ it "accepts supported locales" do+ expect(user.user_option.update(understood_languages: %w[en ja])).to eq(true)+ end++ it "rejects unsupported locales" do+ expect(user.user_option.update(understood_languages: ["not-a-locale"])).to eq(false)+ expect(user.user_option.errors[:understood_languages]).to be_present+ end++ it "allows unrelated preferences to be saved after a language provider is removed" do+ user.user_option.update!(understood_languages: ["en"])+ LocaleSiteSetting.stubs(:supported_locales).returns(["ja"])++ expect(user.user_option.update(automatically_translate: false)).to eq(true)+ expect(user.user_option.reload.automatically_translate).to eq(false)+ end+ end+describe "site settings" doit "should apply defaults from site settings" doSiteSetting.default_other_enable_quoting = falsediff --git a/spec/requests/about_controller_spec.rb b/spec/requests/about_controller_spec.rbindex 648422d6128..54e75da0365 100644--- a/spec/requests/about_controller_spec.rb+++ b/spec/requests/about_controller_spec.rb@@ -100,16 +100,16 @@ RSpec.describe AboutController doexpect(response.body).to include(%(meta name="description" content="日本語の説明"))end- it "uses original values when the user prefers original content" do+ it "localizes site settings independently of topic and post translation preferences" douser = Fabricate(:user, locale: "ja")- user.user_option.update!(show_original_content: true)+ user.user_option.update!(automatically_translate: false)sign_in(user)get "/about.json", params: { Discourse::LOCALE_PARAM => "ja" }expect(response.status).to eq(200)- expect(response.parsed_body.dig("about", "title")).to eq("English title")- expect(response.parsed_body.dig("about", "description")).to eq("English description")+ expect(response.parsed_body.dig("about", "title")).to eq("日本語タイトル")+ expect(response.parsed_body.dig("about", "description")).to eq("日本語の説明")endenddiff --git a/spec/requests/api/schemas/json/user_get_response.json b/spec/requests/api/schemas/json/user_get_response.jsonindex 198b692b0a4..a2448846d71 100644--- a/spec/requests/api/schemas/json/user_get_response.json+++ b/spec/requests/api/schemas/json/user_get_response.json@@ -853,6 +853,15 @@"interface_color_mode": {"type": "integer"},+ "automatically_translate": {+ "type": "boolean"+ },+ "understood_languages": {+ "type": "array",+ "items": {+ "type": "string"+ }+ },"show_original_content": {"type": "boolean"},@@ -903,7 +912,9 @@"topics_unread_when_closed","interface_color_mode","show_original_content",- "send_shortcut"+ "send_shortcut",+ "automatically_translate",+ "understood_languages"]}},diff --git a/spec/requests/search_controller_spec.rb b/spec/requests/search_controller_spec.rbindex aaae6fd09d0..bd2b4599e32 100644--- a/spec/requests/search_controller_spec.rb+++ b/spec/requests/search_controller_spec.rb@@ -637,8 +637,8 @@ RSpec.describe SearchController doendend- context "when show_original cookie is set" do- before { cookies[ContentLocalization::SHOW_ORIGINAL_COOKIE] = "true" }+ context "when automatic translation is disabled by cookie" do+ before { cookies[ContentLocalization::AUTOMATICALLY_TRANSLATE_COOKIE] = "false" }it "returns original blurb in search results" dowith_search_indexer_enabled { SearchIndexer.index(localized_post.topic, force: true) }diff --git a/spec/requests/topics_controller_spec.rb b/spec/requests/topics_controller_spec.rbindex 33d58496ccb..d3ff7ec869d 100644--- a/spec/requests/topics_controller_spec.rb+++ b/spec/requests/topics_controller_spec.rb@@ -3527,8 +3527,8 @@ RSpec.describe TopicsController doexpect(entry["excerpt"]).to include("戦わずして勝つ")end- it "omits localized onebox data when the reader chose to see original content" do- reader.user_option.update!(show_original_content: true)+ it "omits localized onebox data when the reader disables automatic translation" do+ reader.user_option.update!(automatically_translate: false)sign_in(reader)expect(host_post_json.key?("localized_oneboxes")).to eq(false)@@ -4849,8 +4849,8 @@ RSpec.describe TopicsController doendend- context "when show_original cookie is set" do- before { cookies[ContentLocalization::SHOW_ORIGINAL_COOKIE] = "true" }+ context "when automatic translation is disabled by cookie" do+ before { cookies[ContentLocalization::AUTOMATICALLY_TRANSLATE_COOKIE] = "false" }it "returns original posts" doget "/t/#{localized_topic.id}/posts.json"diff --git a/spec/requests/users_controller_spec.rb b/spec/requests/users_controller_spec.rbindex 4034b3bb5d6..cb183127502 100644--- a/spec/requests/users_controller_spec.rb+++ b/spec/requests/users_controller_spec.rb@@ -2701,6 +2701,8 @@ RSpec.describe UsersController dowatched_tags: "#{tags[0].name},#{tag_synonym.name}",card_background_upload_url: upload.url,profile_background_upload_url: upload.url,+ show_original_content: true,+ understood_languages: ["ja"],}expect(response.status).to eq(200)@@ -2717,6 +2719,8 @@ RSpec.describe UsersController donotification_level: TagUser.notification_levels[:watching],).pluck(:tag_id),).to contain_exactly(tags[0].id, tags[1].id)+ expect(user.user_option.automatically_translate).to eq(false)+ expect(user.user_option.understood_languages).to contain_exactly("en", "ja")theme = Fabricate(:theme, user_selectable: true)@@ -2725,6 +2729,7 @@ RSpec.describe UsersController domuted_usernames: "",theme_ids: [theme.id],email_level: UserOption.email_level_types[:always],+ automatically_translate: true,}user.reload@@ -2732,6 +2737,7 @@ RSpec.describe UsersController doexpect(user.muted_users.pluck(:username).sort).to be_emptyexpect(user.user_option.theme_ids).to eq([theme.id])expect(user.user_option.email_level).to eq(UserOption.email_level_types[:always])+ expect(user.user_option.automatically_translate).to eq(true)expect(user.profile_background_upload).to eq(upload)expect(user.card_background_upload).to eq(upload)enddiff --git a/spec/serializers/current_user_serializer_spec.rb b/spec/serializers/current_user_serializer_spec.rbindex e7ded57ad1c..8f79ee24417 100644--- a/spec/serializers/current_user_serializer_spec.rb+++ b/spec/serializers/current_user_serializer_spec.rb@@ -285,6 +285,18 @@ RSpec.describe CurrentUserSerializer doendend+ describe "content localization preferences" do+ it "computes the legacy JSON preference from automatically_translate" do+ user.user_option.automatically_translate = false+ user.user_option.show_original_content = false++ expect(serializer.as_json[:user_option]).to include(+ automatically_translate: false,+ show_original_content: true,+ )+ end+ end+describe "#associated_account_ids" dobefore doUserAssociatedAccount.create(diff --git a/spec/serializers/post_serializer_spec.rb b/spec/serializers/post_serializer_spec.rbindex 6dd3a157001..73e0fecfaea 100644--- a/spec/serializers/post_serializer_spec.rb+++ b/spec/serializers/post_serializer_spec.rb@@ -1080,19 +1080,22 @@ RSpec.describe PostSerializer doexpect(entry[:excerpt]).to include("戦わずして勝つ")end- it "is omitted when the reader chose to see original content" do- reader.user_option.update!(show_original_content: true)+ it "is omitted when the reader disables automatic translation" do+ reader.user_option.update!(automatically_translate: false)expect(json_for(reader).key?(:localized_oneboxes)).to eq(false)end- it "is omitted for an anonymous reader with the show-original cookie" do- env = create_request_env.merge("HTTP_COOKIE" => ContentLocalization::SHOW_ORIGINAL_COOKIE)+ it "is omitted for an anonymous reader with automatic translation disabled by cookie" do+ env =+ create_request_env.merge(+ "HTTP_COOKIE" => "#{ContentLocalization::AUTOMATICALLY_TRANSLATE_COOKIE}=false",+ )anon_scope = Guardian.new(nil, ActionDispatch::Request.new(env))expect(json_for(nil, scope: anon_scope).key?(:localized_oneboxes)).to eq(false)end- it "is included for an anonymous reader without the show-original cookie" do+ it "is included for an anonymous reader without an automatic translation cookie" doanon_scope = Guardian.new(nil, ActionDispatch::Request.new(create_request_env))expect(json_for(nil, scope: anon_scope)[:localized_oneboxes].first[:title]).to eq("孫子の兵法")diff --git a/spec/serializers/user_serializer_spec.rb b/spec/serializers/user_serializer_spec.rbindex 0ff587c61b5..cd009ecc68c 100644--- a/spec/serializers/user_serializer_spec.rb+++ b/spec/serializers/user_serializer_spec.rb@@ -61,6 +61,18 @@ RSpec.describe UserSerializer doexpect(json[:group_users]).to eq([])expect(json[:second_factor_enabled]).to eq(false)end++ it "computes the legacy localization preference from automatically_translate" do+ user.user_option.automatically_translate = false+ user.user_option.show_original_content = false++ json = UserSerializer.new(user, scope: Guardian.new(user), root: false).as_json++ expect(json[:user_option]).to include(+ automatically_translate: false,+ show_original_content: true,+ )+ endendcontext "with a user" dodiff --git a/spec/services/user_updater_spec.rb b/spec/services/user_updater_spec.rbindex 87e701668ce..b6a3b4d554b 100644--- a/spec/services/user_updater_spec.rb+++ b/spec/services/user_updater_spec.rb@@ -46,6 +46,34 @@ RSpec.describe UserUpdater doexpect(user.reload.name).to eq "Jim Tom"end+ it "keeps the interface locale among the user's understood languages" do+ user.update!(locale: "en")+ user.user_option.update!(understood_languages: ["en"])++ UserUpdater.new(user, user).update(locale: "ja", understood_languages: [])++ expect(user.reload.locale).to eq("ja")+ expect(user.user_option.understood_languages).to eq(["ja"])+ end++ it "uses the effective interface locale when user locales are disabled" do+ SiteSetting.default_locale = "en"+ SiteSetting.allow_user_locale = false+ user.update!(locale: "fr")++ UserUpdater.new(user, user).update(understood_languages: [])++ expect(user.user_option.understood_languages).to eq(["en"])+ end++ it "adapts legacy show-original updates to the positive preference" do+ UserUpdater.new(user, user).update(show_original_content: true)+ expect(user.reload.user_option.automatically_translate).to eq(false)++ UserUpdater.new(user, user).update(show_original_content: true, automatically_translate: true)+ expect(user.reload.user_option.automatically_translate).to eq(true)+ end+describe "the within_user_updater_transaction event" doit "allows plugins to perform additional updates" doupdate_attributes = { name: "Jimmmy Johnny" }diff --git a/spec/system/content_localization_spec.rb b/spec/system/content_localization_spec.rbindex 07c763f0cb9..ed4e49e8ccf 100644--- a/spec/system/content_localization_spec.rb+++ b/spec/system/content_localization_spec.rb@@ -42,6 +42,9 @@ describe "Content Localization" dolet(: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) }+ let(:preferences_interface_page) { PageObjects::Pages::UserPreferencesInterface.new }+ let(:sidebar) { PageObjects::Components::NavigationMenu::Sidebar.new }+ let(:language_switcher) { PageObjects::Components::LanguageSwitcher.new }def scroll_to_post(post_number)5.times do@@ -66,7 +69,7 @@ describe "Content Localization" doSiteSetting.content_localization_allowed_groups ="#{Group::AUTO_GROUPS[:admins]}|#{jap_group.id}"SiteSetting.content_localization_supported_locales = "en|ja"- japanese_user.user_option.update!(show_original_content: false)+ japanese_user.user_option.update!(automatically_translate: true)endit "shows the user's language based on their user locale" do@@ -76,24 +79,70 @@ describe "Content Localization" doexpect(topic_page.has_topic_title?("孫子兵法からの人生戦略")).to eq(true)end- it "persists 'Show Original' preference from user preferences interface" do+ it "lets the user keep topic and post content original without changing localized navigation" do+ tag = Fabricate(:tag, name: "strategy", locale: "en")+ Fabricate(:tag_localization, tag:, locale: "ja", name: "戦略")+ Fabricate(:topic_tag, topic:, tag:)+ SiteSetting.tagging_enabled = true+ SiteSetting.navigation_menu = "sidebar"+ SiteSetting.default_navigation_menu_tags = tag.name+ SiteSetting.set_locale_from_cookie = true+ SiteSetting.content_localization_language_switcher = "all"+sign_in(japanese_user)- visit("/u/#{japanese_user.username}/preferences/interface")- page.find(".pref-show-original-content input[type='checkbox']").click- page.find(".save-changes").click+ preferences_interface_page.visit(japanese_user)+ expect(preferences_interface_page).to have_interface_language_section+ expect(preferences_interface_page).to have_content_languages_section+ expect(preferences_interface_page).to have_locked_understood_language+ expect(preferences_interface_page).to be_automatic_translation_enabled- I18n.with_locale(:ja) { expect(page).to have_content(I18n.t("js.saved")) }+ preferences_interface_page.disable_automatic_translation.save_changes+ page.refresh+ expect(preferences_interface_page).to be_automatic_translation_disabled- expect(japanese_user.reload.user_option.show_original_content).to eq(true)+ topic_page.visit_topic(topic)+ expect(topic_page).to have_topic_title("Life strategies from The Art of War")+ expect(post_1_obj).to have_cooked_content(post_1.raw)+ expect(post_3_obj).to have_cooked_content(post_3.raw)+ expect(topic_page).to have_topic_tag("戦略")+ expect(sidebar).to have_section_link("戦略")++ language_switcher.select_language("en")+ preferences_interface_page.visit(japanese_user)+ expect(preferences_interface_page).to be_automatic_translation_disabled+ language_switcher.select_language("ja")++ preferences_interface_page.visit(japanese_user)+ preferences_interface_page.enable_automatic_translation.save_changes+ page.refresh+ expect(preferences_interface_page).to be_automatic_translation_enabled+ expect(preferences_interface_page).to have_understood_language("en")- # preference persists after page reload- visit("/u/#{japanese_user.username}/preferences/interface")- expect(page).to have_css(".pref-show-original-content input[type='checkbox']:checked")+ topic_page.visit_topic(topic)+ expect(topic_page).to have_topic_title("Life strategies from The Art of War")+ expect(post_1_obj).to have_cooked_content(post_1.raw)+ expect(post_3_obj).to have_cooked_content(post_3.raw)+ expect(topic_page).to have_topic_tag("戦略")+ expect(sidebar).to have_section_link("戦略")++ modal = topic_page.open_content_language_preferences+ expect(modal).to be_open+ expect(modal).to have_logged_in_language_controls+ expect(modal).to have_understood_languages_description+ expect(modal).to have_locked_understood_language+ expect(modal).to be_automatic_translation_enabled+ modal.close++ SiteSetting.allow_user_locale = false+ topic_page.visit_topic(topic)+ modal = topic_page.open_content_language_preferences+ expect(modal).to have_read_only_interface_language("English")+ modal.close- # and reflects on topic view- visit("/t/#{topic.id}")- expect(topic_page.has_topic_title?("Life strategies from The Art of War")).to eq(true)+ preferences_interface_page.visit(japanese_user)+ expect(preferences_interface_page).to have_no_interface_language_section+ expect(preferences_interface_page).to have_locked_understood_language("English")endit "allows users to set their post's locale when posting" do@@ -160,6 +209,37 @@ describe "Content Localization" doexpect(topic_page.topic_title).to have_content("織田信長の生涯")end+ it "lets anonymous users control topic and post translation from the topic-side modal" do+ tag = Fabricate(:tag, name: "strategy", locale: "en")+ Fabricate(:tag_localization, tag:, locale: "ja", name: "戦略")+ Fabricate(:topic_tag, topic:, tag:)+ SiteSetting.tagging_enabled = true+ SiteSetting.navigation_menu = "sidebar"+ SiteSetting.default_navigation_menu_tags = tag.name++ visit("/t/#{topic.id}?tl=ja")+ expect(topic_page).to have_topic_title("孫子兵法からの人生戦略")+ expect(post_1_obj).to have_cooked_content("傑作は単なる軍事戦略についてではありません")+ expect(topic_page).to have_content_language_preferences_launcher+ expect(topic_page).to have_no_topic_admin_menu++ modal = topic_page.open_content_language_preferences+ expect(modal).to be_open+ expect(modal).to have_understood_languages_login_prompt+ expect(modal).to be_automatic_translation_enabled+ modal.disable_automatic_translation.save++ expect(topic_page).to have_topic_title("Life strategies from The Art of War")+ expect(post_1_obj).to have_cooked_content(post_1.raw)+ expect(topic_page).to have_topic_tag("戦略")+ expect(sidebar).to have_section_link("戦略")++ SiteSetting.set_locale_from_cookie = false+ page.refresh+ modal = topic_page.open_content_language_preferences+ expect(modal).to have_read_only_interface_language("English")+ end+fab!(:ja_topic) dotopic = Fabricate(:topic, title: "日本語で書かれたトピック", locale: "ja", user: admin)Fabricate(:post, topic:, locale: "ja", raw: "日本語の投稿です")@@ -414,9 +494,41 @@ describe "Content Localization" do# the per-post toggle shows the original content for the loaded postpost_21_obj.toggle_localized_contentexpect(post_21_obj.post).to have_content("日本語コンテンツ 21")+ expect(site_local_user.reload.user_option.automatically_translate).to eq(true)++ page.refresh+ scroll_to_post(21)+ expect(topic_page).to have_post_content(post_number: 21, content: "English translation 21")endend+ it "shows the topic-side launcher when only a later visible post has a localization" do+ late_localization_topic =+ Fabricate(:topic, title: "A long multilingual topic", locale: "en", user: admin)+ 20.times do |index|+ Fabricate(+ :post,+ topic: late_localization_topic,+ locale: "en",+ raw: "English post #{index + 1}",+ )+ end+ late_post =+ Fabricate(:post, topic: late_localization_topic, locale: "ja", raw: "最初のページより後の投稿")+ Fabricate(+ :post_localization,+ post: late_post,+ locale: "en",+ cooked: "<p>A post after the first page</p>",+ )++ sign_in(site_local_user)+ topic_page.visit_topic(late_localization_topic)++ expect(page).to have_no_css("#post_21", wait: 0)+ expect(topic_page).to have_content_language_preferences_launcher+ end+context "for html title" dofab!(:shady_topic) dotopic =diff --git a/spec/system/localized_internal_oneboxes_spec.rb b/spec/system/localized_internal_oneboxes_spec.rbindex 91f17f11119..89d617524e4 100644--- a/spec/system/localized_internal_oneboxes_spec.rb+++ b/spec/system/localized_internal_oneboxes_spec.rb@@ -65,8 +65,8 @@ describe "Localized internal oneboxes" doexpect(host_post_obj).to have_cooked_content("戦わずして勝つ")end- it "leaves the onebox in its original language when 'Show Original' is on" do- japanese_user.user_option.update!(show_original_content: true)+ it "leaves the onebox in its original language when automatic translation is off" do+ japanese_user.user_option.update!(automatically_translate: false)sign_in(japanese_user)visit("/t/#{host_topic.id}")diff --git a/spec/system/page_objects/modals/content_language_preferences.rb b/spec/system/page_objects/modals/content_language_preferences.rbnew file mode 100644index 00000000000..db31c37aa34--- /dev/null+++ b/spec/system/page_objects/modals/content_language_preferences.rb@@ -0,0 +1,78 @@+# frozen_string_literal: true++module PageObjects+ module Modals+ class ContentLanguagePreferences < PageObjects::Modals::Base+ MODAL_SELECTOR = ".content-language-preferences-modal"++ def initialize+ super(body_selector: "", modal_selector: MODAL_SELECTOR)+ end++ def has_understood_languages_login_prompt?+ has_css?(+ "#{full_modal_selector} " \+ ".content-language-preferences-modal__understood a[href='/login']",+ )+ end++ def has_logged_in_language_controls?+ body.has_css?(".form-kit__field[data-name='interfaceLanguage'] select") &&+ body.has_css?(".form-kit__field[data-name='understoodLanguages'] .multi-select") &&+ body.has_no_css?(".content-language-preferences-modal__understood")+ end++ def has_read_only_interface_language?(language)+ body.has_css?(".form-kit__field[data-name='interfaceLanguage'] select:disabled") &&+ body.has_css?(+ ".form-kit__field[data-name='interfaceLanguage'] option:checked",+ text: language,+ )+ end++ def has_understood_languages_description?+ body.has_css?(+ ".form-kit__field[data-name='understoodLanguages'] " \+ ".form-kit__container-description",+ text:+ "Topics and posts in these languages won’t be translated. " \+ "Your interface language is always included.",+ )+ end++ def has_locked_understood_language?+ understood_languages_dropdown.expand+ result =+ body.has_css?(+ ".form-kit__field[data-name='understoodLanguages'] " \+ ".selected-content .tag-choice.disabled",+ count: 1,+ )+ understood_languages_dropdown.collapse+ result+ end++ def automatic_translation_enabled?+ body.has_css?("[data-test-automatically-translate]:checked", visible: :all)+ end++ def disable_automatic_translation+ body.find(".form-kit__field-checkbox label").click+ self+ end++ def save+ click_primary_button+ self+ end++ private++ def understood_languages_dropdown+ PageObjects::Components::SelectKit.new(+ ".form-kit__field[data-name='understoodLanguages'] .multi-select",+ )+ end+ end+ end+enddiff --git a/spec/system/page_objects/pages/topic.rb b/spec/system/page_objects/pages/topic.rbindex ba005ca0c42..2d117776bd1 100644--- a/spec/system/page_objects/pages/topic.rb+++ b/spec/system/page_objects/pages/topic.rb@@ -449,6 +449,23 @@ module PageObjectsall(tags_selector).map(&:text)end+ def has_topic_tag?(name)+ has_css?(".title-wrapper .discourse-tag", text: name)+ end++ def has_content_language_preferences_launcher?+ has_css?(".topic-content-language-preferences")+ end++ def has_no_topic_admin_menu?+ has_no_css?(".topic-admin-menu-trigger")+ end++ def open_content_language_preferences+ find(".topic-content-language-preferences").click+ PageObjects::Modals::ContentLanguagePreferences.new+ end+privatedef within_topic_footer_buttonsdiff --git a/spec/system/page_objects/pages/user_preferences_interface.rb b/spec/system/page_objects/pages/user_preferences_interface.rbindex 291742b569d..f7fee944f6a 100644--- a/spec/system/page_objects/pages/user_preferences_interface.rb+++ b/spec/system/page_objects/pages/user_preferences_interface.rb@@ -3,6 +3,10 @@module PageObjectsmodule Pagesclass UserPreferencesInterface < PageObjects::Pages::Base+ AUTOMATIC_TRANSLATION_SELECTOR =+ "[data-setting-name='user-content-languages'] " \+ "[data-setting-name='user-automatically-translate'] input"+def visit(user)page.visit("/u/#{user.username}/preferences/interface")self@@ -28,6 +32,56 @@ module PageObjectsPageObjects::Components::SelectKit.new("[data-setting-name='user-text-size'] .combo-box")end+ def has_interface_language_section?+ page.has_css?("[data-setting-name='user-locale'] .control-label")+ end++ def has_no_interface_language_section?+ page.has_no_css?("[data-setting-name='user-locale'] .control-label")+ end++ def has_content_languages_section?+ page.has_css?("[data-setting-name='user-content-languages'] .control-label")+ end++ def automatic_translation_enabled?+ page.has_css?("#{AUTOMATIC_TRANSLATION_SELECTOR}:checked")+ end++ def automatic_translation_disabled?+ page.has_no_css?("#{AUTOMATIC_TRANSLATION_SELECTOR}:checked")+ end++ def disable_automatic_translation+ page.find("#{AUTOMATIC_TRANSLATION_SELECTOR}:checked").click+ self+ end++ def enable_automatic_translation+ page.find("#{AUTOMATIC_TRANSLATION_SELECTOR}:not(:checked)").click+ self+ end++ def has_understood_language?(locale)+ understood_languages_dropdown.expand+ result =+ page.has_css?("#understood-languages-selector .selected-choice[data-value='#{locale}']")+ understood_languages_dropdown.collapse+ result+ end++ def has_locked_understood_language?(language = nil)+ understood_languages_dropdown.expand+ result =+ page.has_css?(+ "#understood-languages-selector .selected-content .tag-choice.disabled",+ count: 1,+ text: language,+ )+ understood_languages_dropdown.collapse+ result+ end+def light_scheme_dropdownPageObjects::Components::SelectKit.new(".light-color-scheme .select-kit")end@@ -63,10 +117,16 @@ module PageObjectsenddef save_changes- click_button "Save Changes"- expect(page).to have_content(I18n.t("js.saved"))+ find(".save-changes").click+ expect(page).to have_css(".saved")selfend++ private++ def understood_languages_dropdown+ PageObjects::Components::SelectKit.new("#understood-languages-selector")+ endendendend