-
Notifications
You must be signed in to change notification settings - Fork 66.8k
Expand file tree
/
Copy pathAppRouterLanguagesContext.tsx
More file actions
54 lines (44 loc) · 1.29 KB
/
AppRouterLanguagesContext.tsx
File metadata and controls
54 lines (44 loc) · 1.29 KB
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
52
53
54
'use client'
import { createContext, useContext } from 'react'
import { clientLanguages, type ClientLanguageCode } from '@/languages/lib/client-languages'
export type AppRouterLanguageItem = {
name: string
nativeName?: string
code: string
hreflang?: string
}
export type AppRouterLanguagesContextT = {
languages: Record<string, AppRouterLanguageItem>
currentLanguage?: ClientLanguageCode
}
export const AppRouterLanguagesContext = createContext<AppRouterLanguagesContextT | null>(null)
export const useAppRouterLanguages = (): AppRouterLanguagesContextT => {
const context = useContext(AppRouterLanguagesContext)
if (!context) {
throw new Error(
'"useAppRouterLanguages" may only be used inside "AppRouterLanguagesContext.Provider"',
)
}
return context
}
/**
* Provider component for App Router language context
*/
interface AppRouterLanguagesProviderProps {
children: React.ReactNode
currentLanguage?: ClientLanguageCode
}
export function AppRouterLanguagesProvider({
children,
currentLanguage,
}: AppRouterLanguagesProviderProps) {
const value: AppRouterLanguagesContextT = {
languages: clientLanguages,
currentLanguage,
}
return (
<AppRouterLanguagesContext.Provider value={value}>
{children}
</AppRouterLanguagesContext.Provider>
)
}