Skip to content

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

UX: make translations easier to set up by showing requisite site setting (#35078)

Krisa1fac56e5b53
diff --git a/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_translations_controller.rb b/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_translations_controller.rb
index a2b3f091579..93db652608c 100644
--- a/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_translations_controller.rb
+++ b/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_translations_controller.rb
@@ -13,7 +13,12 @@ module DiscourseAi
return(
render json:
base_result.merge(
- { translation_progress: [], total: 0, posts_with_detected_locale: 0 },
+ {
+ translation_progress: [],
+ total: 0,
+ posts_with_detected_locale: 0,
+ no_locales_configured: true,
+ },
)
)
end
@@ -41,6 +46,7 @@ module DiscourseAi
DiscourseAi::Translation.enabled? &&
SiteSetting.ai_translation_backfill_max_age_days > 0,
backfill_enabled: DiscourseAi::Translation.backfill_enabled?,
+ translation_enabled: SiteSetting.ai_translation_enabled,
}
end
end
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-translations.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-translations.gjs
index 94e697eb235..91dff7ed910 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-translations.gjs
+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-translations.gjs
@@ -1,24 +1,196 @@
import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";
import { hash } from "@ember/helper";
+import { on } from "@ember/modifier";
+import { action } from "@ember/object";
import { service } from "@ember/service";
+import DButton from "discourse/components/d-button";
import DPageSubheader from "discourse/components/d-page-subheader";
-import DTooltip from "discourse/components/d-tooltip";
-import InterpolatedTranslation from "discourse/components/interpolated-translation";
-import icon from "discourse/helpers/d-icon";
+import DToggleSwitch from "discourse/components/d-toggle-switch";
+import { ajax } from "discourse/lib/ajax";
+import { popupAjaxError } from "discourse/lib/ajax-error";
import { i18n } from "discourse-i18n";
import AdminConfigAreaCard from "admin/components/admin-config-area-card";
-import AdminConfigAreaEmptyList from "admin/components/admin-config-area-empty-list";
import Chart from "admin/components/chart";
+import MultiSelect from "select-kit/components/multi-select";
export default class AiTranslations extends Component {
+ @service router;
@service store;
@service languageNameLookup;
@service site;
+ @service siteSettings;
@tracked data = this.args.model?.translation_progress;
@tracked done = this.args.model?.posts_with_detected_locale;
@tracked total = this.args.model?.total;
+ @tracked
+ translationEnabled =
+ this.args.model?.translation_enabled &&
+ !this.args.model?.no_locales_configured;
+ @tracked enabled = this.args.model?.enabled;
+ @tracked
+ selectedLocales = this.siteSettings.content_localization_supported_locales
+ ? this.siteSettings.content_localization_supported_locales.split("|")
+ : [];
+ @tracked
+ originalLocales = this.siteSettings.content_localization_supported_locales
+ ? this.siteSettings.content_localization_supported_locales.split("|")
+ : [];
+ @tracked isSavingLocales = false;
+ @tracked isTogglingTranslation = false;
+
+ get localesChanged() {
+ const current = [...this.selectedLocales].sort().join("|");
+ const original = [...this.originalLocales].sort().join("|");
+ return current !== original;
+ }
+
+ get showLocaleSelector() {
+ const noLocales =
+ this.args.model?.no_locales_configured ||
+ this.originalLocales.length === 0;
+ return noLocales && !this.translationEnabled;
+ }
+
+ get showLocalizationSettingsButton() {
+ return this.enabled || !this.showLocaleSelector;
+ }
+
+ get isToggleDisabled() {
+ return (
+ this.isTogglingTranslation ||
+ (this.args.model?.no_locales_configured &&
+ this.originalLocales.length === 0)
+ );
+ }
+
+ get availableLocales() {
+ const locales = this.siteSettings.available_locales;
+ if (!locales) {
+ return [];
+ }
+
+ return locales;
+ }
+
+ get settingsUrl() {
+ return this.router.urlFor(
+ "adminPlugins.show.discourse-ai-features.edit",
+ this.args.model.translation_id
+ );
+ }
+
+ get descriptionTooltip() {
+ return i18n("discourse_ai.translations.stats.description_tooltip", {
+ count: this.total,
+ });
+ }
+
+ @action
+ navigateToLocalizationSettings() {
+ this.router.transitionTo("adminConfig.localization.settings", {
+ queryParams: { filter: "content_localization_supported_locales" },
+ });
+ }
+
+ @action
+ updateSelectedLocales(locales) {
+ this.selectedLocales = locales;
+ }
+
+ @action
+ async saveLocales() {
+ this.isSavingLocales = true;
+ try {
+ // also enable content_localization_enabled when we're setting locales
+ if (this.selectedLocales.length > 0) {
+ await ajax("/admin/site_settings/content_localization_enabled", {
+ type: "PUT",
+ data: { content_localization_enabled: true },
+ });
+ }
+
+ await ajax(
+ "/admin/site_settings/content_localization_supported_locales",
+ {
+ type: "PUT",
+ data: {
+ content_localization_supported_locales:
+ this.selectedLocales.join("|"),
+ },
+ }
+ );
+ this.originalLocales = [...this.selectedLocales];
+
+ if (this.selectedLocales.length > 0) {
+ this.args.model.no_locales_configured = false;
+ }
+
+ if (this.translationEnabled) {
+ window.location.reload();
+ }
+ } catch (e) {
+ popupAjaxError(e);
+ } finally {
+ this.isSavingLocales = false;
+ }
+ }
+
+ @action
+ cancelLocales() {
+ this.selectedLocales = [...this.originalLocales];
+ }
+
+ @action
+ resetLocales() {
+ this.selectedLocales = [];
+ }
+
+ @action
+ async toggleTranslationEnabled() {
+ if (this.isTogglingTranslation) {
+ return;
+ }
+
+ if (!this.translationEnabled && this.originalLocales.length === 0) {
+ return;
+ }
+
+ this.isTogglingTranslation = true;
+ try {
+ if (!this.translationEnabled && this.originalLocales.length > 0) {
+ await ajax("/admin/site_settings/content_localization_enabled", {
+ type: "PUT",
+ data: { content_localization_enabled: true },
+ });
+ }
+
+ await ajax("/admin/site_settings/ai_translation_enabled", {
+ type: "PUT",
+ data: { ai_translation_enabled: !this.translationEnabled },
+ });
+ this.translationEnabled = !this.translationEnabled;
+
+ if (this.translationEnabled && !this.args.model.no_locales_configured) {
+ const response = await ajax(
+ "/admin/plugins/discourse-ai/ai-translations"
+ );
+ if (response.enabled) {
+ this.data = response.translation_progress;
+ this.total = response.total;
+ this.done = response.posts_with_detected_locale;
+ this.enabled = response.enabled;
+ }
+ } else {
+ this.enabled = false;
+ }
+ } catch (e) {
+ popupAjaxError(e);
+ } finally {
+ this.isTogglingTranslation = false;
+ }
+ }
get chartRightPadding() {
const max = Math.max(...this.data.map(({ done }) => done));
@@ -170,13 +342,15 @@ export default class AiTranslations extends Component {
@learnMoreUrl="https://meta.discourse.org/t/-/370969"
>
<:actions as |actions|>
- {{#if @model.enabled}}
+ {{#if this.enabled}}
<actions.Default
@label="discourse_ai.translations.admin_actions.translation_settings"
@route="adminPlugins.show.discourse-ai-features.edit"
@routeModels={{@model.translation_id}}
class="ai-translation-settings-button"
/>
+ {{/if}}
+ {{#if this.showLocalizationSettingsButton}}
<actions.Default
@label="discourse_ai.translations.admin_actions.localization_settings"
@route="adminConfig.localization.settings"
@@ -186,58 +360,95 @@ export default class AiTranslations extends Component {
</:actions>
</DPageSubheader>
- {{#if @model.enabled}}
- <AdminConfigAreaCard class="ai-translation__charts">
+ {{#if this.showLocaleSelector}}
+ <div class="alert alert-info">
+ <div class="settings">
+ <div class="setting">
+ <div class="setting-label">
+ <label>{{i18n
+ "discourse_ai.translations.supported_locales"
+ }}</label>
+ </div>
+ <div class="setting-value">
+ <div class="ai-translations__locale-input-row">
+ <MultiSelect
+ @value={{this.selectedLocales}}
+ @content={{this.availableLocales}}
+ @nameProperty="name"
+ @valueProperty="value"
+ @onChange={{this.updateSelectedLocales}}
+ @options={{hash allowAny=false}}
+ />
+ {{#if this.localesChanged}}
+ <div class="setting-controls">
+ <DButton
+ @action={{this.saveLocales}}
+ @icon="check"
+ @isLoading={{this.isSavingLocales}}
+ @ariaLabel="save"
+ class="ok setting-controls__ok"
+ />
+ <DButton
+ @action={{this.cancelLocales}}
+ @icon="xmark"
+ @isLoading={{this.isSavingLocales}}
+ @ariaLabel="cancel"
+ class="cancel setting-controls__cancel"
+ />
+ </div>
+ {{else if this.selectedLocales.length}}
+ <DButton
+ @action={{this.resetLocales}}
+ @icon="arrow-rotate-left"
+ @label="admin.settings.reset"
+ class="undo setting-controls__undo"
+ />
+ {{/if}}
+ </div>
+ <div class="desc">{{i18n
+ "discourse_ai.translations.supported_locales_description"
+ }}</div>
+ </div>
+ </div>
+ </div>
+ </div>
+ {{/if}}
+
+ <div class="ai-translations__toggle-container">
+ <DToggleSwitch
+ @state={{this.translationEnabled}}
+ @label="discourse_ai.translations.admin_actions.enable_translations"
+ disabled={{this.isToggleDisabled}}
+ {{on "click" this.toggleTranslationEnabled}}
+ />
+ </div>
+
+ {{#if this.enabled}}
+ <AdminConfigAreaCard class="ai-translations__charts">
<:header>
- <InterpolatedTranslation
- @key="discourse_ai.translations.progress_chart.title"
- as |Placeholder|
- >
- <Placeholder @name="tooltip">
- <DTooltip>
- <:trigger>
- {{icon "circle-question"}}
- </:trigger>
- <:content>
- {{i18n
- "discourse_ai.translations.stats.description_tooltip"
- }}
- </:content>
- </DTooltip>
- </Placeholder>
- </InterpolatedTranslation>
+ {{i18n "discourse_ai.translations.progress_chart.title"}}
</:header>
<:content>
- <div class="ai-translation__stats-container">
- <div class="ai-translation__stat-item">
- <span class="ai-translation__stat-label">
- {{i18n
- this.descriptionKey
- (hash done=this.done total=this.total)
- }}
+ <div class="ai-translations__stats-container">
+ <div class="ai-translations__stat-item">
+ <span class="ai-translations__stat-label">
+ {{this.descriptionTooltip}}
{{#unless @model.backfill_enabled}}
{{i18n "discourse_ai.translations.stats.backfill_disabled"}}
{{/unless}}
</span>
</div>
</div>
- <div class="ai-translation__chart-container">
+ <div class="ai-translations__chart-container">
<Chart
@chartConfig={{this.chartConfig}}
@loadChartDataLabelsPlugin={{true}}
- class="ai-translation__chart"
+ class="ai-translations__chart"
/>
</div>
</:content>
</AdminConfigAreaCard>
- {{else}}
- <AdminConfigAreaEmptyList
- @ctaLabel="discourse_ai.translations.admin_actions.disabled_state.configure"
- @ctaRoute="adminPlugins.show.discourse-ai-features.edit"
- @ctaRouteModels={{@model.translation_id}}
- @ctaClass="ai-translations__configure-button"
- @emptyLabel="discourse_ai.translations.admin_actions.disabled_state.empty_label"
- />
+
{{/if}}
</div>
diff --git a/plugins/discourse-ai/assets/stylesheets/modules/translation/common/admin-translations.scss b/plugins/discourse-ai/assets/stylesheets/modules/translation/common/admin-translations.scss
index a68de0e046d..0030097cb70 100644
--- a/plugins/discourse-ai/assets/stylesheets/modules/translation/common/admin-translations.scss
+++ b/plugins/discourse-ai/assets/stylesheets/modules/translation/common/admin-translations.scss
@@ -3,4 +3,33 @@
--chart-remaining-color: var(--tertiary-low);
--chart-text-color: var(--primary);
--chart-label-color: var(--secondary);
+
+ &__charts {
+ margin-top: 2em;
+ }
+
+ &__locale-input-row {
+ display: flex;
+ align-items: center;
+ gap: 0.5em;
+
+ .multi-select {
+ flex: 1;
+ }
+
+ .setting-controls {
+ float: none;
+ }
+ }
+
+ .settings .setting {
+ padding: 0.5em;
+
+ .setting-label,
+ .setting-value {
+ float: none;
+ width: auto;
+ margin-right: 0;
+ }
+ }
}
diff --git a/plugins/discourse-ai/config/locales/client.en.yml b/plugins/discourse-ai/config/locales/client.en.yml
index f7b4d3acbe4..80895b1d0c6 100644
--- a/plugins/discourse-ai/config/locales/client.en.yml
+++ b/plugins/discourse-ai/config/locales/client.en.yml
@@ -327,20 +327,22 @@ en:
translations:
title: "Translations"
description: "Automatically translate all content on your forum to the user's preferred language"
+ supported_locales: "Supported languages"
+ supported_locales_description: "This site setting also allows visitors to contribute post translations."
admin_actions:
translation_settings: "Translation settings"
localization_settings: "Localization settings"
- disabled_state:
- configure: "Configure translations"
- empty_label: "Automatic translations are disabled, click below to configure"
+ enable_translations: "Enable automatic translations"
stats:
title: "Translation statistics"
complete_language_detection_description: "All %{total} eligible posts have their language detected."
incomplete_language_detection_description: "%{done} of %{total} eligible posts have their language detected."
- backfill_disabled: "Backfilling is currently disabled."
- description_tooltip: "Eligible posts are determined by AI translation settings"
+ backfill_disabled: "Backfilling is disabled, only new posts will be translated."
+ description_tooltip:
+ one: 'There is %{count} post eligible for translation.'
+ other: 'There are %{count} posts eligible for translation.'
progress_chart:
- title: "Translation Progress %{tooltip}"
+ title: "Translation Progress"
data_label: "%{percentage}%"
tooltip_translated: "%{done} out of %{total} posts translated"
bar_done:
diff --git a/plugins/discourse-ai/config/locales/server.en.yml b/plugins/discourse-ai/config/locales/server.en.yml
index da5c41abba8..93b50900019 100644
--- a/plugins/discourse-ai/config/locales/server.en.yml
+++ b/plugins/discourse-ai/config/locales/server.en.yml
@@ -141,7 +141,7 @@ en:
ai_discord_allowed_guilds: "Discord guilds (servers) where the bot is allowed to search"
ai_bot_enable_dedicated_ux: "Allow for full screen bot interface, instead of a PM"
- ai_translation_enabled: "Enables the AI translation feature"
+ ai_translation_enabled: "Enable the AI translation feature"
ai_translation_model: "The model to use for translation. This model must support translation. Personas can override this setting."
ai_translation_backfill_limit_to_public_content: "When enabled, only content in public categories will be translated. When disabled, content in group PMs and private categories will also be sent for translation."
ai_translation_max_post_length: "The maximum number of characters for a post to be translated. Posts longer than this will not be translated."
diff --git a/plugins/discourse-ai/spec/requests/admin/ai_translations_controller_spec.rb b/plugins/discourse-ai/spec/requests/admin/ai_translations_controller_spec.rb
index f11f634f420..2b586b45caf 100644
--- a/plugins/discourse-ai/spec/requests/admin/ai_translations_controller_spec.rb
+++ b/plugins/discourse-ai/spec/requests/admin/ai_translations_controller_spec.rb
@@ -19,6 +19,9 @@ describe DiscourseAi::Admin::AiTranslationsController do
end
it "returns translation progress data" do
+ SiteSetting.ai_translation_backfill_max_age_days = 30
+ SiteSetting.ai_translation_backfill_limit_to_public_content = false
+
Fabricate.times(14, :post, locale: "en")
Fabricate.times(1, :post, locale: "fr")
Fabricate.times(4, :post)
@@ -53,6 +56,27 @@ describe DiscourseAi::Admin::AiTranslationsController do
expect(json["translation_progress"]).to eq([])
expect(json["total"]).to eq(0)
expect(json["posts_with_detected_locale"]).to eq(0)
+ expect(json["no_locales_configured"]).to eq(true)
+ end
+
+ it "returns translation_enabled field" do
+ SiteSetting.ai_translation_backfill_max_age_days = 30
+
+ get "/admin/plugins/discourse-ai/ai-translations.json"
+
+ expect(response.status).to eq(200)
+ json = response.parsed_body
+
+ expect(json["translation_enabled"]).to eq(true)
+
+ SiteSetting.ai_translation_enabled = false
+
+ get "/admin/plugins/discourse-ai/ai-translations.json"
+
+ expect(response.status).to eq(200)
+ json = response.parsed_body
+
+ expect(json["translation_enabled"]).to eq(false)
end
it "correctly indicates if backfill is enabled" do
diff --git a/plugins/discourse-ai/spec/system/admin_ai_translations_spec.rb b/plugins/discourse-ai/spec/system/admin_ai_translations_spec.rb
index ca00d9ad079..d2074554e18 100644
--- a/plugins/discourse-ai/spec/system/admin_ai_translations_spec.rb
+++ b/plugins/discourse-ai/spec/system/admin_ai_translations_spec.rb
@@ -42,8 +42,8 @@ RSpec.describe "Admin AI translations", type: :system do
expect(page).to have_css(".ai-localization-settings-button")
# Verify chart container is present
- expect(page).to have_css(".ai-translation__charts")
- expect(page).to have_css(".ai-translation__chart")
+ expect(page).to have_css(".ai-translations__charts")
+ expect(page).to have_css(".ai-translations__chart")
end
it "navigates to translation settings when clicking the settings button" do
@@ -69,28 +69,98 @@ RSpec.describe "Admin AI translations", type: :system do
SiteSetting.discourse_ai_enabled = true
SiteSetting.ai_translation_enabled = false
SiteSetting.content_localization_supported_locales = "en|fr|es"
+ SiteSetting.ai_translation_backfill_max_age_days = 30
visit "/admin/plugins/discourse-ai/ai-translations"
end
- it "displays the disabled state message and configure button" do
- expect(page).to have_content(
- I18n.t("js.discourse_ai.translations.admin_actions.disabled_state.empty_label"),
- )
- expect(page).to have_css(".ai-translations__configure-button")
+ it "shows the toggle in off state and no chart" do
+ expect(page).to have_css(".d-toggle-switch")
- # Verify chart is NOT shown
- expect(page).to have_no_css(".ai-translation__chart")
+ expect(page).to have_no_css(".ai-translations__chart")
end
- it "navigates to translation settings when clicking the configure button" do
- translation_id = DiscourseAi::Configuration::Module::TRANSLATION_ID
- find(".ai-translations__configure-button").click
+ it "shows localization settings button" do
+ expect(page).to have_css(".ai-localization-settings-button")
+ end
+ end
- # Verify we navigated to the correct route
- expect(page).to have_current_path(
- "/admin/plugins/discourse-ai/ai-features/#{translation_id}/edit",
- )
+ describe "when locales are not configured" do
+ before do
+ SiteSetting.discourse_ai_enabled = true
+ SiteSetting.ai_translation_enabled = false
+ SiteSetting.content_localization_supported_locales = ""
+ SiteSetting.ai_translation_backfill_max_age_days = 30
+
+ visit "/admin/plugins/discourse-ai/ai-translations"
+ end
+
+ it "displays the alert with locale selector" do
+ expect(page).to have_css(".alert.alert-info")
+ expect(page).to have_content(I18n.t("js.discourse_ai.translations.supported_locales"))
+ expect(page).to have_css(".multi-select")
+ end
+
+ it "allows adding and saving languages" do
+ find(".multi-select").click
+
+ find(".select-kit-row[data-value='en']").click
+
+ expect(page).to have_css(".setting-controls__ok")
+
+ find(".setting-controls__ok").click
+
+ expect(page).to have_no_css(".setting-controls__ok")
+
+ expect(SiteSetting.content_localization_supported_locales).to eq("en")
+ end
+ end
+
+ describe "translation toggle" do
+ before do
+ SiteSetting.discourse_ai_enabled = true
+ SiteSetting.content_localization_supported_locales = "en|fr"
+ SiteSetting.ai_translation_backfill_max_age_days = 30
+
+ allow(DiscourseAi::Translation::PostCandidates).to receive(
+ :get_completion_all_locales,
+ ).and_return([{ done: 50, locale: "en", total: 100 }, { done: 50, locale: "fr", total: 100 }])
+ end
+
+ it "displays the translation toggle" do
+ SiteSetting.ai_translation_enabled = false
+
+ visit "/admin/plugins/discourse-ai/ai-translations"
+
+ expect(page).to have_css(".ai-translations__toggle-container .d-toggle-switch")
+ end
+
+ it "shows charts when translations are enabled" do
+ SiteSetting.ai_translation_enabled = true
+ SiteSetting.ai_translation_backfill_hourly_rate = 10
+
+ visit "/admin/plugins/discourse-ai/ai-translations"
+
+ expect(page).to have_css(".ai-translations__toggle-container")
+ expect(page).to have_css(".ai-translations__charts")
+ end
+
+ it "hides charts when translations are disabled" do
+ SiteSetting.ai_translation_enabled = false
+
+ visit "/admin/plugins/discourse-ai/ai-translations"
+
+ expect(page).to have_css(".ai-translations__toggle-container")
+ expect(page).to have_no_css(".ai-translations__charts")
+ end
+
+ it "keeps toggle disabled when no locales are configured" do
+ SiteSetting.ai_translation_enabled = false
+ SiteSetting.content_localization_supported_locales = ""
+
+ visit "/admin/plugins/discourse-ai/ai-translations"
+
+ expect(page).to have_css(".d-toggle-switch__checkbox[disabled]")
end
end
end