|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta charset="UTF-8"> |
| 5 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 6 | + <title>Language Redirect</title> |
| 7 | + <script> |
| 8 | + function getCookie(name) { |
| 9 | + const value = `; ${document.cookie}`; |
| 10 | + const parts = value.split(`; ${name}=`); |
| 11 | + if (parts.length === 2) return parts.pop().split(';').shift(); |
| 12 | + } |
| 13 | + |
| 14 | + function setCookie(name, value, days) { |
| 15 | + const date = new Date(); |
| 16 | + date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000); |
| 17 | + document.cookie = `${name}=${value};path=/;expires=${date.toUTCString()}`; |
| 18 | + } |
| 19 | + |
| 20 | + async function detectAndRedirect() { |
| 21 | + try { |
| 22 | + // Check if a userLang cookie already exists |
| 23 | + const userLang = getCookie('userLang'); |
| 24 | + if (userLang) { |
| 25 | + // Redirect based on saved language preference |
| 26 | + window.location.href = `/docs/${userLang}/guides/`; |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + // Use GeoIP API to detect location |
| 31 | + const response = await fetch('https://ipapi.co/json/'); |
| 32 | + const data = await response.json(); |
| 33 | + const country = data.country; // e.g., "IR" for Iran |
| 34 | + |
| 35 | + // Determine language based on location or browser language |
| 36 | + let lang = 'en'; // Default language |
| 37 | + if (country === "IR") { |
| 38 | + lang = 'fa'; // Set Farsi for Iran |
| 39 | + } else { |
| 40 | + const browserLang = navigator.language || navigator.userLanguage; |
| 41 | + lang = browserLang.startsWith('fa') ? 'fa' : 'en'; |
| 42 | + } |
| 43 | + |
| 44 | + // Save the language in a cookie |
| 45 | + setCookie('userLang', lang, 365); // Save for 1 year |
| 46 | + |
| 47 | + // Redirect to the appropriate language |
| 48 | + window.location.href = `/docs/${lang}/guides/`; |
| 49 | + } catch (error) { |
| 50 | + console.error("Error detecting location or language:", error); |
| 51 | + // Default to English if there's an error |
| 52 | + window.location.href = `/docs/en/guides/`; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + // Run the redirection logic on page load |
| 57 | + window.onload = detectAndRedirect; |
| 58 | + </script> |
| 59 | +</head> |
| 60 | +<body> |
| 61 | + <h1>Redirecting...</h1> |
| 62 | +</body> |
| 63 | +</html> |
0 commit comments