cookie from server

This commit is contained in:
Stephen Zhou 2025-12-29 22:41:14 +08:00
parent 02b68f5a30
commit 442fef4bee
No known key found for this signature in database
2 changed files with 18 additions and 2 deletions

16
web/i18n-config/action.ts Normal file
View File

@ -0,0 +1,16 @@
'use server'
import type { Locale } from './language'
import { cookies } from 'next/headers'
import { LOCALE_COOKIE_NAME } from '@/config'
export async function setUserLocaleServer(locale: Locale) {
(await cookies()).set(
LOCALE_COOKIE_NAME,
locale,
{
// A year in milliseconds
expires: 60 * 60 * 24 * 365 * 1000,
},
)
}

View File

@ -4,6 +4,7 @@ import Cookies from 'js-cookie'
import { LOCALE_COOKIE_NAME } from '@/config'
import { changeLanguage } from '@/i18n-config/i18next-config'
import { LanguagesSupported } from '@/i18n-config/language'
import { setUserLocaleServer } from './action'
export const i18n = {
defaultLocale: 'en-US',
@ -13,10 +14,9 @@ export const i18n = {
export { Locale }
export const setLocaleOnClient = async (locale: Locale, reloadPage = true) => {
Cookies.set(LOCALE_COOKIE_NAME, locale, { expires: 365 })
await changeLanguage(locale)
if (reloadPage)
location.reload()
await setUserLocaleServer(locale)
}
export const getLocaleOnClient = (): Locale => {