Skip to content

A one-of-a-kind resume builder that keeps your privacy in mind. Completely secure, customizable, portable, open-source and free forever. Try it out today!

fix: honor explicit language selection on localized URLs (#3713)

Amruth Pillaifbef87bea51c
diff --git a/apps/web/src/libs/locale.test.ts b/apps/web/src/libs/locale.test.ts
new file mode 100644
index 000000000..8311c8c96
--- /dev/null
+++ b/apps/web/src/libs/locale.test.ts
@@ -0,0 +1,63 @@
+// @vitest-environment happy-dom
+// @vitest-environment-options {"url":"https://localhost:3000"}
+
+import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
+import Cookies from "js-cookie";
+import { changeLocale, getLocale } from "./locale";
+
+beforeEach(() => {
+ Cookies.remove("locale");
+ vi.spyOn(window.location, "reload").mockImplementation(() => {});
+});
+
+afterEach(() => {
+ Cookies.remove("locale");
+ vi.restoreAllMocks();
+});
+
+describe("changeLocale", () => {
+ it.each([
+ ["en-US", "de-DE", "?filter=modern&locale=de-DE&tag=one&tag=two"],
+ ["de-DE", "en-US", "?filter=modern&tag=one&tag=two"],
+ ])("replaces %s with %s and persists the choice", (initial, selected, search) => {
+ const state = { key: "current-page" };
+ window.history.replaceState(state, "", `/resume-templates?filter=modern&locale=${initial}&tag=one&tag=two#gallery`);
+ expect(getLocale()).toBe(initial);
+
+ changeLocale(selected);
+
+ expect(window.location.pathname).toBe("/resume-templates");
+ expect(window.location.search).toBe(search);
+ expect(window.location.hash).toBe("#gallery");
+ expect(window.history.state).toEqual(state);
+ expect(window.location.reload).toHaveBeenCalledOnce();
+ expect(Cookies.get("locale")).toBe(selected);
+ expect(getLocale()).toBe(selected);
+
+ window.history.pushState(null, "", "/dashboard");
+ expect(getLocale()).toBe(selected);
+ });
+
+ it("reloads even when the selected locale has the same URL", () => {
+ window.history.replaceState(null, "", "/resume-templates#gallery");
+ Cookies.set("locale", "de-DE");
+
+ changeLocale("en-US");
+
+ expect(window.location.href).toBe("https://localhost:3000/resume-templates#gallery");
+ expect(window.location.reload).toHaveBeenCalledOnce();
+ expect(getLocale()).toBe("en-US");
+ });
+
+ it.each([null, "", "invalid"])("ignores invalid selection %s", (value) => {
+ window.history.replaceState(null, "", "/resume-templates?locale=de-DE#gallery");
+ Cookies.set("locale", "de-DE");
+ const href = window.location.href;
+
+ changeLocale(value);
+
+ expect(window.location.href).toBe(href);
+ expect(Cookies.get("locale")).toBe("de-DE");
+ expect(window.location.reload).not.toHaveBeenCalled();
+ });
+});
diff --git a/apps/web/src/libs/locale.ts b/apps/web/src/libs/locale.ts
index 2cf055bb2..da11d1d61 100644
--- a/apps/web/src/libs/locale.ts
+++ b/apps/web/src/libs/locale.ts
@@ -3,7 +3,7 @@ import type { Locale } from "@reactive-resume/utils/locale";
import { i18n } from "@lingui/core";
import { msg } from "@lingui/core/macro";
import Cookies from "js-cookie";
-import { defaultLocale, isLocale } from "@reactive-resume/utils/locale";
+import { defaultLocale, isLocale, localizedUrl } from "@reactive-resume/utils/locale";
const storageKey = "locale";
const messageLoaders = import.meta.glob<{ messages: Messages }>("../../locales/*.po");
@@ -148,5 +148,6 @@ export const loadLocale = async (locale: string) => {
export const changeLocale = (value: string | null) => {
if (!value || !isLocale(value)) return;
Cookies.set(storageKey, value);
+ window.history.replaceState(window.history.state, "", localizedUrl(window.location.href, value));
window.location.reload();
};