Skip to content

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

FEATURE: Also allow language switcher to be shown when users are logged in (#34578)

Natalie Tay2daf45e5e54a
diff --git a/app/assets/javascripts/discourse/app/components/header/icons.gjs b/app/assets/javascripts/discourse/app/components/header/icons.gjs
index d8c7e9b9341..53ca489db8e 100644
--- a/app/assets/javascripts/discourse/app/components/header/icons.gjs
+++ b/app/assets/javascripts/discourse/app/components/header/icons.gjs
@@ -68,11 +68,18 @@ export default class Icons extends Component {
}
get showLanguageSwitcher() {
- return (
- !this.currentUser &&
- this.siteSettings.content_localization_anon_language_switcher &&
- this.siteSettings.content_localization_supported_locales
- );
+ const has_locales =
+ !!this.siteSettings.content_localization_supported_locales;
+ switch (this.siteSettings.content_localization_language_switcher) {
+ case "none":
+ return false;
+ case "anonymous":
+ return !this.currentUser && has_locales;
+ case "all":
+ return has_locales;
+ default:
+ return false;
+ }
}
@action
diff --git a/app/assets/javascripts/discourse/app/components/language-switcher.gjs b/app/assets/javascripts/discourse/app/components/language-switcher.gjs
index 2f4ad56d14c..29ca0df26ee 100644
--- a/app/assets/javascripts/discourse/app/components/language-switcher.gjs
+++ b/app/assets/javascripts/discourse/app/components/language-switcher.gjs
@@ -10,10 +10,17 @@ import DMenu from "float-kit/components/d-menu";
export default class LanguageSwitcher extends Component {
@service siteSettings;
@service languageNameLookup;
+ @service currentUser;
@action
async changeLocale(locale) {
- cookie("locale", locale, { path: "/" });
+ if (this.currentUser) {
+ this.currentUser.set("locale", locale);
+ await this.currentUser.save(["locale"]);
+ } else {
+ cookie("locale", locale, { path: "/" });
+ }
+
this.dMenu.close();
// content should switch immediately,
// but we need a hard refresh here for controls to switch to the new locale
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index ee30fa0ca71..9bca8e9cbaf 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -2764,7 +2764,7 @@ en:
content_localization_enabled: "Displays localized content for users based on their browser or user language preferences. Such content may include categories, tags, posts, and topics. Supported locales are set in 'content localization supported locales'."
content_localization_supported_locales: "List of supported locales that user content can be translated to. Requires 'content localization enabled'."
content_localization_allowed_groups: "Groups allowed to update localized content. Requires 'content localization enabled'."
- content_localization_anon_language_switcher: "Enable language switcher which allows site visitors who are not logged in to switch between translated versions of Discourse and user-contributed content. Depends on the list of languages defined in 'content localization supported locales'"
+ content_localization_language_switcher: "Show a language switcher in the header, allowing visitors to switch between translated versions of Discourse and user-contributed content. Uses languages defined in 'content localization supported locales'"
errors:
invalid_css_color: "Invalid color. Enter a color name or hex value."
@@ -2837,7 +2837,7 @@ en:
invalid_uncategorized_category_setting: 'The "Uncategorized" category cannot be selected if ''allow uncategorized topics'' is not enabled.'
invalid_search_ranking_weights: "Value is invalid for search_ranking_weights site setting. Example: '{0.1,0.2,0.3,1.0}'. Note that maximum value for each weight is 1.0."
content_localization_locale_limit: "The number of supported locales cannot exceed %{max}."
- content_localization_anon_language_switcher_requirements: "The language switcher requires the `set locale from cookie` site setting to be enabled, and the `content localization supported locales` to have at least one language."
+ content_localization_language_switcher_requirements: "The language switcher requires the 'set locale from cookie' and 'allow user locale' site settings to be enabled, and 'content localization supported locales' to have at least one language."
mediaconvert_role_arn_required: "AWS IAM role ARN is required when using AWS MediaConvert for video conversion."
s3_credentials_required_for_video_conversion: "S3 credentials are required for video conversion. Please configure S3 access keys or enable IAM profile."
diff --git a/config/site_settings.yml b/config/site_settings.yml
index efc53beb70e..a4d50eba03a 100644
--- a/config/site_settings.yml
+++ b/config/site_settings.yml
@@ -1654,8 +1654,13 @@ content_localization:
default: 10
type: integer
hidden: true
- content_localization_anon_language_switcher:
- default: false
+ content_localization_language_switcher:
+ type: enum
+ choices:
+ - none
+ - anonymous
+ - all
+ default: none
client: true
validator: "LanguageSwitcherSettingValidator"
content_localization_crawler_param:
diff --git a/db/migrate/20250827065400_migrate_content_localization_anon_language_switcher_to_enum.rb b/db/migrate/20250827065400_migrate_content_localization_anon_language_switcher_to_enum.rb
new file mode 100644
index 00000000000..8f1f553112a
--- /dev/null
+++ b/db/migrate/20250827065400_migrate_content_localization_anon_language_switcher_to_enum.rb
@@ -0,0 +1,30 @@
+# frozen_string_literal: true
+
+class MigrateContentLocalizationAnonLanguageSwitcherToEnum < ActiveRecord::Migration[7.1]
+ def up
+ from_value = "content_localization_anon_language_switcher"
+ to_value = "content_localization_language_switcher"
+
+ if DB.query_single("SELECT 1 FROM site_settings WHERE name = '#{from_value}'").first
+ execute <<~SQL
+ UPDATE site_settings
+ SET name = '#{to_value}',
+ value = CASE
+ WHEN value = 'f' THEN 'none'
+ WHEN value = 't' THEN 'anonymous'
+ ELSE 'none'
+ END,
+ data_type = 7
+ WHERE name = '#{from_value}'
+ AND NOT EXISTS (
+ SELECT 1 FROM site_settings
+ WHERE name = '#{to_value}'
+ )
+ SQL
+ end
+ end
+
+ def down
+ raise ActiveRecord::IrreversibleMigration
+ end
+end
diff --git a/lib/validators/language_switcher_setting_validator.rb b/lib/validators/language_switcher_setting_validator.rb
index 2e6dbb42225..9ac4410f2cf 100644
--- a/lib/validators/language_switcher_setting_validator.rb
+++ b/lib/validators/language_switcher_setting_validator.rb
@@ -6,12 +6,12 @@ class LanguageSwitcherSettingValidator
end
def valid_value?(val)
- return true if val == "f" || val == "false"
- SiteSetting.set_locale_from_cookie &&
+ return true if val == "none"
+ SiteSetting.set_locale_from_cookie && SiteSetting.allow_user_locale &&
SiteSetting.content_localization_supported_locales.present?
end
def error_message
- I18n.t("site_settings.errors.content_localization_anon_language_switcher_requirements")
+ I18n.t("site_settings.errors.content_localization_language_switcher_requirements")
end
end
diff --git a/spec/system/anon_language_switcher_spec.rb b/spec/system/anon_language_switcher_spec.rb
deleted file mode 100644
index b4e28e1cbb1..00000000000
--- a/spec/system/anon_language_switcher_spec.rb
+++ /dev/null
@@ -1,65 +0,0 @@
-# frozen_string_literal: true
-
-RSpec.describe "Anonymous user language switcher", type: :system do
- SWITCHER_SELECTOR = "button[data-identifier='language-switcher']"
-
- let(:topic_list) { PageObjects::Components::TopicList.new }
- let(:switcher) { PageObjects::Components::DMenu.new(SWITCHER_SELECTOR) }
-
- fab!(:japanese_user) { Fabricate(:user, locale: "ja") }
-
- fab!(:topic) { Fabricate(:topic, title: "Life strategies from The Art of War", locale: "en") }
- fab!(:post_1) do
- Fabricate(
- :post,
- topic:,
- locale: "en",
- raw: "The masterpiece isn’t just about military strategy",
- )
- end
-
- fab!(:topic_localization) do
- Fabricate(:topic_localization, topic:, locale: "ja", fancy_title: "孫子兵法からの人生戦略")
- end
-
- fab!(:post_localization) do
- Fabricate(:post_localization, post: post_1, locale: "ja", cooked: "傑作は単なる軍事戦略についてではありません")
- end
-
- before do
- SiteSetting.default_locale = "en"
- SiteSetting.content_localization_supported_locales = "es|ja"
- SiteSetting.content_localization_enabled = true
- SiteSetting.allow_user_locale = true
- SiteSetting.set_locale_from_cookie = true
- end
-
- it "only shows the language switcher based on what is in target languages" do
- SiteSetting.content_localization_anon_language_switcher = false
- visit("/")
-
- expect(page).not_to have_css(SWITCHER_SELECTOR)
-
- SiteSetting.content_localization_anon_language_switcher = true
- visit("/")
-
- switcher.expand
- expect(switcher).to have_content("English (US)")
- expect(switcher).to have_content("Japanese (日本語)")
- expect(switcher).to have_content("Spanish (Español)")
-
- SiteSetting.content_localization_supported_locales = "ja"
- visit("/")
-
- switcher.expand
- expect(switcher).not_to have_content("Español")
- switcher.option("[data-menu-option-id='ja']").click
- expect(topic_list).to have_content("孫子兵法からの人生戦略")
- I18n.with_locale("ja") do
- expect(page.find("#navigation-bar")).to have_content(I18n.t("js.filters.latest.title"))
- end
-
- sign_in(japanese_user)
- expect(page).not_to have_css(SWITCHER_SELECTOR)
- end
-end
diff --git a/spec/system/content_localization_language_switcher_spec.rb b/spec/system/content_localization_language_switcher_spec.rb
new file mode 100644
index 00000000000..47d0910a494
--- /dev/null
+++ b/spec/system/content_localization_language_switcher_spec.rb
@@ -0,0 +1,121 @@
+# frozen_string_literal: true
+
+describe "Content localization language switcher", type: :system do
+ SWITCHER_SELECTOR = "button[data-identifier='language-switcher']"
+
+ let(:topic_list) { PageObjects::Components::TopicList.new }
+ let(:switcher) { PageObjects::Components::DMenu.new(SWITCHER_SELECTOR) }
+
+ fab!(:japanese_user) { Fabricate(:user, locale: "ja") }
+
+ fab!(:topic) { Fabricate(:topic, title: "Life strategies from The Art of War", locale: "en") }
+ fab!(:post_1) do
+ Fabricate(
+ :post,
+ topic:,
+ locale: "en",
+ raw: "The masterpiece isn’t just about military strategy",
+ )
+ end
+
+ before do
+ SiteSetting.default_locale = "en"
+ SiteSetting.content_localization_supported_locales = "es|ja"
+ SiteSetting.content_localization_enabled = true
+ SiteSetting.allow_user_locale = true
+ SiteSetting.set_locale_from_cookie = true
+
+ Fabricate(:topic_localization, topic:, locale: "ja", fancy_title: "孫子兵法からの人生戦略")
+ Fabricate(
+ :topic_localization,
+ topic:,
+ locale: "es",
+ fancy_title: "Estrategias de vida de El arte de la guerra",
+ )
+ Fabricate(:post_localization, post: post_1, locale: "ja", cooked: "傑作は単なる軍事戦略についてではありません")
+ Fabricate(
+ :post_localization,
+ post: post_1,
+ locale: "es",
+ cooked: "La obra maestra no se trata solo de estrategia militar",
+ )
+ end
+
+ it "only shows the language switcher based on what is in target languages" do
+ SiteSetting.content_localization_language_switcher = "anonymous"
+ visit("/")
+
+ switcher.expand
+ expect(switcher).to have_content("English (US)")
+ expect(switcher).to have_content("Japanese (日本語)")
+ expect(switcher).to have_content("Spanish (Español)")
+
+ SiteSetting.content_localization_supported_locales = "ja"
+ visit("/")
+
+ switcher.expand
+ expect(switcher).not_to have_content("Español")
+ end
+
+ it "only shows the language switcher if turned on for various types of users (anon, logged in)" do
+ SiteSetting.content_localization_language_switcher = "none"
+ visit("/")
+ expect(page).not_to have_css(SWITCHER_SELECTOR)
+
+ SiteSetting.content_localization_language_switcher = "anonymous"
+ visit("/")
+ expect(page).to have_css(SWITCHER_SELECTOR)
+
+ SiteSetting.content_localization_language_switcher = "all"
+ visit("/")
+ expect(page).to have_css(SWITCHER_SELECTOR)
+
+ sign_in(japanese_user)
+
+ SiteSetting.content_localization_language_switcher = "none"
+ visit("/")
+ expect(page).not_to have_css(SWITCHER_SELECTOR)
+
+ SiteSetting.content_localization_language_switcher = "anonymous"
+ visit("/")
+ expect(page).not_to have_css(SWITCHER_SELECTOR)
+
+ SiteSetting.content_localization_language_switcher = "all"
+ visit("/")
+ expect(page).to have_css(SWITCHER_SELECTOR)
+ end
+
+ it "shows localized content when switching languages (anon, logged in)" do
+ SiteSetting.content_localization_language_switcher = "all"
+
+ visit("/")
+ expect(topic_list).to have_content("Life strategies from The Art of War")
+
+ select_language("es")
+
+ expect(topic_list).to have_content("Estrategias de vida de El arte de la guerra")
+ I18n.with_locale("es") do
+ expect(page.find("#navigation-bar")).to have_content(I18n.t("js.filters.latest.title"))
+ end
+
+ sign_in(japanese_user)
+
+ visit("/")
+ expect(topic_list).to have_content("孫子兵法からの人生戦略")
+ I18n.with_locale("ja") do
+ expect(page.find("#navigation-bar")).to have_content(I18n.t("js.filters.latest.title"))
+ end
+
+ select_language("es")
+
+ expect(topic_list).to have_content("Estrategias de vida de El arte de la guerra")
+ I18n.with_locale("es") do
+ expect(page.find("#navigation-bar")).to have_content(I18n.t("js.filters.latest.title"))
+ end
+ end
+
+ def select_language(locale)
+ switcher.expand
+ switcher.option("[data-menu-option-id='#{locale}']").click
+ end
+end