fix language switch for http protocol

This commit is contained in:
smanylov
2026-01-08 15:53:12 +07:00
parent 0e35c5e601
commit 3c0fea3eb0
+32 -6
View File
@@ -7,15 +7,41 @@ import { useState, useRef, useEffect } from 'react'
import { useClickOutside } from '@/app/hooks/useClickOutside'; import { useClickOutside } from '@/app/hooks/useClickOutside';
export default function LanguageSwitcher() { export default function LanguageSwitcher() {
const locale = useLocale() const locale = useLocale();
const router = useRouter() const router = useRouter();
const pathname = usePathname() const pathname = usePathname();
const [isOpen, setIsOpen] = useState(false) const [isOpen, setIsOpen] = useState(false);
const dropdownRef = useRef<HTMLDivElement>(null) const dropdownRef = useRef<HTMLDivElement>(null);
const switchLanguage = (newLocale: string) => { const switchLanguage = (newLocale: string) => {
const currentLang = document.documentElement.getAttribute('lang') || 'ru';
router.replace(pathname, { locale: newLocale });
setIsOpen(false);
const observer = new MutationObserver((mutations) => {
const newLang = document.documentElement.getAttribute('lang')
if (newLang && newLang !== currentLang) {
observer.disconnect();
if (window.location.protocol === 'http:' && process.env.NODE_ENV !== 'development') {
setTimeout(() => window.location.reload(), 100)
}
}
})
if (window.location.protocol === 'http:' && process.env.NODE_ENV !== 'development') {
observer.observe(document.documentElement, {
attributes: true,
attributeFilter: ['lang', 'dir']
})
}
router.replace(pathname, { locale: newLocale }) router.replace(pathname, { locale: newLocale })
setIsOpen(false)
setTimeout(() => {
observer.disconnect();
}, 3000)
} }
useClickOutside(dropdownRef, () => { useClickOutside(dropdownRef, () => {