| 1 | import { Globe } from "lucide-react"; |
| 2 | import { useTranslation } from "react-i18next"; |
| 3 | |
| 4 | import { setAppLanguage } from "@/i18n"; |
| 5 | import { |
| 6 | currentLocale, |
| 7 | } from "@/i18n"; |
| 8 | import { |
| 9 | localeOption, |
| 10 | supportedLocales, |
| 11 | type SupportedLocale, |
| 12 | } from "@/i18n/config"; |
| 13 | import { Button } from "@/components/ui/button"; |
| 14 | import { |
| 15 | DropdownMenu, |
| 16 | DropdownMenuContent, |
| 17 | DropdownMenuLabel, |
| 18 | DropdownMenuRadioGroup, |
| 19 | DropdownMenuRadioItem, |
| 20 | DropdownMenuSeparator, |
| 21 | DropdownMenuTrigger, |
| 22 | } from "@/components/ui/dropdown-menu"; |
| 23 | |
| 24 | export function LanguageSwitcher() { |
| 25 | const { t } = useTranslation(); |
| 26 | const locale = currentLocale(); |
| 27 | const selected = localeOption(locale); |
| 28 | |
| 29 | return ( |
| 30 | <DropdownMenu> |
| 31 | <DropdownMenuTrigger asChild> |
| 32 | <Button |
| 33 | variant="ghost" |
| 34 | size="sm" |
| 35 | aria-label={t("sidebar.language.ariaLabel")} |
| 36 | className="h-7 gap-1.5 rounded-md px-2 text-[11px] text-muted-foreground hover:bg-sidebar-accent hover:text-sidebar-foreground" |
| 37 | > |
| 38 | <Globe className="h-3.5 w-3.5" /> |
| 39 | <span className="max-w-[7rem] truncate">{selected.nativeLabel}</span> |
| 40 | </Button> |
| 41 | </DropdownMenuTrigger> |
| 42 | <DropdownMenuContent align="end"> |
| 43 | <DropdownMenuLabel>{t("sidebar.language.label")}</DropdownMenuLabel> |
| 44 | <DropdownMenuSeparator /> |
| 45 | <DropdownMenuRadioGroup |
| 46 | value={locale} |
| 47 | onValueChange={(value) => { |
| 48 | void setAppLanguage(value as SupportedLocale); |
| 49 | }} |
| 50 | > |
| 51 | {supportedLocales.map((option) => ( |
| 52 | <DropdownMenuRadioItem key={option.code} value={option.code}> |
| 53 | <span className="flex min-w-0 items-center gap-2"> |
| 54 | <span>{option.nativeLabel}</span> |
| 55 | {option.nativeLabel !== option.label ? ( |
| 56 | <span className="truncate text-xs text-muted-foreground"> |
| 57 | {option.label} |
| 58 | </span> |
| 59 | ) : null} |
| 60 | </span> |
| 61 | </DropdownMenuRadioItem> |
| 62 | ))} |
| 63 | </DropdownMenuRadioGroup> |
| 64 | </DropdownMenuContent> |
| 65 | </DropdownMenu> |
| 66 | ); |
| 67 | } |
| 68 |