1+ import { useTranslation } from 'react-i18next' ;
2+ import { Monitor , Check } from 'lucide-react' ;
3+
4+ const LanguageButtons = ( ) => {
5+ const { i18n } = useTranslation ( ) ;
6+
7+ const handleLanguageChange = ( lang : string | null ) => {
8+ const params = new URLSearchParams ( window . location . search ) ;
9+
10+ if ( lang ) {
11+ params . set ( 'lng' , lang ) ;
12+ i18n . changeLanguage ( lang ) ;
13+ } else {
14+ params . delete ( 'lng' ) ;
15+ const userLang = navigator . language . split ( '-' ) [ 0 ] ;
16+ i18n . changeLanguage ( userLang ) ;
17+ }
18+
19+ // Update URL without reloading the page
20+ const newUrl = `${ window . location . pathname } ${ params . toString ( ) ? '?' + params . toString ( ) : '' } ` ;
21+ window . history . pushState ( { path : newUrl } , '' , newUrl ) ;
22+ } ;
23+
24+ // Get current language from URL, fallback to i18n.language
25+ const getCurrentLanguage = ( ) => {
26+ const params = new URLSearchParams ( window . location . search ) ;
27+ return params . get ( 'lng' ) || i18n . language ;
28+ } ;
29+
30+ // Check if system language is being used (no lng param in URL)
31+ const isUsingSystemLanguage = ( ) => {
32+ const params = new URLSearchParams ( window . location . search ) ;
33+ return ! params . has ( 'lng' ) ;
34+ } ;
35+
36+ return (
37+ < div className = "fixed bottom-4 right-4 flex gap-2 bg-white rounded-full shadow-lg p-1 z-50" >
38+ < button
39+ onClick = { ( ) => handleLanguageChange ( null ) }
40+ className = { `p-2 rounded-full transition-colors hover:bg-gray-100 relative group ${
41+ isUsingSystemLanguage ( ) ? 'text-blue-500' : 'text-gray-600'
42+ } `}
43+ title = "Use system language"
44+ >
45+ < Monitor size = { 20 } />
46+ { isUsingSystemLanguage ( ) && (
47+ < Check className = "absolute -top-1 -right-1 text-blue-500" size = { 12 } />
48+ ) }
49+ < span className = "absolute right-0 bottom-full mb-2 hidden group-hover:block bg-gray-800 text-white text-xs px-2 py-1 rounded whitespace-nowrap" >
50+ System ({ navigator . language . split ( '-' ) [ 0 ] . toUpperCase ( ) } )
51+ </ span >
52+ </ button >
53+ < button
54+ onClick = { ( ) => handleLanguageChange ( 'nl' ) }
55+ className = { `p-2 rounded-full transition-colors hover:bg-gray-100 font-semibold relative group ${
56+ getCurrentLanguage ( ) === 'nl' && ! isUsingSystemLanguage ( ) ? 'text-blue-500' : 'text-gray-600'
57+ } `}
58+ title = "Nederlands"
59+ >
60+ NL
61+ { getCurrentLanguage ( ) === 'nl' && ! isUsingSystemLanguage ( ) && (
62+ < Check className = "absolute -top-1 -right-1 text-blue-500" size = { 12 } />
63+ ) }
64+ < span className = "absolute right-0 bottom-full mb-2 hidden group-hover:block bg-gray-800 text-white text-xs px-2 py-1 rounded" >
65+ Nederlands
66+ </ span >
67+ </ button >
68+ < button
69+ onClick = { ( ) => handleLanguageChange ( 'en' ) }
70+ className = { `p-2 rounded-full transition-colors hover:bg-gray-100 font-semibold relative group ${
71+ getCurrentLanguage ( ) === 'en' && ! isUsingSystemLanguage ( ) ? 'text-blue-500' : 'text-gray-600'
72+ } `}
73+ title = "English"
74+ >
75+ EN
76+ { getCurrentLanguage ( ) === 'en' && ! isUsingSystemLanguage ( ) && (
77+ < Check className = "absolute -top-1 -right-1 text-blue-500" size = { 12 } />
78+ ) }
79+ < span className = "absolute right-0 bottom-full mb-2 hidden group-hover:block bg-gray-800 text-white text-xs px-2 py-1 rounded" >
80+ English
81+ </ span >
82+ </ button >
83+ </ div >
84+ ) ;
85+ } ;
86+
87+ export default LanguageButtons ;
0 commit comments