Skip to content

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

DEV: Update allowed supported locales in translation editor (#32920)

Keegan Georgeadc8dd29eb89
diff --git a/app/assets/javascripts/discourse/app/components/post-translation-editor.gjs b/app/assets/javascripts/discourse/app/components/post-translation-editor.gjs
index 6246211916f..e3dcd452695 100644
--- a/app/assets/javascripts/discourse/app/components/post-translation-editor.gjs
+++ b/app/assets/javascripts/discourse/app/components/post-translation-editor.gjs
@@ -13,7 +13,21 @@ export default class PostTranslationEditor extends Component {
@service siteSettings;
get availableLocales() {
- return JSON.parse(this.siteSettings.available_locales);
+ const allAvailableLocales = JSON.parse(this.siteSettings.available_locales);
+ const supportedLocales =
+ this.siteSettings.experimental_content_localization_supported_locales.split(
+ "|"
+ );
+
+ if (!supportedLocales.includes(this.siteSettings.default_locale)) {
+ supportedLocales.push(this.siteSettings.default_locale);
+ }
+
+ const filtered = allAvailableLocales.filter((locale) => {
+ return supportedLocales.includes(locale.value);
+ });
+
+ return filtered;
}
findCurrentLocalization() {
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index 33598998b5f..3184fc4d672 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -2774,6 +2774,7 @@ en:
experimental_content_localization: "Displays localized content for users based on their language preferences. Such content may include categories, tags, posts, and topics. This feature is under heavy development."
rich_editor: "Enable the rich editor so all users can switch between the current Markdown mode and the new rich text editor for more intuitive and user-friendly composition. The rich text editor is under active development, so not all features are supported yet — <a href='https://meta.discourse.org/t/test-our-new-composer/352347' target='_blank'>see Meta for more details</a>."
experimental_content_localization_allowed_groups: 'Groups allowed to update localized content. Requires "experimental content localization" to be enabled.'
+ experimental_content_localization_supported_locales: "List of supported locales for updating localized content"
viewport_based_mobile_mode: "EXPERIMENTAL: Disable the user-agent-based mobile/desktop modes and use viewport width instead."
errors:
invalid_css_color: "Invalid color. Enter a color name or hex value."
diff --git a/config/site_settings.yml b/config/site_settings.yml
index 78e371dab93..fca7919c5a4 100644
--- a/config/site_settings.yml
+++ b/config/site_settings.yml
@@ -4001,12 +4001,14 @@ experimental:
experimental_content_localization:
client: true
default: false
+ area: "localization"
experimental_content_localization_allowed_groups:
type: group_list
list_type: compact
allow_any: false
client: true
default: "1|2" # admin, moderator
+ area: "localization"
content_localization_debug_allowed_groups:
type: group_list
list_type: compact
@@ -4014,3 +4016,11 @@ experimental:
client: true
default: "1|2" # admin, moderator
hidden: true
+ experimental_content_localization_supported_locales:
+ default: ""
+ type: list
+ client: true
+ list_type: named
+ allow_any: false
+ choices: "LocaleSiteSetting.values"
+ area: "localization"
diff --git a/db/migrate/20250526164734_copy_translation_target_languages_to_content_localization_supported_locales.rb b/db/migrate/20250526164734_copy_translation_target_languages_to_content_localization_supported_locales.rb
new file mode 100644
index 00000000000..753c3f84ad7
--- /dev/null
+++ b/db/migrate/20250526164734_copy_translation_target_languages_to_content_localization_supported_locales.rb
@@ -0,0 +1,23 @@
+# frozen_string_literal: true
+
+class CopyTranslationTargetLanguagesToContentLocalizationSupportedLocales < ActiveRecord::Migration[
+ 7.2
+]
+ def up
+ translation_settings =
+ DB.query_single(
+ "SELECT value FROM site_settings WHERE name = 'automatic_translation_target_languages'",
+ )
+
+ if translation_settings.present?
+ DB.exec(
+ "UPDATE site_settings SET value = :value WHERE name = 'experimental_content_localization_supported_locales'",
+ value: translation_settings[0],
+ )
+ end
+ end
+
+ def down
+ raise ActiveRecord::IrreversibleMigration
+ end
+end
diff --git a/spec/system/post_translation_spec.rb b/spec/system/post_translation_spec.rb
index 4e3313ad125..36dd8a075c6 100644
--- a/spec/system/post_translation_spec.rb
+++ b/spec/system/post_translation_spec.rb
@@ -12,12 +12,34 @@ describe "Post translations", type: :system do
before do
sign_in(user)
+ SiteSetting.experimental_content_localization_supported_locales = "en|fr|es|pt_BR"
SiteSetting.experimental_content_localization = true
SiteSetting.experimental_content_localization_allowed_groups = Group::AUTO_GROUPS[:everyone]
SiteSetting.post_menu =
"read|like|copyLink|flag|edit|bookmark|delete|admin|reply|addTranslation"
end
+ it "should only show the languages listed in the site setting" do
+ topic_page.visit_topic(topic)
+ find("#post_#{post.post_number} .post-action-menu__add-translation").click
+ translation_selector.expand
+ expect(all(".translation-selector-dropdown .select-kit-collection li").count).to eq(4)
+ expect(translation_selector).to have_option_value("en")
+ expect(translation_selector).to have_option_value("fr")
+ expect(translation_selector).to have_option_value("es")
+ expect(translation_selector).to have_option_value("pt_BR")
+ expect(translation_selector).to have_no_option_value("de")
+ end
+
+ it "always includes the site's default locale in the list of available languages" do
+ SiteSetting.default_locale = "de"
+ topic_page.visit_topic(topic)
+ find("#post_#{post.post_number} .post-action-menu__add-translation").click
+ translation_selector.expand
+ expect(all(".translation-selector-dropdown .select-kit-collection li").count).to eq(5)
+ expect(translation_selector).to have_option_value("de")
+ end
+
it "allows a user to translate a post" do
topic_page.visit_topic(topic)
find("#post_#{post.post_number} .post-action-menu__add-translation").click