Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 8x 8x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 8x 8x 8x 8x 8x 8x 8x 1x 1x 12x 12x 4x 4x 8x 8x | 'use client'
import { createContext, useContext, useState, type ReactNode } from 'react'
import { getMessages, type Locale } from '@/i18n/messages'
import { LOCALE_COOKIE_NAME } from '@/i18n/routing'
interface LocaleContextValue {
locale: Locale
messages: Record<string, any>
changeLocale: (newLocale: Locale) => Promise<void>
}
const LocaleContext = createContext<LocaleContextValue | undefined>(undefined)
interface LocaleProviderProps {
children: ReactNode
initialLocale: Locale
initialMessages: Record<string, any>
}
export function LocaleProvider({ children, initialLocale, initialMessages }: LocaleProviderProps) {
const [locale, setLocale] = useState<Locale>(initialLocale)
const [messages, setMessages] = useState<Record<string, any>>(initialMessages)
const changeLocale = async (newLocale: Locale) => {
// Update cookie
document.cookie = `${LOCALE_COOKIE_NAME}=${newLocale}; path=/; max-age=${60 * 60 * 24 * 365}`
// Load new messages
const newMessages = await getMessages(newLocale)
// Update state
setLocale(newLocale)
setMessages(newMessages)
}
return (
<LocaleContext.Provider value={{ locale, messages, changeLocale }}>
{children}
</LocaleContext.Provider>
)
}
export function useLocaleContext() {
const context = useContext(LocaleContext)
if (!context) {
throw new Error('useLocaleContext must be used within LocaleProvider')
}
return context
}
|