"use client"; import Image from "next/image"; import { usePathname } from "next/navigation"; import { Button } from "@/components/ui/button"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu"; import { AppLocale, getLocalizedPath, stripLocalePrefix } from "@/lib/locale"; import { cn } from "@/lib/utils"; const PENDING_TOAST_STORAGE_KEY = "mohfarawati-pending-toast"; type LocaleToggleProps = { locale: string; className?: string; showLabel?: boolean; }; const localeChangedMessages: Record = { de: "Sprache auf Deutsch gewechselt", en: "Language changed to English", ar: "تم تغيير اللغة إلى العربية", }; export function LocaleToggle({ locale, className, showLabel = false, }: LocaleToggleProps) { const pathname = usePathname(); const locales: AppLocale[] = ["de", "en", "ar"]; const currentPath = stripLocalePrefix(pathname); const currentLocale = (["de", "en", "ar"].includes(locale) ? locale : "de") as AppLocale; const availableLocales = locales.filter((targetLocale) => targetLocale !== currentLocale); const contentFontClassName = currentLocale === "ar" ? "font-arabic" : "font-latin"; const localeFlags: Record = { de: { src: "/flags/de.svg", alt: "German" }, en: { src: "/flags/us.svg", alt: "English" }, ar: { src: "/flags/sy.svg", alt: "Arabic" }, }; return ( {availableLocales.map((targetLocale) => ( { const message = localeChangedMessages[targetLocale]; if (!message) { return; } window.sessionStorage.setItem( PENDING_TOAST_STORAGE_KEY, JSON.stringify({ type: "success", message, targetLocale, }), ); }} className="group flex h-10 w-10 items-center justify-center rounded-full" > {localeFlags[targetLocale].alt} ))} ); }