Skip to content

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

UX: Show users X of Y languages selected. (#42531)

Jordan Vidrinef4e1963581ef
diff --git a/config/site_settings.yml b/config/site_settings.yml
index aa678401d4c..4b57b783d17 100644
--- a/config/site_settings.yml
+++ b/config/site_settings.yml
@@ -1957,6 +1957,7 @@ content_localization:
area: "localization"
content_localization_max_locales:
default: 10
+ client: true
type: integer
hidden: true
content_localization_crawler_param:
diff --git a/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-translations.gjs b/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-translations.gjs
index 1a81f33f6f7..013a8043e09 100644
--- a/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-translations.gjs
+++ b/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-translations.gjs
@@ -6,7 +6,9 @@ import { action } from "@ember/object";
import { service } from "@ember/service";
import { trustHTML } from "@ember/template";
import moment from "moment";
+import PluginOutlet from "discourse/components/plugin-outlet";
import DTooltip from "discourse/float-kit/components/d-tooltip";
+import lazyHash from "discourse/helpers/lazy-hash";
import { ajax } from "discourse/lib/ajax";
import { popupAjaxError } from "discourse/lib/ajax-error";
import { bind } from "discourse/lib/decorators";
@@ -29,6 +31,7 @@ export default class AiTranslations extends Component {
@service aiCredits;
@service router;
@service siteSettings;
+ @service toasts;
@tracked overviewGeneration = 0;
@tracked expandedTargetType = null;
@@ -100,6 +103,18 @@ export default class AiTranslations extends Component {
return this.creditStatus?.hard_limit_reached === true;
}
+ get maxLocaleToast() {
+ const max = this.siteSettings.content_localization_max_locales;
+ return {
+ duration: "short",
+ data: {
+ message: i18n("discourse_ai.translations.max_locales_reached", {
+ max,
+ }),
+ },
+ };
+ }
+
get creditLimitWarningMessage() {
if (!this.creditLimitReached) {
return null;
@@ -206,6 +221,13 @@ export default class AiTranslations extends Component {
@action
updateSelectedLocales(locales) {
+ if (
+ this.siteSettings.content_localization_max_locales &&
+ locales.length > this.siteSettings.content_localization_max_locales
+ ) {
+ this.toasts.error(this.maxLocaleToast);
+ return;
+ }
this.selectedLocales = locales;
}
@@ -576,6 +598,25 @@ export default class AiTranslations extends Component {
}}</label>
</div>
<div class="setting-value">
+ {{#if this.siteSettings.content_localization_max_locales}}
+ <div class="ai-translations__locale-info">
+ <p class="ai-translations__locale-count">
+ {{i18n
+ "discourse_ai.translations.locale_count"
+ count=this.selectedLocales.length
+ max=this.siteSettings.content_localization_max_locales
+ }}
+ </p>
+ <PluginOutlet
+ @name="ai-translations-locale-info"
+ @connectorTagName="div"
+ @outletArgs={{lazyHash
+ localesCount=this.selectedLocales.length
+ maxLocales=this.siteSettings.content_localization_max_locales
+ }}
+ />
+ </div>
+ {{/if}}
<div class="ai-translations__locale-input-row">
<MultiSelect
@value={{this.selectedLocales}}
@@ -616,6 +657,9 @@ export default class AiTranslations extends Component {
<div class="setting">
<div class="setting-label">
<label>{{i18n "discourse_ai.translations.category_scope"}}</label>
+ <div class="desc ai-translations__category-scope-desc">{{i18n
+ "discourse_ai.translations.category_scope_description"
+ }}</div>
</div>
<div class="setting-value">
<div class="ai-translations__category-input-row">
@@ -689,9 +733,6 @@ export default class AiTranslations extends Component {
</div>
{{/if}}
</div>
- <div class="desc">{{i18n
- "discourse_ai.translations.category_scope_description"
- }}</div>
</div>
</div>
</div>
diff --git a/plugins/discourse-ai/assets/stylesheets/modules/translation/admin/translations.scss b/plugins/discourse-ai/assets/stylesheets/modules/translation/admin/translations.scss
index 0baa5b7cfbf..e0d5be8fac6 100644
--- a/plugins/discourse-ai/assets/stylesheets/modules/translation/admin/translations.scss
+++ b/plugins/discourse-ai/assets/stylesheets/modules/translation/admin/translations.scss
@@ -10,6 +10,10 @@
margin-block: var(--space-3);
}
+ &__language-switcher label {
+ font-weight: 600;
+ }
+
&__toggle-container {
margin-block-end: var(--space-3);
}
@@ -66,6 +70,26 @@
}
}
+ &__locale-info {
+ display: flex;
+ align-items: center;
+ gap: var(--space-2);
+ margin-block-end: var(--space-2);
+ }
+
+ &__locale-count {
+ margin: 0;
+ font-size: var(--font-down-1);
+ color: var(--primary-high);
+ }
+
+ &__category-scope-desc {
+ padding: 0;
+ margin-block-end: var(--space-2);
+ font-size: var(--font-down-1);
+ color: var(--primary-medium);
+ }
+
.settings .setting {
padding: var(--space-2);
diff --git a/plugins/discourse-ai/config/locales/client.en.yml b/plugins/discourse-ai/config/locales/client.en.yml
index c80fbe2099d..8208a09a91c 100644
--- a/plugins/discourse-ai/config/locales/client.en.yml
+++ b/plugins/discourse-ai/config/locales/client.en.yml
@@ -438,6 +438,8 @@ en:
title: "Translations"
description: "Automatically translate all content on your forum to the user's preferred language"
supported_locales: "Supported languages"
+ max_locales_reached: "You can only select up to %{max} languages."
+ locale_count: "Using %{count} of %{max} available languages."
category_scope: "Categories"
category_scope_description: "Choose whether automatic translations apply to content in all categories, public categories, or selected categories."
cached_results_notice: "Showing cached results from %{relative_time}."
diff --git a/plugins/discourse-ai/test/javascripts/integration/components/ai-translations-test.gjs b/plugins/discourse-ai/test/javascripts/integration/components/ai-translations-test.gjs
index 555379ad524..64d8f234305 100644
--- a/plugins/discourse-ai/test/javascripts/integration/components/ai-translations-test.gjs
+++ b/plugins/discourse-ai/test/javascripts/integration/components/ai-translations-test.gjs
@@ -3,6 +3,8 @@ import { click, find, render, settled, waitUntil } from "@ember/test-helpers";
import { module, test } from "qunit";
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
import pretender, { response } from "discourse/tests/helpers/create-pretender";
+import selectKit from "discourse/tests/helpers/select-kit-helper";
+import { i18n } from "discourse-i18n";
import AiTranslations from "discourse/plugins/discourse-ai/discourse/components/ai-translations";
class AiCreditsStub extends Service {
@@ -197,6 +199,65 @@ module("Integration | Component | AiTranslations", function (hooks) {
.isDisabled("the toggle stays disabled after saving no locales");
});
+ test("shows the locale count and blocks selections beyond the max", async function (assert) {
+ this.siteSettings.content_localization_max_locales = 2;
+ this.siteSettings.available_locales = [
+ { name: "English", value: "en" },
+ { name: "French", value: "fr" },
+ { name: "Spanish", value: "es" },
+ ];
+
+ await render(<template><AiTranslations @model={{this.model}} /></template>);
+
+ assert
+ .dom(".ai-translations__locale-count")
+ .hasText(
+ i18n("discourse_ai.translations.locale_count", { count: 2, max: 2 }),
+ "renders the locale usage count"
+ );
+
+ const locales = selectKit(
+ ".ai-translations__locale-input-row .multi-select"
+ );
+ await locales.expand();
+ await locales.selectRowByValue("es");
+
+ const toasts = this.owner.lookup("service:toasts");
+ assert.strictEqual(
+ toasts.activeToasts.length,
+ 1,
+ "selecting a locale beyond the max shows a toast"
+ );
+ assert.strictEqual(
+ toasts.activeToasts[0].options.data.message,
+ i18n("discourse_ai.translations.max_locales_reached", { max: 2 }),
+ "the toast explains the limit"
+ );
+
+ assert.strictEqual(
+ locales.header().value(),
+ "en,fr",
+ "the selection is unchanged"
+ );
+ assert
+ .dom(".ai-translations__locale-count")
+ .hasText(
+ i18n("discourse_ai.translations.locale_count", { count: 2, max: 2 }),
+ "the count is unchanged"
+ );
+ assert
+ .dom(".ai-translations__locale-input-row .setting-controls")
+ .doesNotExist("no save controls appear for the rejected selection");
+ });
+
+ test("hides the locale count when no max is configured", async function (assert) {
+ this.siteSettings.content_localization_max_locales = 0;
+
+ await render(<template><AiTranslations @model={{this.model}} /></template>);
+
+ assert.dom(".ai-translations__locale-info").doesNotExist();
+ });
+
test("enables the language switcher along with translations", async function (assert) {
let bulkUpdate;
this.model = {