@@ -9,6 +9,8 @@ import { Sheet, SheetContent, SheetTrigger, SheetTitle } from "@/components/ui/s
99import { Menu } from "lucide-react" ;
1010import { siteConfig } from "@/config/site" ;
1111import { Locale } from "@/lib/i18n/settings" ;
12+ import { useRouter } from "next/navigation" ;
13+ import { setCookie } from "@/lib/cookies" ;
1214
1315interface HeaderProps {
1416 translations : any ;
@@ -18,11 +20,24 @@ interface HeaderProps {
1820export default function Header ( { translations, locale } : HeaderProps ) {
1921 const t = translations . common ;
2022 const { theme, setTheme } = useTheme ( ) ;
23+ const router = useRouter ( ) ;
2124
2225 const toggleTheme = ( ) => {
2326 setTheme ( theme === "dark" ? "light" : "dark" ) ;
2427 } ;
2528
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+
2641 return (
2742 < header className = "sticky top-0 z-40 w-full border-b bg-background/95 backdrop-blur supports-[backdrop-filter]:bg-background/60" >
2843 < 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) {
5267 { theme === "dark" ? < SunIcon size = { 18 } /> : < MoonIcon size = { 18 } /> }
5368 </ Button >
5469 < 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 >
7386 </ div >
7487 </ div >
7588 < Sheet >
@@ -104,18 +117,20 @@ export default function Header({ translations, locale }: HeaderProps) {
104117 < div className = "space-y-3" >
105118 < h3 className = "text-sm uppercase text-muted-foreground font-medium" > { t . languages ?. title || 'Languages' } </ h3 >
106119 < 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 >
119134 </ div >
120135 </ div >
121136 < div className = "space-y-3" >
0 commit comments