|
6 | 6 | <title>Language Redirect</title>
|
7 | 7 | <script>
|
8 | 8 | function getCookie(name) {
|
| 9 | + console.log('getCookie function run!!'); |
9 | 10 | const value = `; ${document.cookie}`;
|
10 | 11 | const parts = value.split(`; ${name}=`);
|
11 | 12 | if (parts.length === 2) return parts.pop().split(';').shift();
|
12 | 13 | }
|
13 | 14 |
|
14 | 15 | function setCookie(name, value, days) {
|
| 16 | + console.log('setCookie function run!!'); |
15 | 17 | const date = new Date();
|
16 | 18 | date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
|
17 | 19 | document.cookie = `${name}=${value};path=/;expires=${date.toUTCString()};SameSite=Lax`;
|
18 |
| - |
19 |
| - console.log(document.cookie) |
20 | 20 | }
|
21 | 21 |
|
22 | 22 | async function detectAndRedirect() {
|
|
33 | 33 | const response = await fetch('https://ipapi.co/json/');
|
34 | 34 | const data = await response.json();
|
35 | 35 | const country = data.country; // e.g., "IR" for Iran
|
36 |
| - |
| 36 | + |
37 | 37 | // Determine language based on location or browser language
|
38 | 38 | let lang = 'en'; // Default language
|
39 | 39 | if (country === "IR") {
|
|
44 | 44 | }
|
45 | 45 |
|
46 | 46 | // Save the language in a cookie
|
47 |
| - await setCookie('userLang', lang, 365); // Save for 1 year |
48 |
| - |
| 47 | + setCookie('userLang', lang, 365); // Save for 1 year |
| 48 | + |
49 | 49 | // Redirect to the appropriate language
|
50 | 50 | window.location.href = `/docs/${lang}/guides/`;
|
51 | 51 | } catch (error) {
|
52 | 52 | console.error("Error detecting location or language:", error);
|
53 | 53 | // Default to English if there's an error
|
54 |
| - window.location.href = `/docs/en/guides/`; |
| 54 | + if(Intl.DateTimeFormat().resolvedOptions().timeZone === 'Asia/Tehran'){ |
| 55 | + await setCookie('userLang', 'fa', 365); // Save for 1 year |
| 56 | + window.location.href = `/docs/fa/guides/`; |
| 57 | + }else{ |
| 58 | + await setCookie('userLang', 'en', 365); // Save for 1 year |
| 59 | + window.location.href = `/docs/en/guides/`; |
| 60 | + } |
55 | 61 | }
|
56 | 62 | }
|
57 | 63 |
|
|
0 commit comments