|
| 1 | +import { Globe } from 'lucide-react' |
| 2 | +import React from 'react' |
| 3 | +import { useTranslation } from 'react-i18next' |
| 4 | + |
| 5 | +import { |
| 6 | + Select, |
| 7 | + SelectContent, |
| 8 | + SelectItem, |
| 9 | + SelectTrigger, |
| 10 | + SelectValue, |
| 11 | +} from '@/components/ui/select' |
| 12 | +import { cn } from '@/lib/css-utils' |
| 13 | + |
| 14 | +const defaultLocales: Record<string, string> = { |
| 15 | + en: 'English', |
| 16 | + fr: 'Français', |
| 17 | + ar: 'العربية', |
| 18 | + es: 'Español', |
| 19 | + eo: 'Esperanto', |
| 20 | + ru: 'Русский', |
| 21 | + pt_BR: 'Português (Brasil)', |
| 22 | +} |
| 23 | + |
| 24 | +interface LanguageSelectorProps { |
| 25 | + value?: string |
| 26 | + handleChange?: (value: string) => void |
| 27 | + id?: string |
| 28 | + className?: string |
| 29 | + withIcon?: boolean |
| 30 | + additionalOptions?: Record<string, string> |
| 31 | +} |
| 32 | + |
| 33 | +const renderIcon = (value: string) => { |
| 34 | + if (value === 'fr') { |
| 35 | + return '🇫🇷' |
| 36 | + } else if (value === 'en') { |
| 37 | + return '🇬🇧' |
| 38 | + } else if (value === 'ar') { |
| 39 | + return '🇩🇿' |
| 40 | + } else if (value === 'pt_BR') { |
| 41 | + return '🇧🇷' |
| 42 | + } else if (value === 'es') { |
| 43 | + return '🇪🇸' |
| 44 | + } else if (value === 'eo') { |
| 45 | + return '🌍' |
| 46 | + } else if (value === 'ru') { |
| 47 | + return '🇷🇺' |
| 48 | + } |
| 49 | + return <Globe size={32} /> |
| 50 | +} |
| 51 | + |
| 52 | +const LanguageSelector: React.FC<LanguageSelectorProps> = ({ |
| 53 | + value, |
| 54 | + handleChange, |
| 55 | + id, |
| 56 | + className, |
| 57 | + withIcon, |
| 58 | + additionalOptions, |
| 59 | +}) => { |
| 60 | + // Force waiting for translations to be loaded |
| 61 | + useTranslation() |
| 62 | + |
| 63 | + const options = React.useMemo(() => { |
| 64 | + const merged: Record<number, React.ReactNode> = { |
| 65 | + ...defaultLocales, |
| 66 | + ...(additionalOptions || {}), |
| 67 | + } |
| 68 | + // Sort by key |
| 69 | + return Object.keys(merged) |
| 70 | + .sort() |
| 71 | + .reduce( |
| 72 | + (acc, key) => { |
| 73 | + acc[key] = !withIcon ? ( |
| 74 | + merged[key] |
| 75 | + ) : ( |
| 76 | + <> |
| 77 | + {renderIcon(key)} {merged[key]} |
| 78 | + </> |
| 79 | + ) |
| 80 | + return acc |
| 81 | + }, |
| 82 | + {} as Record<string, string>, |
| 83 | + ) |
| 84 | + }, [additionalOptions, withIcon]) |
| 85 | + |
| 86 | + return ( |
| 87 | + <div className={cn('flex items-center min-w-24', className)} data-cy="language-selector"> |
| 88 | + <Select onValueChange={handleChange} value={value}> |
| 89 | + <SelectTrigger className="w-full md:min-w-32 min-w-12" id={id}> |
| 90 | + <SelectValue /> |
| 91 | + </SelectTrigger> |
| 92 | + <SelectContent data-cy="language-selector-options"> |
| 93 | + {Object.entries(options).map(([key, label]) => ( |
| 94 | + <SelectItem key={key} value={key}> |
| 95 | + {label} |
| 96 | + </SelectItem> |
| 97 | + ))} |
| 98 | + </SelectContent> |
| 99 | + </Select> |
| 100 | + </div> |
| 101 | + ) |
| 102 | +} |
| 103 | + |
| 104 | +export default LanguageSelector |
0 commit comments