|
@ -8,91 +8,90 @@ import http from "@/api/http"; |
|
|
import { useTranslation } from "next-i18next"; // Import the useTranslation hook
|
|
|
import { useTranslation } from "next-i18next"; // Import the useTranslation hook
|
|
|
import { useRouter } from "next/router"; |
|
|
import { useRouter } from "next/router"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const data = [ |
|
|
const data = [ |
|
|
{ |
|
|
{ |
|
|
"name": "English", |
|
|
|
|
|
"code": "en" |
|
|
|
|
|
|
|
|
name: "English", |
|
|
|
|
|
code: "en", |
|
|
}, |
|
|
}, |
|
|
{ |
|
|
{ |
|
|
"name": "Spanish", |
|
|
|
|
|
"code": "es" |
|
|
|
|
|
|
|
|
name: "Spanish", |
|
|
|
|
|
code: "es", |
|
|
}, |
|
|
}, |
|
|
{ |
|
|
{ |
|
|
"name": "German", |
|
|
|
|
|
"code": "de" |
|
|
|
|
|
|
|
|
name: "German", |
|
|
|
|
|
code: "de", |
|
|
}, |
|
|
}, |
|
|
{ |
|
|
{ |
|
|
"name": "Uzbek", |
|
|
|
|
|
"code": "uz" |
|
|
|
|
|
|
|
|
name: "Uzbek", |
|
|
|
|
|
code: "uz", |
|
|
}, |
|
|
}, |
|
|
{ |
|
|
{ |
|
|
"name": "Portuguese", |
|
|
|
|
|
"code": "pt" |
|
|
|
|
|
|
|
|
name: "Portuguese", |
|
|
|
|
|
code: "pt", |
|
|
}, |
|
|
}, |
|
|
{ |
|
|
{ |
|
|
"name": "Bengali", |
|
|
|
|
|
"code": "bn" |
|
|
|
|
|
|
|
|
name: "Bengali", |
|
|
|
|
|
code: "bn", |
|
|
}, |
|
|
}, |
|
|
{ |
|
|
{ |
|
|
"name": "Chinese", |
|
|
|
|
|
"code": "zh" |
|
|
|
|
|
|
|
|
name: "Chinese", |
|
|
|
|
|
code: "zh", |
|
|
}, |
|
|
}, |
|
|
{ |
|
|
{ |
|
|
"name": "Azerbaijani", |
|
|
|
|
|
"code": "az" |
|
|
|
|
|
|
|
|
name: "Azerbaijani", |
|
|
|
|
|
code: "az", |
|
|
}, |
|
|
}, |
|
|
{ |
|
|
{ |
|
|
"name": "Urdu", |
|
|
|
|
|
"code": "ur" |
|
|
|
|
|
|
|
|
name: "Urdu", |
|
|
|
|
|
code: "ur", |
|
|
}, |
|
|
}, |
|
|
{ |
|
|
{ |
|
|
"name": "French", |
|
|
|
|
|
"code": "fr" |
|
|
|
|
|
|
|
|
name: "French", |
|
|
|
|
|
code: "fr", |
|
|
}, |
|
|
}, |
|
|
{ |
|
|
{ |
|
|
"name": "Turkish", |
|
|
|
|
|
"code": "tr" |
|
|
|
|
|
|
|
|
name: "Turkish", |
|
|
|
|
|
code: "tr", |
|
|
}, |
|
|
}, |
|
|
{ |
|
|
{ |
|
|
"name": "Indonesian", |
|
|
|
|
|
"code": "id" |
|
|
|
|
|
|
|
|
name: "Indonesian", |
|
|
|
|
|
code: "id", |
|
|
}, |
|
|
}, |
|
|
{ |
|
|
{ |
|
|
"name": "Swahili", |
|
|
|
|
|
"code": "sw" |
|
|
|
|
|
|
|
|
name: "Swahili", |
|
|
|
|
|
code: "sw", |
|
|
}, |
|
|
}, |
|
|
{ |
|
|
{ |
|
|
"name": "Russian", |
|
|
|
|
|
"code": "ru" |
|
|
|
|
|
|
|
|
name: "Russian", |
|
|
|
|
|
code: "ru", |
|
|
}, |
|
|
}, |
|
|
{ |
|
|
{ |
|
|
"name": "Arabic", |
|
|
|
|
|
"code": "ar" |
|
|
|
|
|
|
|
|
name: "Arabic", |
|
|
|
|
|
code: "ar", |
|
|
}, |
|
|
}, |
|
|
{ |
|
|
{ |
|
|
"name": "Tajik", |
|
|
|
|
|
"code": "tg" |
|
|
|
|
|
|
|
|
name: "Tajik", |
|
|
|
|
|
code: "tg", |
|
|
}, |
|
|
}, |
|
|
{ |
|
|
{ |
|
|
"name": "Persian", |
|
|
|
|
|
"code": "fa" |
|
|
|
|
|
|
|
|
name: "Persian", |
|
|
|
|
|
code: "fa", |
|
|
}, |
|
|
}, |
|
|
{ |
|
|
{ |
|
|
"name": "Gujarati", |
|
|
|
|
|
"code": "gu" |
|
|
|
|
|
|
|
|
name: "Gujarati", |
|
|
|
|
|
code: "gu", |
|
|
}, |
|
|
}, |
|
|
{ |
|
|
{ |
|
|
"name": "Kashmiri", |
|
|
|
|
|
"code": "ks" |
|
|
|
|
|
|
|
|
name: "Kashmiri", |
|
|
|
|
|
code: "ks", |
|
|
}, |
|
|
}, |
|
|
{ |
|
|
{ |
|
|
"name": "Hausa", |
|
|
|
|
|
"code": "ha" |
|
|
|
|
|
} |
|
|
|
|
|
] |
|
|
|
|
|
|
|
|
name: "Hausa", |
|
|
|
|
|
code: "ha", |
|
|
|
|
|
}, |
|
|
|
|
|
]; |
|
|
const LanguageSwitcher: React.FC = () => { |
|
|
const LanguageSwitcher: React.FC = () => { |
|
|
const { t } = useTranslation('common'); // Initialize translation hook with 'common' namespace
|
|
|
|
|
|
|
|
|
const { t } = useTranslation("common"); // Initialize translation hook with 'common' namespace
|
|
|
const [isOpen, setIsOpen] = useState(false); |
|
|
const [isOpen, setIsOpen] = useState(false); |
|
|
const { openModal } = useUI(); |
|
|
const { openModal } = useUI(); |
|
|
const router = useRouter(); |
|
|
const router = useRouter(); |
|
@ -102,7 +101,9 @@ const LanguageSwitcher: React.FC = () => { |
|
|
typeof window !== "undefined" ? window.innerWidth : 0 |
|
|
typeof window !== "undefined" ? window.innerWidth : 0 |
|
|
); |
|
|
); |
|
|
|
|
|
|
|
|
const [languages, setLanguages] = useState<{ code: string; name: string }[]>([]); |
|
|
|
|
|
|
|
|
const [languages, setLanguages] = useState<{ code: string; name: string }[]>( |
|
|
|
|
|
[] |
|
|
|
|
|
); |
|
|
const [selectedLanguage, setSelectedLanguage] = useState<string>("en"); |
|
|
const [selectedLanguage, setSelectedLanguage] = useState<string>("en"); |
|
|
const [isLoading, setIsLoading] = useState<boolean>(false); |
|
|
const [isLoading, setIsLoading] = useState<boolean>(false); |
|
|
const [error, setError] = useState<string | null>(null); |
|
|
const [error, setError] = useState<string | null>(null); |
|
@ -130,7 +131,7 @@ const LanguageSwitcher: React.FC = () => { |
|
|
// try {
|
|
|
// try {
|
|
|
// setIsLoading(true);
|
|
|
// setIsLoading(true);
|
|
|
// const response = await http.get("v1/languages/");
|
|
|
// const response = await http.get("v1/languages/");
|
|
|
setLanguages(data); |
|
|
|
|
|
|
|
|
setLanguages(data); |
|
|
// } catch (err) {
|
|
|
// } catch (err) {
|
|
|
// setError(t("error_message")); // Use translation for error message
|
|
|
// setError(t("error_message")); // Use translation for error message
|
|
|
// } finally {
|
|
|
// } finally {
|
|
@ -145,10 +146,22 @@ const LanguageSwitcher: React.FC = () => { |
|
|
} |
|
|
} |
|
|
}, [t]); // Add t as a dependency to ensure translation is updated
|
|
|
}, [t]); // Add t as a dependency to ensure translation is updated
|
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
|
|
const savedLanguage = localStorage.getItem("locale"); |
|
|
|
|
|
console.log(savedLanguage , router.locale , router.locale !== savedLanguage); |
|
|
|
|
|
|
|
|
|
|
|
if (savedLanguage && router.locale && router.locale !== savedLanguage) { |
|
|
|
|
|
selectLanguage(router.locale) |
|
|
|
|
|
} |
|
|
|
|
|
}, [router]); // Only depend on `router.locale`, not the entire `router` object
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
useEffect(() => { |
|
|
const handleResize = () => setWindowWidth(window.innerWidth); |
|
|
const handleResize = () => setWindowWidth(window.innerWidth); |
|
|
const handleClickOutside = (event: MouseEvent) => { |
|
|
const handleClickOutside = (event: MouseEvent) => { |
|
|
if (wrapperRef.current && !wrapperRef.current.contains(event.target as Node)) { |
|
|
|
|
|
|
|
|
if ( |
|
|
|
|
|
wrapperRef.current && |
|
|
|
|
|
!wrapperRef.current.contains(event.target as Node) |
|
|
|
|
|
) { |
|
|
setIsOpen(false); |
|
|
setIsOpen(false); |
|
|
} |
|
|
} |
|
|
}; |
|
|
}; |
|
@ -175,7 +188,9 @@ const LanguageSwitcher: React.FC = () => { |
|
|
<IoGlobeSharp color="black" size={25} className="mr-2" /> |
|
|
<IoGlobeSharp color="black" size={25} className="mr-2" /> |
|
|
{selectedLanguageName} |
|
|
{selectedLanguageName} |
|
|
<span |
|
|
<span |
|
|
className={`ml-2 transform transition-transform duration-200 ${isOpen ? "rotate-180" : "rotate-0"}`} |
|
|
|
|
|
|
|
|
className={`ml-2 transform transition-transform duration-200 ${ |
|
|
|
|
|
isOpen ? "rotate-180" : "rotate-0" |
|
|
|
|
|
}`}
|
|
|
> |
|
|
> |
|
|
<IoIosArrowDown color="black" /> |
|
|
<IoIosArrowDown color="black" /> |
|
|
</span> |
|
|
</span> |
|
@ -200,7 +215,7 @@ const LanguageSwitcher: React.FC = () => { |
|
|
</ul> |
|
|
</ul> |
|
|
)} |
|
|
)} |
|
|
|
|
|
|
|
|
{isLoading && isOpen &&( |
|
|
|
|
|
|
|
|
{isLoading && isOpen && ( |
|
|
<div className="absolute left-0 mt-1 w-full bg-white border border-gray-200 rounded shadow-lg z-50 p-4 text-center"> |
|
|
<div className="absolute left-0 mt-1 w-full bg-white border border-gray-200 rounded shadow-lg z-50 p-4 text-center"> |
|
|
{t("loading")} {/* Use translation for loading text */} |
|
|
{t("loading")} {/* Use translation for loading text */} |
|
|
</div> |
|
|
</div> |
|
|