|
| 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>VoiceCraft Documentation</title> |
| 7 | + <meta name="robots" content="noindex" /> |
| 8 | + <script> |
| 9 | + (async () => { |
| 10 | + const COOKIE_KEY = 'voicecraft-docs-locale' |
| 11 | + const FALLBACK_LOCALE = 'en' |
| 12 | + |
| 13 | + const readCookie = (key) => { |
| 14 | + const cookie = document.cookie |
| 15 | + .split('; ') |
| 16 | + .find((item) => item.startsWith(key + '=')) |
| 17 | + return cookie ? decodeURIComponent(cookie.split('=')[1]) : '' |
| 18 | + } |
| 19 | + |
| 20 | + const expandLocale = (value) => { |
| 21 | + if (!value) return [] |
| 22 | + const normalized = value.toLowerCase().replace('_', '-') |
| 23 | + const [base] = normalized.split('-') |
| 24 | + return [normalized, base].filter(Boolean) |
| 25 | + } |
| 26 | + |
| 27 | + const localeCandidates = [] |
| 28 | + const seen = new Set() |
| 29 | + |
| 30 | + const pushCandidate = (value) => { |
| 31 | + for (const candidate of expandLocale(value)) { |
| 32 | + if (!seen.has(candidate)) { |
| 33 | + seen.add(candidate) |
| 34 | + localeCandidates.push(candidate) |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + pushCandidate(readCookie(COOKIE_KEY)) |
| 40 | + |
| 41 | + const browserLocales = Array.isArray(navigator.languages) |
| 42 | + ? navigator.languages |
| 43 | + : [navigator.language] |
| 44 | + |
| 45 | + for (const locale of browserLocales) { |
| 46 | + pushCandidate(locale) |
| 47 | + } |
| 48 | + |
| 49 | + pushCandidate(FALLBACK_LOCALE) |
| 50 | + |
| 51 | + const detectExistingLocale = async () => { |
| 52 | + for (const locale of localeCandidates) { |
| 53 | + try { |
| 54 | + const response = await fetch(`/${locale}/`, { |
| 55 | + method: 'HEAD', |
| 56 | + cache: 'no-store', |
| 57 | + }) |
| 58 | + |
| 59 | + if (response.ok || (response.status >= 300 && response.status < 400)) { |
| 60 | + return locale |
| 61 | + } |
| 62 | + } catch { |
| 63 | + // ignore network/head probe errors |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + return FALLBACK_LOCALE |
| 68 | + } |
| 69 | + |
| 70 | + const locale = await detectExistingLocale() |
| 71 | + window.location.replace(`/${locale}`) |
| 72 | + })() |
| 73 | + </script> |
| 74 | + </head> |
| 75 | + <body></body> |
| 76 | +</html> |
0 commit comments