discourse/spec/system/content_localization_language_switcher_spec.rb
Natalie Tay 2daf45e5e5
FEATURE: Also allow language switcher to be shown when users are logged in (#34578)
This PR extends the current setting
`content_localization_anonymous_language_switcher` to one that can be
shown even for logged in users ->
`content_localization_language_switcher`

When logged in, the preference is saved to the `user` rather than a
cookie. 🍪



https://github.com/user-attachments/assets/b6f4d41b-4716-4020-a97d-2c3f90d54576
2025-08-27 23:01:07 +08:00

121 lines
3.9 KiB
Ruby
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 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 isnt 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