2022-03-10 20:59:16 +00:00
|
|
|
import { Language } from '@mui/icons-material';
|
|
|
|
import { IconButton, Popover } from '@mui/material';
|
|
|
|
import { useRouter } from 'next/router';
|
2022-03-11 07:43:20 +00:00
|
|
|
import { useTranslation } from 'next-i18next';
|
2022-03-10 20:59:16 +00:00
|
|
|
import { MouseEvent, useState } from 'react';
|
|
|
|
|
|
|
|
import { languages } from '@/config/languages';
|
|
|
|
|
|
|
|
import styles from './LanguageSwitcher.module.scss';
|
|
|
|
|
|
|
|
const LanguageSwitcher = () => {
|
|
|
|
const router = useRouter();
|
|
|
|
|
2022-03-11 07:43:20 +00:00
|
|
|
const { t } = useTranslation();
|
2022-03-10 20:59:16 +00:00
|
|
|
|
|
|
|
const [anchorEl, setAnchorEl] = useState<HTMLButtonElement | null>(null);
|
|
|
|
|
|
|
|
const handleClick = (event: MouseEvent<HTMLButtonElement>) => setAnchorEl(event.currentTarget);
|
|
|
|
|
|
|
|
const handleClose = () => setAnchorEl(null);
|
|
|
|
|
|
|
|
const handleChangeLanguage = (locale: string) => {
|
|
|
|
const { pathname, asPath, query } = router;
|
|
|
|
|
|
|
|
document.cookie = `NEXT_LOCALE=${locale}; path=/; expires=2147483647`;
|
|
|
|
router.push({ pathname, query }, asPath, { locale });
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<IconButton onClick={handleClick}>
|
|
|
|
<Language />
|
|
|
|
</IconButton>
|
|
|
|
|
|
|
|
<Popover
|
|
|
|
anchorEl={anchorEl}
|
|
|
|
open={Boolean(anchorEl)}
|
|
|
|
onClose={handleClose}
|
|
|
|
anchorOrigin={{
|
|
|
|
vertical: 'top',
|
|
|
|
horizontal: 'right',
|
|
|
|
}}
|
|
|
|
transformOrigin={{
|
|
|
|
vertical: 'bottom',
|
|
|
|
horizontal: 'right',
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<div className={styles.popover}>
|
|
|
|
<div className={styles.container}>
|
|
|
|
{languages.map(({ code, name, localName }) => (
|
|
|
|
<p key={code} className={styles.language} onClick={() => handleChangeLanguage(code)}>
|
|
|
|
{name} {localName && `(${localName})`}
|
|
|
|
</p>
|
|
|
|
))}
|
|
|
|
|
|
|
|
<a href="https://translate.rxresu.me" target="_blank" rel="noreferrer" className={styles.language}>
|
2022-03-11 07:43:20 +00:00
|
|
|
{t('common.footer.language.missing')}
|
2022-03-10 20:59:16 +00:00
|
|
|
</a>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</Popover>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default LanguageSwitcher;
|