import React, { Component } from 'react'; import Box from '@mui/material/Box'; import Button from '@mui/material/Button'; import Menu from '@mui/material/Menu'; import MenuItem from '@mui/material/MenuItem'; import Typography from '@mui/material/Typography'; import { withI18n } from '../i18n/withTranslation.js'; class LanguageSwitcher extends Component { constructor(props) { super(props); this.state = { anchorEl: null, loadedFlags: {} }; } handleClick = (event) => { this.setState({ anchorEl: event.currentTarget }); }; handleClose = () => { this.setState({ anchorEl: null }); }; handleLanguageChange = async (language) => { const { languageContext } = this.props; if (languageContext) { try { await languageContext.changeLanguage(language); } catch (error) { console.error('Failed to change language:', error); } } this.handleClose(); }; // Lazy load flag components loadFlagComponent = async (lang) => { if (this.state.loadedFlags[lang]) { return this.state.loadedFlags[lang]; } try { const flagMap = { 'ar': () => import('country-flag-icons/react/3x2').then(m => m.EG), 'bg': () => import('country-flag-icons/react/3x2').then(m => m.BG), 'cs': () => import('country-flag-icons/react/3x2').then(m => m.CZ), 'de': () => import('country-flag-icons/react/3x2').then(m => m.DE), 'el': () => import('country-flag-icons/react/3x2').then(m => m.GR), 'en': () => import('country-flag-icons/react/3x2').then(m => m.US), 'es': () => import('country-flag-icons/react/3x2').then(m => m.ES), 'fr': () => import('country-flag-icons/react/3x2').then(m => m.FR), 'hr': () => import('country-flag-icons/react/3x2').then(m => m.HR), 'hu': () => import('country-flag-icons/react/3x2').then(m => m.HU), 'it': () => import('country-flag-icons/react/3x2').then(m => m.IT), 'pl': () => import('country-flag-icons/react/3x2').then(m => m.PL), 'ro': () => import('country-flag-icons/react/3x2').then(m => m.RO), 'ru': () => import('country-flag-icons/react/3x2').then(m => m.RU), 'sk': () => import('country-flag-icons/react/3x2').then(m => m.SK), 'sl': () => import('country-flag-icons/react/3x2').then(m => m.SI), 'sq': () => import('country-flag-icons/react/3x2').then(m => m.AL), 'sr': () => import('country-flag-icons/react/3x2').then(m => m.RS), 'sv': () => import('country-flag-icons/react/3x2').then(m => m.SE), 'tr': () => import('country-flag-icons/react/3x2').then(m => m.TR), 'uk': () => import('country-flag-icons/react/3x2').then(m => m.UA), 'zh': () => import('country-flag-icons/react/3x2').then(m => m.CN) }; const flagLoader = flagMap[lang]; if (flagLoader) { const FlagComponent = await flagLoader(); this.setState(prevState => ({ loadedFlags: { ...prevState.loadedFlags, [lang]: FlagComponent } })); return FlagComponent; } } catch (error) { console.warn(`Failed to load flag for language: ${lang}`, error); } return null; }; getLanguageFlag = (lang) => { const FlagComponent = this.state.loadedFlags[lang]; if (FlagComponent) { return ( ); } // Loading placeholder or fallback return ( {this.getLanguageLabel(lang)} ); }; // Load flags when menu opens componentDidUpdate(prevProps, prevState) { const { anchorEl } = this.state; const { languageContext } = this.props; if (anchorEl && !prevState.anchorEl && languageContext) { // Menu just opened, lazy load flags for all languages (not just available ones) languageContext.allLanguages.forEach(lang => { if (!this.state.loadedFlags[lang]) { this.loadFlagComponent(lang); } }); } } getLanguageLabel = (lang) => { const labels = { 'ar': 'EG', 'bg': 'BG', 'cs': 'CZ', 'de': 'DE', 'el': 'GR', 'en': 'US', 'es': 'ES', 'fr': 'FR', 'hr': 'HR', 'hu': 'HU', 'it': 'IT', 'pl': 'PL', 'ro': 'RO', 'ru': 'RU', 'sk': 'SK', 'sl': 'SI', 'sq': 'AL', 'sr': 'RS', 'sv': 'SE', 'tr': 'TR', 'uk': 'UA', 'zh': 'CN' }; return labels[lang] || lang.toUpperCase(); }; getLanguageName = (lang) => { const names = { 'ar': 'العربية', 'bg': 'Български', 'cs': 'Čeština', 'de': 'Deutsch', 'el': 'Ελληνικά', 'en': 'English', 'es': 'Español', 'fr': 'Français', 'hr': 'Hrvatski', 'hu': 'Magyar', 'it': 'Italiano', 'pl': 'Polski', 'ro': 'Română', 'ru': 'Русский', 'sk': 'Slovenčina', 'sl': 'Slovenščina', 'sq': 'Shqip', 'sr': 'Српски', 'sv': 'Svenska', 'tr': 'Türkçe', 'uk': 'Українська', 'zh': '中文' }; return names[lang] || lang; }; render() { const { languageContext } = this.props; const { anchorEl } = this.state; if (!languageContext) { return null; } const { currentLanguage, allLanguages } = languageContext; const open = Boolean(anchorEl); return ( {allLanguages.map((language) => { return ( this.handleLanguageChange(language)} selected={language === currentLanguage} sx={{ minWidth: 160, display: 'flex', justifyContent: 'space-between', gap: 2 }} > {this.getLanguageFlag(language)} {this.getLanguageName(language)} {this.getLanguageLabel(language)} ); })} ); } } export default withI18n()(LanguageSwitcher);