2025-06-30 13:45:33 +03:30
|
|
|
'use client';
|
|
|
|
|
|
|
|
|
|
import { useEffect, useState } from 'react';
|
|
|
|
|
import { TLocales } from '@/models/layout';
|
|
|
|
|
import images from '@/assets/images/images';
|
|
|
|
|
import Image from 'next/image';
|
2025-06-30 17:06:27 +03:30
|
|
|
import { useTranslations } from 'next-intl';
|
2025-06-30 13:45:33 +03:30
|
|
|
|
|
|
|
|
const languages = [
|
|
|
|
|
{
|
|
|
|
|
title: 'EN',
|
2025-06-30 17:06:27 +03:30
|
|
|
image: images.en,
|
2025-06-30 13:45:33 +03:30
|
|
|
locale: 'en',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
title: 'FA',
|
2025-06-30 17:06:27 +03:30
|
|
|
image: images.fa,
|
2025-06-30 13:45:33 +03:30
|
|
|
locale: 'fa',
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
export default function ModalLanguageSelector() {
|
2025-06-30 17:06:27 +03:30
|
|
|
const [show, setShow] = useState(false);
|
|
|
|
|
const t = useTranslations();
|
2025-06-30 13:45:33 +03:30
|
|
|
useEffect(() => {
|
2025-06-30 17:06:27 +03:30
|
|
|
const selected = localStorage.getItem('selectedLocale');
|
2026-07-19 07:59:16 +03:30
|
|
|
const cookieLocale = document.cookie
|
|
|
|
|
.split('; ')
|
|
|
|
|
.find((item) => item.startsWith('locale='))
|
|
|
|
|
?.split('=')[1];
|
|
|
|
|
|
|
|
|
|
if (!selected && !cookieLocale) setShow(true);
|
2025-06-30 13:45:33 +03:30
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const changeLocale = (locale: TLocales) => {
|
|
|
|
|
localStorage.setItem('selectedLocale', locale);
|
2026-07-19 07:59:16 +03:30
|
|
|
document.cookie = `locale=${locale}; path=/; max-age=31536000; samesite=lax`;
|
2025-06-30 17:06:27 +03:30
|
|
|
const path = window.location.pathname.replace(/^\/(en|fa)/, '');
|
|
|
|
|
window.location.pathname = `/${locale}${path}`;
|
2025-06-30 13:45:33 +03:30
|
|
|
};
|
2025-06-30 17:06:27 +03:30
|
|
|
const closeModal = () => setShow(false);
|
2025-06-30 13:45:33 +03:30
|
|
|
|
2025-06-30 17:06:27 +03:30
|
|
|
if (!show) return null;
|
2025-06-30 13:45:33 +03:30
|
|
|
|
|
|
|
|
return (
|
2025-06-30 17:06:27 +03:30
|
|
|
<div
|
|
|
|
|
className="fixed inset-0 z-100 flex items-center justify-center bg-black/50 backdrop-blur-sm"
|
|
|
|
|
onClick={closeModal}
|
|
|
|
|
>
|
|
|
|
|
<div className="relative h-auto w-100 rounded-3xl bg-white p-6 shadow-2xl">
|
|
|
|
|
<div className="flex justify-around gap-4">
|
2025-06-30 13:45:33 +03:30
|
|
|
{languages.map((lang) => (
|
2025-06-30 17:06:27 +03:30
|
|
|
<div
|
2025-06-30 13:45:33 +03:30
|
|
|
key={lang.locale}
|
|
|
|
|
onClick={() => changeLocale(lang.locale as TLocales)}
|
2025-06-30 17:06:27 +03:30
|
|
|
className="flex w-32 flex-col items-center justify-between gap-2 rounded-xl border border-gray-300 bg-gray-50 px-4 py-3 text-sm font-medium text-gray-700 shadow-sm transition-all hover:border-blue-400 hover:bg-blue-100"
|
2025-06-30 13:45:33 +03:30
|
|
|
>
|
2025-06-30 17:06:27 +03:30
|
|
|
<span className="text-2xl">
|
|
|
|
|
<Image src={lang.image} alt={lang.title} className="w-100" />
|
|
|
|
|
</span>
|
|
|
|
|
{lang.title}
|
|
|
|
|
</div>
|
2025-06-30 13:45:33 +03:30
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|