Save language selction local storage or cookie #1201
Answered
by
aralroca
MuhammadSaim
asked this question in
Q&A
-
How can I save the selected language in local storage or cookie when a user comes back and according to the cookie or value in storage selects his/her selected language? |
Beta Was this translation helpful? Give feedback.
Answered by
aralroca
Mar 25, 2024
Replies: 1 comment 1 reply
-
I hope this documentation helps: https://github.com/aralroca/next-translate?tab=readme-ov-file#11-how-to-save-the-user-defined-language: import { useRouter } from 'next/router'
// ...
function usePersistLocaleCookie() {
const { locale, defaultLocale } = useRouter()
useEffect(persistLocaleCookie, [locale, defaultLocale])
function persistLocaleCookie() {
if(locale !== defaultLocale) {
const date = new Date()
const expireMs = 100 * 24 * 60 * 60 * 1000 // 100 days
date.setTime(date.getTime() + expireMs)
document.cookie = `NEXT_LOCALE=${locale};expires=${date.toUTCString()};path=/`
}
}
} The cookie name should be |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
MuhammadSaim
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I hope this documentation helps: https://github.com/aralroca/next-translate?tab=readme-ov-file#11-how-to-save-the-user-defined-language:
The cookie name should be
NEXT_LOCALE
, t…