Skip to content

Commit 2fa47c2

Browse files
authored
DEV: Prevent multiple instances of same locale cookie due to path (#272)
When using the language switcher, the cookie gets added but the path is not specified, so we may have multiple of the same cookie with varying paths. This commit fixes the path to root so there won't be dups.
1 parent a90d737 commit 2fa47c2

File tree

2 files changed

+59
-14
lines changed

2 files changed

+59
-14
lines changed

assets/javascripts/discourse/components/language-switcher.gjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export default class LanguageSwitcher extends Component {
2828

2929
@action
3030
async changeLocale(locale) {
31-
cookie("locale", locale);
31+
cookie("locale", locale, { path: "/" });
3232
this.dMenu.close();
3333
// we need a hard refresh here for the locale to take effect
3434
window.location.reload();
Lines changed: 58 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,84 @@
11
# frozen_string_literal: true
22

33
RSpec.describe "Anonymous user language switcher", type: :system do
4-
fab!(:japanese_user) { Fabricate(:user, locale: "ja") }
4+
SWITCHER_SELECTOR = "button[data-identifier='discourse-translator_language-switcher']"
55

6-
it "shows the correct language based on the selected language and login status" do
7-
SWITCHER_SELECTOR = "button[data-identifier='discourse-translator_language-switcher']"
6+
let(:topic_page) { PageObjects::Pages::Topic.new }
7+
let(:switcher) { PageObjects::Components::DMenu.new(SWITCHER_SELECTOR) }
88

9-
visit("/")
10-
expect(page).not_to have_css(SWITCHER_SELECTOR)
9+
fab!(:japanese_user) { Fabricate(:user, locale: "ja") }
10+
fab!(:topic) do
11+
topic = Fabricate(:topic, title: "Life strategies from The Art of War")
12+
Fabricate(:post, topic:)
13+
topic
14+
end
1115

16+
before do
1217
SiteSetting.translator_enabled = true
1318
SiteSetting.allow_user_locale = true
1419
SiteSetting.set_locale_from_cookie = true
1520
SiteSetting.automatic_translation_backfill_rate = 1
21+
end
22+
23+
it "only shows the language switcher based on what is in target languages" do
1624
SiteSetting.automatic_translation_target_languages = "es|ja"
1725
SiteSetting.experimental_anon_language_switcher = true
1826
visit("/")
27+
1928
expect(page).to have_css(SWITCHER_SELECTOR)
20-
expect(find(".nav-item_latest")).to have_content("Latest")
2129

22-
switcher = PageObjects::Components::DMenu.new(SWITCHER_SELECTOR)
2330
switcher.expand
2431
expect(switcher).to have_content("日本語")
32+
expect(switcher).to have_content("Español")
2533

2634
SiteSetting.automatic_translation_target_languages = "es"
27-
SiteSetting.experimental_anon_language_switcher = true
2835
visit("/")
2936

3037
switcher.expand
3138
expect(switcher).not_to have_content("日本語")
32-
switcher.click_button("Español")
33-
expect(find(".nav-item_latest")).to have_content("Recientes")
39+
end
3440

35-
sign_in(japanese_user)
36-
visit("/")
37-
expect(find(".nav-item_latest")).to have_content("最新")
41+
describe "with Spanish and Japanese" do
42+
before do
43+
SiteSetting.automatic_translation_target_languages = "es|ja"
44+
SiteSetting.experimental_anon_language_switcher = true
45+
SiteSetting.experimental_inline_translation = true
46+
47+
topic.set_detected_locale("en")
48+
topic.set_translation("ja", "孫子兵法からの人生戦略")
49+
topic.set_translation("es", "Estrategias de vida de El arte de la guerra")
50+
end
51+
52+
it "shows the correct language based on the selected language and login status" do
53+
visit("/")
54+
expect(find(".nav-item_latest")).to have_content("Latest")
55+
56+
switcher.expand
57+
switcher.click_button("Español")
58+
expect(find(".nav-item_latest")).to have_content("Recientes")
59+
60+
sign_in(japanese_user)
61+
visit("/")
62+
expect(find(".nav-item_latest")).to have_content("最新")
63+
end
64+
65+
it "shows the most recently selected language" do
66+
visit("/")
67+
68+
switcher.expand
69+
switcher.click_button("Español")
70+
expect(find(".nav-item_latest")).to have_content("Recientes")
71+
72+
topic_page.visit_topic(topic)
73+
topic_page.has_topic_title?("Estrategias de vida de El arte de la guerra")
74+
75+
switcher.expand
76+
switcher.click_button("日本語")
77+
topic_page.visit_topic(topic)
78+
topic_page.has_topic_title?("孫子兵法からの人生戦略")
79+
80+
visit("/")
81+
expect(find(".nav-item_latest")).to have_content("最新")
82+
end
3883
end
3984
end

0 commit comments

Comments
 (0)