Skip to content

Commit

Permalink
fix: use cookie to read locale
Browse files Browse the repository at this point in the history
  • Loading branch information
alexgoff committed Aug 29, 2024
1 parent 8377428 commit 72fafd9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
8 changes: 5 additions & 3 deletions components/global/Header/LanguageSelect/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import PropTypes from "prop-types";
import { useEffect, useState } from "react";
import { useRouter, useSearchParams, usePathname } from "next/navigation";
import { useTranslation } from "react-i18next";
import { getCookie } from "cookies-next";
import { cookieName, fallbackLng } from "@/lib/i18n/settings";
import * as Styled from "./styles";
import { fallbackLng } from "@/lib/i18n/settings";

const filterSearchParams = (searchParams) => {
const filteredParams = [];
Expand All @@ -24,8 +25,9 @@ export default function LanguageSelect({ id }) {
const searchParams = useSearchParams();
const {
t,
i18n: { changeLanguage, resolvedLanguage: locale },
i18n: { changeLanguage },
} = useTranslation();
const locale = getCookie(cookieName);

const isDefaultLocale = fallbackLng.includes(locale);

Expand All @@ -47,9 +49,9 @@ export default function LanguageSelect({ id }) {
const route = `/${newLocale}/${parts.join("/")}${filterSearchParams(
searchParams
)}`;
changeLanguage(newLocale);
setLoading(true);
router.replace(route, { scroll: false });
changeLanguage(newLocale);
}
};

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"add": "^2.0.6",
"classnames": "^2.3.1",
"convert": "^4.13.0",
"cookies-next": "^4.2.1",
"feed": "^4.2.2",
"focus-trap": "^7.0.0",
"focus-visible": "^5.1.0",
Expand Down
18 changes: 18 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3340,6 +3340,11 @@
dependencies:
"@types/node" "*"

"@types/cookie@^0.6.0":
version "0.6.0"
resolved "https://registry.yarnpkg.com/@types/cookie/-/cookie-0.6.0.tgz#eac397f28bf1d6ae0ae081363eca2f425bedf0d5"
integrity sha512-4Kh9a6B2bQciAhf7FSuMRRkUWecJgJu9nPnx3yzpsfXX/c50REIqpHY4C82bXP90qrLtXtkDxTZosYO3UpOwlA==

"@types/detect-port@^1.3.0":
version "1.3.2"
resolved "https://registry.yarnpkg.com/@types/detect-port/-/detect-port-1.3.2.tgz#8c06a975e472803b931ee73740aeebd0a2eb27ae"
Expand Down Expand Up @@ -4995,6 +5000,19 @@ [email protected]:
resolved "https://registry.yarnpkg.com/cookie/-/cookie-0.5.0.tgz#d1f5d71adec6558c58f389987c366aa47e994f8b"
integrity sha512-YZ3GUyn/o8gfKJlnlX7g7xq4gyO6OSuhGPKaaGssGB2qgDUS0gPgtTvoyZLTt9Ab6dC4hfc9dV5arkvc/OCmrw==

cookie@^0.6.0:
version "0.6.0"
resolved "https://registry.yarnpkg.com/cookie/-/cookie-0.6.0.tgz#2798b04b071b0ecbff0dbb62a505a8efa4e19051"
integrity sha512-U71cyTamuh1CRNCfpGY6to28lxvNwPG4Guz/EVjgf3Jmzv0vlDp1atT9eS5dDjMYHucpHbWns6Lwf3BKz6svdw==

cookies-next@^4.2.1:
version "4.2.1"
resolved "https://registry.yarnpkg.com/cookies-next/-/cookies-next-4.2.1.tgz#a0c2942afee16f1ffc2bc05a003c7c0cf32deda5"
integrity sha512-qsjtZ8TLlxCSX2JphMQNhkm3V3zIMQ05WrLkBKBwu50npBbBfiZWIdmSMzBGcdGKfMK19E0PIitTfRFAdMGHXg==
dependencies:
"@types/cookie" "^0.6.0"
cookie "^0.6.0"

core-js-compat@^3.25.1:
version "3.27.1"
resolved "https://registry.yarnpkg.com/core-js-compat/-/core-js-compat-3.27.1.tgz#b5695eb25c602d72b1d30cbfba3cb7e5e4cf0a67"
Expand Down

0 comments on commit 72fafd9

Please sign in to comment.