1
1
// components/LanguageSwitcher.tsx
2
2
import { useRouter } from 'next/router' ;
3
3
import { useEffect , useRef , useState } from 'react' ;
4
+ import { MdTranslate } from 'react-icons/md' ;
4
5
5
6
type LanguageSwitchProps = {
6
7
type ?: string ;
@@ -9,29 +10,25 @@ type LanguageSwitchProps = {
9
10
const LanguageSwitcher = ( props : LanguageSwitchProps ) => {
10
11
const router = useRouter ( ) ;
11
12
const { locale, asPath } = router ;
12
- const [ selectedLocale , setSelectedLocale ] = useState ( locale ) ;
13
13
const [ isHovered , setIsHovered ] = useState ( false ) ;
14
14
const dropdownRef = useRef < HTMLDivElement > ( null ) ;
15
15
const timeoutRef = useRef < NodeJS . Timeout | null > ( null ) ;
16
16
17
17
useEffect ( ( ) => {
18
18
const storedLocale = localStorage . getItem ( 'locale' ) ;
19
19
if ( storedLocale ) {
20
- setSelectedLocale ( storedLocale ) ;
21
20
router . push ( asPath , asPath , { locale : storedLocale } ) ;
22
21
} else {
23
22
const systemLocale = navigator . language . toLowerCase ( ) . startsWith ( 'zh' )
24
23
? 'zh'
25
24
: 'en' ;
26
- setSelectedLocale ( systemLocale ) ;
27
25
localStorage . setItem ( 'locale' , systemLocale ) ;
28
26
router . push ( asPath , asPath , { locale : systemLocale } ) ;
29
27
}
30
28
} , [ ] ) ;
31
29
32
30
const changeLanguage = ( language : string ) => {
33
31
localStorage . setItem ( 'locale' , language ) ;
34
- setSelectedLocale ( language ) ;
35
32
setIsHovered ( false ) ;
36
33
router . push ( asPath , asPath , { locale : language } ) ;
37
34
} ;
@@ -74,7 +71,8 @@ const LanguageSwitcher = (props: LanguageSwitchProps) => {
74
71
aria-haspopup = 'true'
75
72
aria-expanded = { isHovered ? 'true' : 'false' }
76
73
>
77
- { selectedLocale === 'zh' ? '中文' : 'EN' }
74
+ < MdTranslate size = { 16 } />
75
+ { /* {selectedLocale === 'zh' ? '中文' : 'EN'} */ }
78
76
</ button >
79
77
</ div >
80
78
0 commit comments