From 3f6f97eab29ed6abe83618240950a787af74ef9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ladislav=20Slez=C3=A1k?= Date: Tue, 26 Mar 2024 12:49:24 +0100 Subject: [PATCH] More consistent language cookie name --- web/src/App.test.jsx | 4 ++-- web/src/context/installerL10n.jsx | 6 +++--- web/src/context/installerL10n.test.jsx | 16 ++++++++-------- web/src/lib/webpack-po-handler.js | 2 +- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/web/src/App.test.jsx b/web/src/App.test.jsx index 1ab810fc3c..bd0a963959 100644 --- a/web/src/App.test.jsx +++ b/web/src/App.test.jsx @@ -61,7 +61,7 @@ const changePhaseTo = phase => act(() => callbacks.onPhaseChange(phase)); describe("App", () => { beforeEach(() => { // setting the language through a cookie - document.cookie = "AgamaLang=en-us; path=/;"; + document.cookie = "agamaLang=en-us; path=/;"; createClient.mockImplementation(() => { return { manager: { @@ -94,7 +94,7 @@ describe("App", () => { afterEach(() => { // setting a cookie with already expired date removes it - document.cookie = "AgamaLang=; path=/; expires=" + new Date(0).toUTCString(); + document.cookie = "agamaLang=; path=/; expires=" + new Date(0).toUTCString(); }); describe("when the software context is not initialized", () => { diff --git a/web/src/context/installerL10n.jsx b/web/src/context/installerL10n.jsx index 4f19e1c433..d73353d7ee 100644 --- a/web/src/context/installerL10n.jsx +++ b/web/src/context/installerL10n.jsx @@ -52,14 +52,14 @@ function useInstallerL10n() { /** * Current language (in xx_XX format). * - * It takes the language from the AgamaLang cookie. + * It takes the language from the agamaLang cookie. * * @return {string|undefined} Undefined if language is not set. */ function agamaLanguage() { // language from cookie, empty string if not set (regexp taken from Cockpit) // https://github.com/cockpit-project/cockpit/blob/98a2e093c42ea8cd2431cf15c7ca0e44bb4ce3f1/pkg/shell/shell-modals.jsx#L91 - const languageString = decodeURIComponent(document.cookie.replace(/(?:(?:^|.*;\s*)AgamaLang\s*=\s*([^;]*).*$)|^.*$/, "$1")); + const languageString = decodeURIComponent(document.cookie.replace(/(?:(?:^|.*;\s*)agamaLang\s*=\s*([^;]*).*$)|^.*$/, "$1")); if (languageString) { return languageString.toLowerCase(); } @@ -78,7 +78,7 @@ function storeAgamaLanguage(language) { if (current === language) return false; // Code taken from Cockpit. - const cookie = "AgamaLang=" + encodeURIComponent(language) + "; path=/; expires=Sun, 16 Jul 3567 06:23:41 GMT"; + const cookie = "agamaLang=" + encodeURIComponent(language) + "; path=/; expires=Sun, 16 Jul 3567 06:23:41 GMT"; document.cookie = cookie; // for backward compatibility, CockpitLang cookie is needed to load correct po.js content from Cockpit diff --git a/web/src/context/installerL10n.test.jsx b/web/src/context/installerL10n.test.jsx index c604977054..bcf703a51d 100644 --- a/web/src/context/installerL10n.test.jsx +++ b/web/src/context/installerL10n.test.jsx @@ -56,7 +56,7 @@ jest.mock("~/lib/cockpit", () => ({ })); // Helper component that displays a translated message depending on the -// AgamaLang value. +// agamaLang value. const TranslatedContent = () => { const text = { "cs-cz": "ahoj", @@ -65,7 +65,7 @@ const TranslatedContent = () => { "es-ar": "hola!", }; - const regexp = /AgamaLang=([^;]+)/; + const regexp = /agamaLang=([^;]+)/; const found = document.cookie.match(regexp); if (!found) return <>{text["en-us"]}; @@ -85,7 +85,7 @@ describe("InstallerL10nProvider", () => { // remove the Cockpit language cookie after each test afterEach(() => { // setting a cookie with already expired date removes it - document.cookie = "AgamaLang=; path=/; expires=" + new Date(0).toUTCString(); + document.cookie = "agamaLang=; path=/; expires=" + new Date(0).toUTCString(); }); describe("when no URL query parameter is set", () => { @@ -95,7 +95,7 @@ describe("InstallerL10nProvider", () => { describe("when the Cockpit language is already set", () => { beforeEach(() => { - document.cookie = "AgamaLang=en-us; path=/;"; + document.cookie = "agamaLang=en-us; path=/;"; getUILocaleFn.mockResolvedValueOnce("en_US.UTF-8"); }); @@ -115,7 +115,7 @@ describe("InstallerL10nProvider", () => { describe("when the Cockpit language is set to an unsupported language", () => { beforeEach(() => { - document.cookie = "AgamaLang=de-de; path=/;"; + document.cookie = "agamaLang=de-de; path=/;"; getUILocaleFn.mockResolvedValueOnce("de_DE.UTF-8"); getUILocaleFn.mockResolvedValueOnce("es_ES.UTF-8"); }); @@ -200,7 +200,7 @@ describe("InstallerL10nProvider", () => { describe("when the Cockpit language is already set to 'cs-cz'", () => { beforeEach(() => { - document.cookie = "AgamaLang=cs-cz; path=/;"; + document.cookie = "agamaLang=cs-cz; path=/;"; getUILocaleFn.mockResolvedValueOnce("cs_CZ.UTF-8"); }); @@ -215,7 +215,7 @@ describe("InstallerL10nProvider", () => { await screen.findByText("ahoj"); expect(setUILocaleFn).not.toHaveBeenCalled(); - expect(document.cookie).toMatch(/AgamaLang=cs-cz/); + expect(document.cookie).toMatch(/agamaLang=cs-cz/); expect(utils.locationReload).not.toHaveBeenCalled(); expect(utils.setLocationSearch).not.toHaveBeenCalled(); }); @@ -223,7 +223,7 @@ describe("InstallerL10nProvider", () => { describe("when the Cockpit language is set to 'en-us'", () => { beforeEach(() => { - document.cookie = "AgamaLang=en-us; path=/;"; + document.cookie = "agamaLang=en-us; path=/;"; getUILocaleFn.mockResolvedValueOnce("en_US"); getUILocaleFn.mockResolvedValueOnce("cs_CZ"); setUILocaleFn.mockResolvedValue(); diff --git a/web/src/lib/webpack-po-handler.js b/web/src/lib/webpack-po-handler.js index ead749c899..9d9a51a38b 100644 --- a/web/src/lib/webpack-po-handler.js +++ b/web/src/lib/webpack-po-handler.js @@ -11,7 +11,7 @@ const path = require("path"); // @param res HTTP response module.exports = function (req, res) { // the regexp was taken from the original Cockpit code :-) - const language = req.headers.cookie.replace(/(?:(?:^|.*;\s*)AgamaLang\s*=\s*([^;]*).*$)|^.*$/, "$1") || ""; + const language = req.headers.cookie.replace(/(?:(?:^|.*;\s*)agamaLang\s*=\s*([^;]*).*$)|^.*$/, "$1") || ""; // the cookie uses "pt-br" format while the PO file is "pt_BR" :-/ let [lang, country] = language.split("-"); country = country?.toUpperCase();