import { Link } from '@/i18n/navigation'; import { locales, Locale } from '@/i18n/locales'; import { getTranslations } from 'next-intl/server'; // Map locales to flag emojis const flagEmojis: Record = { en: '🇬🇧', tr: '🇹🇷' }; type Props = { locale: string; className?: string; pathname?: string; // Optional pathname to navigate to }; /** * Server Component for switching locales * Uses the Link component from next-intl/navigation * Renders as a dropdown that excludes the current language */ export default async function LocaleSwitcherServer({ locale, className = '', pathname }: Props) { const t = await getTranslations({ locale: locale as Locale, namespace: 'Index' }); // Use the provided pathname or default to the current route let pathWithoutLocale = pathname || '/home'; // If we're on a locale-prefixed route like /en/home or /tr/about, // extract just the path part without the locale prefix if (pathWithoutLocale.startsWith(`/${locale}/`)) { pathWithoutLocale = pathWithoutLocale.substring(locale.length + 1); } else if (pathWithoutLocale.startsWith('/')) { // If it already starts with /, keep it as is } else { // Ensure it starts with / pathWithoutLocale = `/${pathWithoutLocale}`; } // Filter out the current locale from the available options const availableLocales = locales.filter(lang => lang !== locale); return (
{availableLocales.map((lang) => ( {flagEmojis[lang]} ))}
); }