-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeader.jsx
More file actions
47 lines (44 loc) · 1.47 KB
/
Copy pathHeader.jsx
File metadata and controls
47 lines (44 loc) · 1.47 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
import React, { useState, useEffect } from 'react';
import { useTranslation } from 'react-i18next';
export function Header() {
const { t, i18n } = useTranslation();
const [lang, setLang] = useState(() => localStorage.getItem('language') || i18n.language || 'zh');
useEffect(() => {
i18n.changeLanguage(lang);
localStorage.setItem('language', lang);
}, [lang, i18n]);
return (
<div className="flex items-center gap-2 whitespace-nowrap">
<div
role="group"
aria-label={t('header.language')}
className="flex overflow-hidden rounded border text-xs whitespace-nowrap"
>
<button
type="button"
onClick={() => setLang('zh')}
aria-pressed={lang === 'zh'}
className={`px-2 py-1 transition-colors focus:outline-none whitespace-nowrap ${
lang === 'zh'
? 'bg-blue-500 text-white'
: 'bg-white text-gray-700 dark:bg-gray-800 dark:text-gray-300'
}`}
>
{t('language.zh')}
</button>
<button
type="button"
onClick={() => setLang('en')}
aria-pressed={lang === 'en'}
className={`px-2 py-1 transition-colors focus:outline-none whitespace-nowrap ${
lang === 'en'
? 'bg-blue-500 text-white'
: 'bg-white text-gray-700 dark:bg-gray-800 dark:text-gray-300'
}`}
>
{t('language.en')}
</button>
</div>
</div>
);
}