Skip to content

Commit

Permalink
More consistent language cookie name
Browse files Browse the repository at this point in the history
  • Loading branch information
lslezak committed Mar 26, 2024
1 parent 950e927 commit 3f6f97e
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 14 deletions.
4 changes: 2 additions & 2 deletions web/src/App.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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", () => {
Expand Down
6 changes: 3 additions & 3 deletions web/src/context/installerL10n.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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
Expand Down
16 changes: 8 additions & 8 deletions web/src/context/installerL10n.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"]}</>;

Expand All @@ -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", () => {
Expand All @@ -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");
});

Expand All @@ -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");
});
Expand Down Expand Up @@ -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");
});

Expand All @@ -215,15 +215,15 @@ 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();
});
});

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();
Expand Down
2 changes: 1 addition & 1 deletion web/src/lib/webpack-po-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 3f6f97e

Please sign in to comment.