A platform for community discussion. Free, open, simple.
UX: Confirm languages before translating theme text (#44070)
Natalie Tay811c607d3e40
diff --git a/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_theme_translations_controller.rb b/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_theme_translations_controller.rbindex 50994e1edc3..e84cd56193a 100644--- a/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_theme_translations_controller.rb+++ b/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_theme_translations_controller.rb@@ -12,7 +12,18 @@ module DiscourseAilocale = params[:locale].to_ssource_locale = (locale.present? && LocaleSiteSetting.valid_value?(locale)) ? locale : "en"- Jobs.enqueue(:localize_theme_translations, theme_id: theme.id, source_locale:)+ target_locales =+ SiteSetting.content_localization_supported_locales.to_s.split("|") - [source_locale]+ target_locales &= Array(params[:target_locales]) if params.key?(:target_locales)+ override_existing = ActiveModel::Type::Boolean.new.cast(params[:override_existing]) == true++ Jobs.enqueue(+ :localize_theme_translations,+ theme_id: theme.id,+ source_locale:,+ target_locales:,+ override_existing:,+ )head :no_contentenddiff --git a/plugins/discourse-ai/app/jobs/regular/localize_theme_translations.rb b/plugins/discourse-ai/app/jobs/regular/localize_theme_translations.rbindex fd1158aa102..66a773fcd88 100644--- a/plugins/discourse-ai/app/jobs/regular/localize_theme_translations.rb+++ b/plugins/discourse-ai/app/jobs/regular/localize_theme_translations.rb@@ -8,13 +8,20 @@ module Jobstheme_id = args[:theme_id]raise Discourse::InvalidParameters.new(:theme_id) if theme_id.blank?- theme = Theme.find_by(id: theme_id)+ theme = Theme.includes(:theme_fields).find_by(id: theme_id)return if theme.blank?+ source_locale = args[:source_locale].presence || "en"target_locales = SiteSetting.content_localization_supported_locales.to_s.split("|")+ target_locales &= Array(args[:target_locales]) if args.key?(:target_locales)+ target_locales -= [source_locale]return if target_locales.empty?- source_locale = args[:source_locale].presence || "en"+ override_existing = args[:override_existing] == true+ target_keys =+ target_locales.to_h do |locale|+ [locale, (load_yaml(theme, locale).keys + load_overrides(theme, locale).keys).to_set]+ endnon_en_source = source_locale != "en"source_overrides = non_en_source ? load_overrides(theme, source_locale) : {}@@ -32,7 +39,8 @@ module Jobsend(target_locales - [effective_locale]).each do |locale|- translate_and_upsert(theme, key, text, locale)+ next if !override_existing && target_keys[locale].include?(key)+ translate_and_upsert(theme, key, text, locale, override_existing:)endendend@@ -47,7 +55,10 @@ module Jobsenddef load_yaml(theme, locale)- field = theme.theme_fields.find_by(target_id: Theme.targets[:translations], name: locale)+ field =+ theme.theme_fields.find do |entry|+ entry.target_id == Theme.targets[:translations] && entry.name == locale+ endreturn {} if field.blank?data = field.raw_translation_data[locale.to_sym] || {}ThemeTranslationManager@@ -55,7 +66,7 @@ module Jobs.each_with_object({}) { |tm, h| h[tm.key] = tm.default }end- def translate_and_upsert(theme, key, text, locale)+ def translate_and_upsert(theme, key, text, locale, override_existing:)value =DiscourseAi::Translation::ShortTextTranslator.new(text: text,@@ -63,14 +74,19 @@ module Jobs).translatereturn if value.blank?- record =- ThemeTranslationOverride.find_or_initialize_by(- theme_id: theme.id,- locale: locale,- translation_key: key,- )- record.value = value- record.save!+ theme.with_lock do+ record =+ ThemeTranslationOverride.find_or_initialize_by(+ theme_id: theme.id,+ locale: locale,+ translation_key: key,+ )+ next if !override_existing && (record.persisted? || load_yaml(theme, locale).key?(key))+ next unless load_yaml(theme, "en").key?(key)++ record.value = value+ record.save!+ endrescue FinalDestination::SSRFDetector::LookupFailedError# transient lookup failuresrescue => ediff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/modal/ai-theme-translation-modal.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/modal/ai-theme-translation-modal.gjsnew file mode 100644index 00000000000..36588747bad--- /dev/null+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/modal/ai-theme-translation-modal.gjs@@ -0,0 +1,122 @@+import Component from "@glimmer/component";+import { cached } from "@glimmer/tracking";+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 DModal from "discourse/ui-kit/d-modal";+import { i18n } from "discourse-i18n";++export default class AiThemeTranslationModal extends Component {+ @service siteSettings;+ @service toasts;++ @cached+ get formData() {+ return { override_existing: false };+ }++ get sourceLanguage() {+ return (+ this.siteSettings.available_locales.find(+ (locale) => locale.value === this.args.model.locale+ )?.name ?? this.args.model.locale+ );+ }++ @cached+ get targetLocales() {+ return [+ ...new Set(+ (this.siteSettings.content_localization_supported_locales || "").split(+ "|"+ )+ ),+ ]+ .filter((locale) => locale && locale !== this.args.model.locale)+ .map((value) => ({+ value,+ name:+ this.siteSettings.available_locales.find(+ (locale) => locale.value === value+ )?.name ?? value,+ }));+ }++ @action+ async translate(data) {+ if (!this.targetLocales.length) {+ return;+ }+ try {+ await ajax("/admin/plugins/discourse-ai/ai-theme-translations", {+ type: "POST",+ data: {+ theme_id: this.args.model.theme.id,+ locale: this.args.model.locale,+ target_locales: this.targetLocales.map((locale) => locale.value),+ override_existing: data.override_existing,+ },+ });+ this.args.closeModal();+ this.toasts.success({+ duration: "short",+ data: {+ message: i18n(+ "discourse_ai.translations.theme_translations.translate.queued"+ ),+ },+ });+ } catch (error) {+ popupAjaxError(error);+ }+ }++ <template>+ <DModal+ class="ai-theme-translation-modal"+ @closeModal={{@closeModal}}+ @inline={{@inline}}+ @title={{i18n+ "discourse_ai.translations.theme_translations.translate.label"+ }}+ >+ <:body>+ <p>{{i18n+ "discourse_ai.translations.theme_translations.translate.confirm"+ language=this.sourceLanguage+ }}</p>+ {{#if this.targetLocales.length}}+ <ul class="ai-theme-translation-modal__languages">+ {{#each this.targetLocales as |locale|}}+ <li>{{locale.name}}</li>+ {{/each}}+ </ul>+ <Form @data={{this.formData}} @onSubmit={{this.translate}} as |form|>+ <form.Field+ @format="full"+ @name="override_existing"+ @title={{i18n+ "discourse_ai.translations.theme_translations.translate.override_existing"+ }}+ @type="checkbox"+ as |field|+ >+ <field.Control />+ </form.Field>+ <form.Actions>+ <form.Submit+ @label="discourse_ai.translations.theme_translations.translate.label"+ />+ </form.Actions>+ </Form>+ {{else}}+ <p>{{i18n+ "discourse_ai.translations.theme_translations.translate.no_targets"+ }}</p>+ {{/if}}+ </:body>+ </DModal>+ </template>+}diff --git a/plugins/discourse-ai/assets/javascripts/discourse/connectors/admin-customize-theme-translation-selector/ai-theme-translate.gjs b/plugins/discourse-ai/assets/javascripts/discourse/connectors/admin-customize-theme-translation-selector/ai-theme-translate.gjsindex a17d32eeec3..29a24fb6883 100644--- a/plugins/discourse-ai/assets/javascripts/discourse/connectors/admin-customize-theme-translation-selector/ai-theme-translate.gjs+++ b/plugins/discourse-ai/assets/javascripts/discourse/connectors/admin-customize-theme-translation-selector/ai-theme-translate.gjs@@ -1,10 +1,8 @@import Component from "@glimmer/component";import { action } from "@ember/object";import { service } from "@ember/service";-import { ajax } from "discourse/lib/ajax";-import { extractError } from "discourse/lib/ajax-error";import DButton from "discourse/ui-kit/d-button";-import { i18n } from "discourse-i18n";+import AiThemeTranslationModal from "discourse/plugins/discourse-ai/discourse/components/modal/ai-theme-translation-modal";export default class AiThemeTranslate extends Component {static shouldRender(args, { siteSettings }) {@@ -13,37 +11,12 @@ export default class AiThemeTranslate extends Component {);}- @service toasts;+ @service modal;@action- async translate() {+ translate() {const { theme, locale } = this.args.outletArgs;- try {- await ajax("/admin/plugins/discourse-ai/ai-theme-translations", {- type: "POST",- data: { theme_id: theme.id, locale },- });- this.toasts.success({- duration: "short",- data: {- message: i18n(- "discourse_ai.translations.theme_translations.translate.queued"- ),- },- });- } catch (e) {- this.toasts.error({- duration: "short",- data: {- message: extractError(- e,- i18n(- "discourse_ai.translations.theme_translations.translate.failed"- )- ),- },- });- }+ this.modal.show(AiThemeTranslationModal, { model: { theme, locale } });}<template>diff --git a/plugins/discourse-ai/assets/stylesheets/modules/translation/admin/translations.scss b/plugins/discourse-ai/assets/stylesheets/modules/translation/admin/translations.scssindex e0d5be8fac6..fe5f036e559 100644--- a/plugins/discourse-ai/assets/stylesheets/modules/translation/admin/translations.scss+++ b/plugins/discourse-ai/assets/stylesheets/modules/translation/admin/translations.scss@@ -473,3 +473,7 @@}}}++.ai-theme-translation-modal__languages {+ padding-block-end: var(--space-2);+}diff --git a/plugins/discourse-ai/config/locales/client.en.yml b/plugins/discourse-ai/config/locales/client.en.ymlindex a23f621e262..67e2b649a18 100644--- a/plugins/discourse-ai/config/locales/client.en.yml+++ b/plugins/discourse-ai/config/locales/client.en.yml@@ -636,6 +636,9 @@ en:translate:label: "Translate"title: "Translate to all supported languages using the selected language as the source"+ confirm: "Translate from %{language} to:"+ override_existing: "Also override existing translations"+ no_targets: "No other target languages are configured. Add languages to the content localization supported locales setting."queued: "Translation queued"failed: "Failed to queue translation"translations_menu:diff --git a/plugins/discourse-ai/spec/jobs/regular/localize_theme_translations_spec.rb b/plugins/discourse-ai/spec/jobs/regular/localize_theme_translations_spec.rbindex 96d365f8ce8..107f5af2050 100644--- a/plugins/discourse-ai/spec/jobs/regular/localize_theme_translations_spec.rb+++ b/plugins/discourse-ai/spec/jobs/regular/localize_theme_translations_spec.rb@@ -57,6 +57,16 @@ describe Jobs::LocalizeThemeTranslations do)end+ it "only translates to confirmed targets that are still supported" do+ translator = mock+ translator.stubs(:translate).returns("translated")+ DiscourseAi::Translation::ShortTextTranslator.stubs(:new).returns(translator)++ job.execute(theme_id: theme.id, target_locales: %w[en fr ja])++ expect(theme.theme_translation_overrides.reload.pluck(:locale)).to eq(["fr"])+ end+it "skips empty translations" dotranslator = mocktranslator.stubs(:translate).returns("")@@ -86,6 +96,67 @@ describe Jobs::LocalizeThemeTranslations dojob.execute(theme_id: theme.id)end+ it "preserves overrides and translations shipped in locale files by default" do+ theme.set_field(target: :translations, name: "fr", value: "fr:\n greeting: Bonjour\n")+ theme.save!+ override =+ ThemeTranslationOverride.create!(+ theme: theme,+ locale: "es",+ translation_key: "greeting",+ value: "Hola",+ )++ DiscourseAi::Translation::ShortTextTranslator.expects(:new).never+ job.execute(theme_id: theme.id)++ expect(override.reload.value).to eq("Hola")+ expect(theme.theme_translation_overrides.where(locale: "fr")).not_to exist+ end++ it "replaces overrides and shipped translations when explicitly requested" do+ theme.set_field(target: :translations, name: "fr", value: "fr:\n greeting: Bonjour\n")+ theme.save!+ ThemeTranslationOverride.create!(+ theme: theme,+ locale: "es",+ translation_key: "greeting",+ value: "Hola",+ )+ translator = mock+ translator.stubs(:translate).returns("translated")+ DiscourseAi::Translation::ShortTextTranslator.stubs(:new).returns(translator)++ job.execute(theme_id: theme.id, override_existing: true)++ expect(theme.theme_translation_overrides.reload.pluck(:locale, :value)).to contain_exactly(+ %w[fr translated],+ %w[es translated],+ )+ end++ it "preserves a manual translation saved while AI is running" do+ SiteSetting.content_localization_supported_locales = "fr"+ theme_id = theme.id+ translator = Object.new+ translator.define_singleton_method(:translate) do+ ThemeTranslationOverride.create!(+ theme_id: theme_id,+ locale: "fr",+ translation_key: "greeting",+ value: "Manual translation",+ )+ "AI translation"+ end+ DiscourseAi::Translation::ShortTextTranslator.stubs(:new).returns(translator)++ job.execute(theme_id: theme.id)++ expect(theme.theme_translation_overrides.reload.find_by!(locale: "fr").value).to eq(+ "Manual translation",+ )+ end+describe "with a non-en source locale" dobefore dotheme.set_field(target: :translations, name: "fr", value: <<~YAML)@@ -111,7 +182,7 @@ describe Jobs::LocalizeThemeTranslations do.with(has_entries(text: "Salut", target_locale: "es")).returns(translator)- job.execute(theme_id: theme.id, source_locale: "fr")+ job.execute(theme_id: theme.id, source_locale: "fr", override_existing: true)expect(ThemeTranslationOverride.where(theme_id: theme.id, value: "translated").pluck(:locale),@@ -127,10 +198,10 @@ describe Jobs::LocalizeThemeTranslations do.with(has_entries(text: "Bonjour", target_locale: "es")).returns(translator)- job.execute(theme_id: theme.id, source_locale: "fr")+ job.execute(theme_id: theme.id, source_locale: "fr", override_existing: true)end- it "falls back to en override, then en yaml, when source locale has neither and translates into the source locale too" do+ it "falls back to en override, then en yaml, when the source locale has neither" dotheme.theme_fields.find_by(target_id: Theme.targets[:translations], name: "fr").destroy!ThemeTranslationOverride.create!(theme_id: theme.id,@@ -141,16 +212,12 @@ describe Jobs::LocalizeThemeTranslations dotranslator = mocktranslator.stubs(:translate).returns("translated")- DiscourseAi::Translation::ShortTextTranslator- .expects(:new)- .with(has_entries(text: "Howdy", target_locale: "fr"))- .returns(translator)DiscourseAi::Translation::ShortTextTranslator.expects(:new).with(has_entries(text: "Howdy", target_locale: "es")).returns(translator)- job.execute(theme_id: theme.id, source_locale: "fr")+ job.execute(theme_id: theme.id, source_locale: "fr", override_existing: true)endit "invalidates the baked theme JS so new locales reach the browser" do@@ -168,7 +235,7 @@ describe Jobs::LocalizeThemeTranslations doDiscourseAi::Translation::ShortTextTranslator.stubs(:new).returns(translator)Theme.any_instance.expects(:remove_from_cache!).at_least_once- job.execute(theme_id: theme.id, source_locale: "fr")+ job.execute(theme_id: theme.id, source_locale: "fr", override_existing: true)expect(theme.theme_fields.where(target_id: Theme.targets[:translations]).pluck(:value_baked),@@ -188,7 +255,7 @@ describe Jobs::LocalizeThemeTranslations dotranslator.stubs(:translate).returns("translated")DiscourseAi::Translation::ShortTextTranslator.stubs(:new).returns(translator)- job.execute(theme_id: theme.id, source_locale: "fr")+ job.execute(theme_id: theme.id, source_locale: "fr", override_existing: true)expect(theme.theme_fields.where(target_id: Theme.targets[:translations]).pluck(:value_baked),@@ -202,7 +269,7 @@ describe Jobs::LocalizeThemeTranslations do).to eq("translated")end- it "excludes only the effective source locale from target locales per key" do+ it "excludes the selected source locale and each field's fallback language" dotheme.set_field(target: :translations, name: "en", value: <<~YAML)en:greeting: "Hello"@@ -214,7 +281,7 @@ describe Jobs::LocalizeThemeTranslations dotranslator.stubs(:translate).returns("translated")DiscourseAi::Translation::ShortTextTranslator.stubs(:new).returns(translator)- job.execute(theme_id: theme.id, source_locale: "fr")+ job.execute(theme_id: theme.id, source_locale: "fr", override_existing: true)greeting_locales =ThemeTranslationOverride.where(@@ -229,10 +296,8 @@ describe Jobs::LocalizeThemeTranslations dovalue: "translated",).pluck(:locale)- # greeting has a fr yaml → effective source fr → targets en and esexpect(greeting_locales).to contain_exactly("en", "es")- # farewell has no fr anywhere → effective source en → targets fr and es- expect(farewell_locales).to contain_exactly("fr", "es")+ expect(farewell_locales).to contain_exactly("es")endendenddiff --git a/plugins/discourse-ai/spec/requests/admin/ai_theme_translations_controller_spec.rb b/plugins/discourse-ai/spec/requests/admin/ai_theme_translations_controller_spec.rbindex 4fa2031a951..7454ec74348 100644--- a/plugins/discourse-ai/spec/requests/admin/ai_theme_translations_controller_spec.rb+++ b/plugins/discourse-ai/spec/requests/admin/ai_theme_translations_controller_spec.rb@@ -5,7 +5,10 @@ describe DiscourseAi::Admin::AiThemeTranslationsController dofab!(:user)fab!(:theme)- before { enable_current_plugin }+ before do+ enable_current_plugin+ SiteSetting.content_localization_supported_locales = "en|fr|es"+ enddescribe "#create" docontext "when logged in as admin" do@@ -17,6 +20,8 @@ describe DiscourseAi::Admin::AiThemeTranslationsController doargs: {theme_id: theme.id,source_locale: "en",+ target_locales: %w[fr es],+ override_existing: false,},) dopost "/admin/plugins/discourse-ai/ai-theme-translations.json",@@ -34,6 +39,8 @@ describe DiscourseAi::Admin::AiThemeTranslationsController doargs: {theme_id: theme.id,source_locale: "fr",+ target_locales: %w[en es],+ override_existing: false,},) dopost "/admin/plugins/discourse-ai/ai-theme-translations.json",@@ -50,6 +57,8 @@ describe DiscourseAi::Admin::AiThemeTranslationsController doargs: {theme_id: theme.id,source_locale: "en",+ target_locales: %w[fr es],+ override_existing: false,},) dopost "/admin/plugins/discourse-ai/ai-theme-translations.json",@@ -60,6 +69,45 @@ describe DiscourseAi::Admin::AiThemeTranslationsController doendend+ it "queues only confirmed supported targets and explicitly requested replacement" do+ expect_enqueued_with(+ job: :localize_theme_translations,+ args: {+ theme_id: theme.id,+ source_locale: "en",+ target_locales: ["fr"],+ override_existing: true,+ },+ ) do+ post "/admin/plugins/discourse-ai/ai-theme-translations.json",+ params: {+ theme_id: theme.id,+ locale: "en",+ target_locales: %w[en fr ja],+ override_existing: "true",+ }+ end+ expect(response.status).to eq(204)+ end++ it "does not treat the string false as permission to replace translations" do+ expect_enqueued_with(+ job: :localize_theme_translations,+ args: {+ theme_id: theme.id,+ source_locale: "en",+ target_locales: %w[fr es],+ override_existing: false,+ },+ ) do+ post "/admin/plugins/discourse-ai/ai-theme-translations.json",+ params: {+ theme_id: theme.id,+ override_existing: "false",+ }+ end+ end+it "returns 404 when the theme does not exist" dopost "/admin/plugins/discourse-ai/ai-theme-translations.json", params: { theme_id: -999 }expect(response.status).to eq(404)diff --git a/plugins/discourse-ai/test/javascripts/integration/components/ai-theme-translation-modal-test.gjs b/plugins/discourse-ai/test/javascripts/integration/components/ai-theme-translation-modal-test.gjsnew file mode 100644index 00000000000..f0d35f51471--- /dev/null+++ b/plugins/discourse-ai/test/javascripts/integration/components/ai-theme-translation-modal-test.gjs@@ -0,0 +1,93 @@+import { click, render } from "@ember/test-helpers";+import { module, test } from "qunit";+import { setupRenderingTest } from "discourse/tests/helpers/component-test";+import pretender from "discourse/tests/helpers/create-pretender";+import formKit from "discourse/tests/helpers/form-kit-helper";+import AiThemeTranslationModal from "discourse/plugins/discourse-ai/discourse/components/modal/ai-theme-translation-modal";++module(+ "Integration | Component | Modal | AiThemeTranslationModal",+ function (hooks) {+ setupRenderingTest(hooks);++ hooks.beforeEach(function () {+ this.siteSettings.available_locales = [+ { name: "English", value: "en" },+ { name: "French", value: "fr" },+ { name: "Spanish", value: "es" },+ ];+ this.siteSettings.content_localization_supported_locales = "en|fr|es";+ this.model = {+ theme: { id: 1, name: "Community welcome" },+ locale: "en",+ };+ this.closed = false;+ this.closeModal = () => (this.closed = true);+ this.requests = [];+ pretender.post(+ "/admin/plugins/discourse-ai/ai-theme-translations",+ (request) => {+ this.requests.push(new URLSearchParams(request.requestBody));+ return [204, {}, ""];+ }+ );+ });++ for (const replace of [false, true]) {+ test(`confirms languages and submits override_existing=${replace}`, async function (assert) {+ await render(+ <template>+ <AiThemeTranslationModal+ @closeModal={{this.closeModal}}+ @inline={{true}}+ @model={{this.model}}+ />+ </template>+ );++ assert+ .dom(".ai-theme-translation-modal")+ .includesText("Translate from English");+ assert+ .dom(".ai-theme-translation-modal__languages")+ .hasText("French Spanish");+ assert.dom('input[name="override_existing"]').isNotChecked();+ assert.strictEqual(this.requests.length, 0);+ if (replace) {+ await click('input[name="override_existing"]');+ }+ await formKit().submit();++ assert.strictEqual(this.requests.length, 1);+ assert.strictEqual(+ this.requests[0].get("override_existing"),+ String(replace)+ );+ assert.strictEqual(this.requests[0].get("locale"), "en");+ assert.deepEqual(this.requests[0].getAll("target_locales[]"), [+ "fr",+ "es",+ ]);+ assert.true(this.closed);+ });+ }++ test("does not offer to submit without target languages", async function (assert) {+ this.siteSettings.content_localization_supported_locales = "en";+ await render(+ <template>+ <AiThemeTranslationModal+ @closeModal={{this.closeModal}}+ @inline={{true}}+ @model={{this.model}}+ />+ </template>+ );+ assert+ .dom(".ai-theme-translation-modal")+ .includesText("No other target languages");+ assert.dom('button[type="submit"]').doesNotExist();+ assert.strictEqual(this.requests.length, 0);+ });+ }+);