Files
webapp-conversation/i18n/server.ts
lyzno1 9ff81c0306 fix: update cookies() usage for Next.js 15 compatibility
- Make getLocaleOnServer async and await cookies()
- Update LocaleLayout to be async component
- Fix react-tooltip compatibility with React 19

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-15 10:56:19 +08:00

31 lines
1.1 KiB
TypeScript

import 'server-only'
import { cookies, headers } from 'next/headers'
import Negotiator from 'negotiator'
import { match } from '@formatjs/intl-localematcher'
import type { Locale } from '.'
import { i18n } from '.'
export const getLocaleOnServer = async (): Promise<Locale> => {
// @ts-expect-error locales are readonly
const locales: string[] = i18n.locales
let languages: string[] | undefined
// get locale from cookie
const localeCookie = (await cookies()).get('locale')
languages = localeCookie?.value ? [localeCookie.value] : []
if (!languages.length) {
// Negotiator expects plain object so we need to transform headers
const negotiatorHeaders: Record<string, string> = {}
const headersList = await headers()
headersList.forEach((value, key) => (negotiatorHeaders[key] = value))
// Use negotiator and intl-localematcher to get best locale
languages = new Negotiator({ headers: negotiatorHeaders }).languages()
}
// match locale
const matchedLocale = match(languages, locales, i18n.defaultLocale) as Locale
return matchedLocale
}