'use client' import { useState } from 'react' import { useRouter, usePathname } from 'next/navigation' import { useLocale, useTranslations } from 'next-intl' import { IconButton, Menu, MenuItem, Box, Typography, ListItemIcon, } from '@mui/material' import { Language, Check } from '@mui/icons-material' const languages = [ { code: 'en', name: 'English', flag: '🇺🇸' }, { code: 'ro', name: 'Română', flag: '🇷🇴' }, { code: 'es', name: 'Español', flag: '🇪🇸' }, { code: 'it', name: 'Italiano', flag: '🇮🇹' }, ] export function LanguageSwitcher() { const [anchorEl, setAnchorEl] = useState(null) const router = useRouter() const pathname = usePathname() const locale = useLocale() const t = useTranslations('navigation') const handleOpen = (event: React.MouseEvent) => { setAnchorEl(event.currentTarget) } const handleClose = () => { setAnchorEl(null) } const handleLanguageChange = async (newLocale: string) => { // Check if we're on a Bible page const isBiblePage = pathname.includes('/bible/') if (isBiblePage) { // For Bible pages, redirect to Bible reader without specific version // Let the reader component select appropriate version for the new language const newPath = `/${newLocale}/bible` router.push(newPath) } else { // For other pages, just change the locale in the URL const pathWithoutLocale = pathname.replace(`/${locale}`, '') || '/' const newPath = `/${newLocale}${pathWithoutLocale === '/' ? '' : pathWithoutLocale}` router.push(newPath) } handleClose() } const currentLanguage = languages.find(lang => lang.code === locale) return ( <> {languages.map((language) => ( handleLanguageChange(language.code)} selected={language.code === locale} > {language.flag} {language.name} {language.code === locale && ( )} ))} ) }