From 37ce14f16c49dbe7a49fde7c2b73318775a644c2 Mon Sep 17 00:00:00 2001 From: myl7 Date: Sat, 12 Feb 2022 22:25:55 +0800 Subject: [PATCH] remember language preference --- components/SwitchLang.tsx | 13 +++++++++++-- package.json | 1 + pnpm-lock.yaml | 29 +++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/components/SwitchLang.tsx b/components/SwitchLang.tsx index 8d40dd1..2f88daa 100644 --- a/components/SwitchLang.tsx +++ b/components/SwitchLang.tsx @@ -4,6 +4,7 @@ import { Menu, Transition } from '@headlessui/react' import { useRouter } from 'next/router' import Link from 'next/link' +import { useCookies, withCookies } from 'react-cookie' // https://headlessui.dev/react/menu#integrating-with-next-js const CustomLink = ({ href, children, as, locale, ...props }): JSX.Element => { @@ -28,6 +29,8 @@ const localeText = (locale: string): string => { const SwitchLang = () => { const { locales, pathname, query, asPath } = useRouter() + const [_, setCookie] = useCookies(['NEXT_LOCALE']) + return (
@@ -48,7 +51,13 @@ const SwitchLang = () => { {locales!.map(locale => ( - + setCookie('NEXT_LOCALE', locale, { path: '/' })} + >
{localeText(locale)}
@@ -62,4 +71,4 @@ const SwitchLang = () => { ) } -export default SwitchLang +export default withCookies(SwitchLang) diff --git a/package.json b/package.json index 08c4c2f..6182151 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,7 @@ "react": "^17.0.2", "react-async-hook": "^4.0.0", "react-audio-player": "^0.17.0", + "react-cookie": "^4.1.1", "react-copy-to-clipboard": "^5.0.3", "react-dom": "^17.0.2", "react-dplayer": "^0.4.2", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 5c9831a..974fe60 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -42,6 +42,7 @@ specifiers: react: ^17.0.2 react-async-hook: ^4.0.0 react-audio-player: ^0.17.0 + react-cookie: ^4.1.1 react-copy-to-clipboard: ^5.0.3 react-dom: ^17.0.2 react-dplayer: ^0.4.2 @@ -86,6 +87,7 @@ dependencies: react: 17.0.2 react-async-hook: 4.0.0_react@17.0.2 react-audio-player: 0.17.0_react-dom@17.0.2+react@17.0.2 + react-cookie: 4.1.1_react@17.0.2 react-copy-to-clipboard: 5.0.4_react@17.0.2 react-dom: 17.0.2_react@17.0.2 react-dplayer: 0.4.2_react@17.0.2 @@ -395,6 +397,10 @@ packages: tailwindcss: 3.0.18_833e1018ad0d7954aa80c53675939269 dev: false + /@types/cookie/0.3.3: + resolution: {integrity: sha512-LKVP3cgXBT9RYj+t+9FDKwS5tdI+rPBXaNSkma7hvqy35lc7mAokC2zsqWJH0LaqIt3B962nuYI77hsJoT1gow==} + dev: false + /@types/cors/2.8.12: resolution: {integrity: sha512-vt+kDhq/M2ayberEtJcIN/hxXy1Pk+59g2FV/ZQceeaTyCtCucjL2Q7FXlFjtWn4n15KCr1NE2lNNFhp0lEThw==} dev: true @@ -1068,6 +1074,11 @@ packages: safe-buffer: 5.1.2 dev: true + /cookie/0.4.2: + resolution: {integrity: sha512-aSWTXFzaKWkvHO1Ny/s+ePFpvKsPnjc551iI41v3ny/ow6tBG5Vd+FuqGNhh1LxOmVzOlGUriIlOaokOvhaStA==} + engines: {node: '>= 0.6'} + dev: false + /copy-to-clipboard/3.3.1: resolution: {integrity: sha512-i13qo6kIHTTpCm8/Wup+0b1mVWETvu2kIMzKoK8FpkLkFxlt0znUAHcMzox+T8sPlqtZXq3CulEjQHsYiGFJUw==} dependencies: @@ -3645,6 +3656,17 @@ packages: react-dom: 17.0.2_react@17.0.2 dev: false + /react-cookie/4.1.1_react@17.0.2: + resolution: {integrity: sha512-ffn7Y7G4bXiFbnE+dKhHhbP+b8I34mH9jqnm8Llhj89zF4nPxPutxHT1suUqMeCEhLDBI7InYwf1tpaSoK5w8A==} + peerDependencies: + react: '>= 16.3.0' + dependencies: + '@types/hoist-non-react-statics': 3.3.1 + hoist-non-react-statics: 3.3.2 + react: 17.0.2 + universal-cookie: 4.0.4 + dev: false + /react-copy-to-clipboard/5.0.4_react@17.0.2: resolution: {integrity: sha512-IeVAiNVKjSPeGax/Gmkqfa/+PuMTBhutEvFUaMQLwE2tS0EXrAdgOpWDX26bWTXF3HrioorR7lr08NqeYUWQCQ==} peerDependencies: @@ -4473,6 +4495,13 @@ packages: unist-util-visit-parents: 5.1.0 dev: false + /universal-cookie/4.0.4: + resolution: {integrity: sha512-lbRVHoOMtItjWbM7TwDLdl8wug7izB0tq3/YVKhT/ahB4VDvWMyvnADfnJI8y6fSvsjh51Ix7lTGC6Tn4rMPhw==} + dependencies: + '@types/cookie': 0.3.3 + cookie: 0.4.2 + dev: false + /universalify/0.1.2: resolution: {integrity: sha512-rBJeI5CXAlmy1pV+617WB9J63U6XcazHHF2f2dbJix4XzpUF0RS3Zbj0FGIOCAva5P/d/GBOYaACQ1w+0azUkg==} engines: {node: '>= 4.0.0'}