@@ -9,6 +9,8 @@ import { Sheet, SheetContent, SheetTrigger, SheetTitle } from "@/components/ui/s
9
9
import { Menu } from "lucide-react" ;
10
10
import { siteConfig } from "@/config/site" ;
11
11
import { Locale } from "@/lib/i18n/settings" ;
12
+ import { useRouter } from "next/navigation" ;
13
+ import { setCookie } from "@/lib/cookies" ;
12
14
13
15
interface HeaderProps {
14
16
translations : any ;
@@ -18,11 +20,24 @@ interface HeaderProps {
18
20
export default function Header ( { translations, locale } : HeaderProps ) {
19
21
const t = translations . common ;
20
22
const { theme, setTheme } = useTheme ( ) ;
23
+ const router = useRouter ( ) ;
21
24
22
25
const toggleTheme = ( ) => {
23
26
setTheme ( theme === "dark" ? "light" : "dark" ) ;
24
27
} ;
25
28
29
+ const handleLanguageChange = ( newLocale : Locale ) => {
30
+ // Save the preferred locale to localStorage
31
+ if ( typeof window !== "undefined" ) {
32
+ localStorage . setItem ( 'preferredLocale' , newLocale ) ;
33
+
34
+ // Set a cookie for the middleware (which can't access localStorage)
35
+ setCookie ( 'preferredLocale' , newLocale , 365 ) ; // Valid for 1 year
36
+ }
37
+
38
+ router . push ( `/${ newLocale } ` ) ;
39
+ } ;
40
+
26
41
return (
27
42
< header className = "sticky top-0 z-40 w-full border-b bg-background/95 backdrop-blur supports-[backdrop-filter]:bg-background/60" >
28
43
< div className = "w-full flex h-16 md:h-20 items-center px-4 md:px-6 lg:px-8" >
@@ -52,24 +67,22 @@ export default function Header({ translations, locale }: HeaderProps) {
52
67
{ theme === "dark" ? < SunIcon size = { 18 } /> : < MoonIcon size = { 18 } /> }
53
68
</ Button >
54
69
< div className = "flex items-center gap-1 ml-2 sm:ml-4 border rounded-full px-1 py-0.5" >
55
- < Link href = "/en" locale = "en" className = "cursor-pointer" >
56
- < Button
57
- variant = "ghost"
58
- size = "sm"
59
- className = { `rounded-full px-2 sm:px-3 text-xs sm:text-sm h-7 sm:h-8 ${ locale === "en" ? "bg-primary text-primary-foreground" : "" } ` }
60
- >
61
- EN
62
- </ Button >
63
- </ Link >
64
- < Link href = "/vi" locale = "vi" className = "cursor-pointer" >
65
- < Button
66
- variant = "ghost"
67
- size = "sm"
68
- className = { `rounded-full px-2 sm:px-3 text-xs sm:text-sm h-7 sm:h-8 ${ locale === "vi" ? "bg-primary text-primary-foreground" : "" } ` }
69
- >
70
- VI
71
- </ Button >
72
- </ Link >
70
+ < Button
71
+ variant = "ghost"
72
+ size = "sm"
73
+ className = { `rounded-full px-2 sm:px-3 text-xs sm:text-sm h-7 sm:h-8 ${ locale === "en" ? "bg-primary text-primary-foreground" : "" } ` }
74
+ onClick = { ( ) => handleLanguageChange ( "en" ) }
75
+ >
76
+ EN
77
+ </ Button >
78
+ < Button
79
+ variant = "ghost"
80
+ size = "sm"
81
+ className = { `rounded-full px-2 sm:px-3 text-xs sm:text-sm h-7 sm:h-8 ${ locale === "vi" ? "bg-primary text-primary-foreground" : "" } ` }
82
+ onClick = { ( ) => handleLanguageChange ( "vi" ) }
83
+ >
84
+ VI
85
+ </ Button >
73
86
</ div >
74
87
</ div >
75
88
< Sheet >
@@ -104,18 +117,20 @@ export default function Header({ translations, locale }: HeaderProps) {
104
117
< div className = "space-y-3" >
105
118
< h3 className = "text-sm uppercase text-muted-foreground font-medium" > { t . languages ?. title || 'Languages' } </ h3 >
106
119
< div className = "flex flex-col space-y-2" >
107
- < Link href = "/en" locale = "en" className = "cursor-pointer" >
108
- < div className = { `flex items-center px-3 py-2 rounded-lg ${ locale === "en" ? "bg-primary/10 text-primary" : "" } ` } >
109
- < span className = "text-base" > { t . languages ?. english || 'English' } </ span >
110
- { locale === "en" && < span className = "ml-auto text-sm bg-primary text-primary-foreground px-2 py-0.5 rounded-full" > Active</ span > }
111
- </ div >
112
- </ Link >
113
- < Link href = "/vi" locale = "vi" className = "cursor-pointer" >
114
- < div className = { `flex items-center px-3 py-2 rounded-lg ${ locale === "vi" ? "bg-primary/10 text-primary" : "" } ` } >
115
- < span className = "text-base" > { t . languages ?. vietnamese || 'Tiếng Việt' } </ span >
116
- { locale === "vi" && < span className = "ml-auto text-sm bg-primary text-primary-foreground px-2 py-0.5 rounded-full" > Active</ span > }
117
- </ div >
118
- </ Link >
120
+ < div
121
+ className = { `flex items-center px-3 py-2 rounded-lg cursor-pointer ${ locale === "en" ? "bg-primary/10 text-primary" : "" } ` }
122
+ onClick = { ( ) => handleLanguageChange ( "en" ) }
123
+ >
124
+ < span className = "text-base" > { t . languages ?. english || 'English' } </ span >
125
+ { locale === "en" && < span className = "ml-auto text-sm bg-primary text-primary-foreground px-2 py-0.5 rounded-full" > Active</ span > }
126
+ </ div >
127
+ < div
128
+ className = { `flex items-center px-3 py-2 rounded-lg cursor-pointer ${ locale === "vi" ? "bg-primary/10 text-primary" : "" } ` }
129
+ onClick = { ( ) => handleLanguageChange ( "vi" ) }
130
+ >
131
+ < span className = "text-base" > { t . languages ?. vietnamese || 'Tiếng Việt' } </ span >
132
+ { locale === "vi" && < span className = "ml-auto text-sm bg-primary text-primary-foreground px-2 py-0.5 rounded-full" > Active</ span > }
133
+ </ div >
119
134
</ div >
120
135
</ div >
121
136
< div className = "space-y-3" >
0 commit comments