2
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-03-04 01:15:08 +08:00
discourse/spec/system/content_localization_language_switcher_spec.rb
Natalie Tay fda96d7351
DEV: Wrap specs in appropriate locale (#38056)
These specs cover untranslated i18n keys, but now with them translated
in https://github.com/discourse/discourse/pull/37917, we need to make
sure we wrap them in the appropriate locale.
2026-02-25 19:49:09 +08:00

200 lines
6.4 KiB
Ruby

# frozen_string_literal: true
describe "Content localization language switcher", type: :system do
SWITCHER_SELECTOR = "button[data-identifier='language-switcher']"
TOGGLE_LOCALIZE_BUTTON_SELECTOR = "button.btn-toggle-localized-content"
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!(: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")
select_language("ja")
expect(page.find(SWITCHER_SELECTOR)).to have_content("JA")
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")
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
it "resets localized content toggle after changing languages" do
SiteSetting.content_localization_language_switcher = "all"
visit("/t/#{topic.id}")
select_language("ja")
expect(topic_list).to have_content("孫子兵法からの人生戦略")
I18n.with_locale(:ja) do
expect(page.find(TOGGLE_LOCALIZE_BUTTON_SELECTOR)["title"]).to eq(
I18n.t("js.content_localization.toggle_localized.translated"),
)
end
page.find(TOGGLE_LOCALIZE_BUTTON_SELECTOR).click
expect(topic_list).to have_content("Life strategies from The Art of War")
I18n.with_locale(:ja) do
expect(page.find(TOGGLE_LOCALIZE_BUTTON_SELECTOR)["title"]).to eq(
I18n.t("js.content_localization.toggle_localized.not_translated"),
)
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(TOGGLE_LOCALIZE_BUTTON_SELECTOR)["title"]).to eq(
I18n.t("js.content_localization.toggle_localized.translated"),
)
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
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
def select_language(locale)
switcher.expand
switcher.option("[data-menu-option-id='#{locale}']").click
end
end