|
1 | 1 | import React from 'react'; |
2 | 2 | import {useTranslation} from 'react-i18next'; |
| 3 | +import {Dropdown} from 'primereact/dropdown'; |
3 | 4 | import {SUPPORTED_LANGUAGES} from './languages'; |
4 | 5 |
|
5 | | -// Dependency-free language picker (plain <select>) so the shared package stays |
6 | | -// light. Apps pass className/style to match their own chrome. |
| 6 | +// Language picker built on primereact's Dropdown so it inherits the site theme |
| 7 | +// (both apps already ship primereact). A leading globe icon marks it as the |
| 8 | +// language control. Apps pass className/style to fit their own chrome. |
7 | 9 | export function LanguageSwitcher({className, style}) { |
8 | 10 | const {i18n, t} = useTranslation(); |
9 | 11 | const active = (i18n.resolvedLanguage || i18n.language || 'en').split('-')[0]; |
10 | 12 | const value = SUPPORTED_LANGUAGES.some((l) => l.code === active) ? active : 'en'; |
11 | 13 |
|
| 14 | + const selectedTemplate = (option) => { |
| 15 | + const label = |
| 16 | + option?.label ?? |
| 17 | + SUPPORTED_LANGUAGES.find((l) => l.code === value)?.label ?? |
| 18 | + ''; |
| 19 | + return ( |
| 20 | + <span className="flex align-items-center gap-2"> |
| 21 | + <i className="pi pi-globe" /> |
| 22 | + <span>{label}</span> |
| 23 | + </span> |
| 24 | + ); |
| 25 | + }; |
| 26 | + |
12 | 27 | return ( |
13 | | - <select |
| 28 | + <Dropdown |
14 | 29 | aria-label={t('language')} |
15 | 30 | className={className} |
16 | | - style={style} |
| 31 | + style={{width: '11rem', ...style}} |
17 | 32 | value={value} |
18 | | - onChange={(e) => i18n.changeLanguage(e.target.value)} |
19 | | - > |
20 | | - {SUPPORTED_LANGUAGES.map((l) => ( |
21 | | - <option key={l.code} value={l.code}>{l.label}</option> |
22 | | - ))} |
23 | | - </select> |
| 33 | + options={SUPPORTED_LANGUAGES} |
| 34 | + optionLabel="label" |
| 35 | + optionValue="code" |
| 36 | + scrollHeight="500px" |
| 37 | + onChange={(e) => i18n.changeLanguage(e.value)} |
| 38 | + valueTemplate={selectedTemplate} |
| 39 | + pt={{ |
| 40 | + input: {className: 'p-2'}, |
| 41 | + trigger: {style: {width: '2rem'}}, |
| 42 | + }} |
| 43 | + /> |
24 | 44 | ); |
25 | 45 | } |
0 commit comments