0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-07 13:19:19 +08:00
discourse/spec/system/content_localization_language_switcher_spec.rb
Natalie Tay e1b9103c38
DEV: Remove per topic toggle (#40869)
With https://github.com/discourse/discourse/pull/40785, we now can
remove the per-topic toggle.

Users can still toggle showing full topic original content from their
user profile.
2026-06-15 18:43:36 +08:00

163 lines
5.3 KiB
Ruby
Vendored

# frozen_string_literal: true
describe "Content localization language switcher" do
let(:switcher_selector) { "button[data-identifier='language-switcher']" }
let(:topic_list) { PageObjects::Components::TopicList.new }
let(:switcher) { PageObjects::Components::DMenu.new(switcher_selector) }
let(:language_switcher) { PageObjects::Components::LanguageSwitcher.new }
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!(:post_2) do
Fabricate(
:post,
topic:,
locale: "en",
raw: "The greatest victory is that which requires no battle",
)
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 enabled state and what is in target languages" do
SiteSetting.content_localization_enabled = false
SiteSetting.content_localization_language_switcher = "anonymous"
visit("/")
expect(page).to have_no_css(switcher_selector)
SiteSetting.content_localization_enabled = true
page.refresh
switcher.expand
expect(switcher).to have_content("English")
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 "displays the current language code on the trigger button" do
SiteSetting.content_localization_language_switcher = "all"
visit("/")
expect(page.find(switcher_selector)).to have_content("EN")
language_switcher.select_language("ja")
expect(page.find(switcher_selector)).to have_content("JA")
language_switcher.select_language("es")
expect(page.find(switcher_selector)).to have_content("ES")
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")
language_switcher.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
language_switcher.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
it "marks the current language as selected in the dropdown" do
SiteSetting.content_localization_language_switcher = "all"
visit("/")
switcher.expand
expect(page).to have_css("[data-menu-option-id='en'].--selected")
expect(page).to have_no_css("[data-menu-option-id='ja'].--selected")
expect(page).to have_no_css("[data-menu-option-id='es'].--selected")
switcher.collapse
language_switcher.select_language("ja")
switcher.expand
expect(page).to have_css("[data-menu-option-id='ja'].--selected")
expect(page).to have_no_css("[data-menu-option-id='en'].--selected")
expect(page).to have_no_css("[data-menu-option-id='es'].--selected")
end
end