|
1 | | -const userLang = navigator.language.startsWith("ko") |
2 | | - ? "ko" |
3 | | - : navigator.language.startsWith("ja") |
4 | | - ? "ja" |
5 | | - : navigator.language.startsWith("zh") |
6 | | - ? "zh" |
7 | | - : navigator.language.startsWith("es") |
8 | | - ? "es" |
9 | | - : navigator.language.startsWith("fr") |
10 | | - ? "fr" |
11 | | - : "en" |
| 1 | +const userLang = navigator.language.split('-')[0] |
| 2 | +const currentLang = localStorage.getItem('lang') || userLang |
12 | 3 |
|
13 | | -const currentLang = localStorage.getItem("lang") ?? userLang |
14 | | -document.documentElement.setAttribute("lang", currentLang) |
15 | | - |
16 | | -const emitLangChangeEvent = (lang: string) => { |
17 | | - const event = new CustomEvent("langchange", { |
18 | | - detail: { lang }, |
19 | | - }) |
20 | | - document.dispatchEvent(event) |
| 4 | +function getCurrentPathWithoutLang() { |
| 5 | + const path = window.location.pathname |
| 6 | + const parts = path.split('/') |
| 7 | + if (parts[1] === 'i18n') { |
| 8 | + parts.splice(1, 2) |
| 9 | + } |
| 10 | + return parts.join('/').replace(/^\/+/, '') |
21 | 11 | } |
22 | 12 |
|
23 | | -const getCurrentPathWithoutLang = () => { |
24 | | - const path = window.location.pathname |
25 | | - const pathParts = path.split("/").filter((part) => part) |
26 | | - |
27 | | - if (pathParts[0] === "i18n") { |
28 | | - pathParts.splice(0, 2) |
29 | | - } |
30 | | - return pathParts.join("/") |
| 13 | +function navigateToUrl(url: string) { |
| 14 | + window.location.href = url |
31 | 15 | } |
32 | 16 |
|
33 | | -const navigateToUrl = async (url) => { |
34 | | - try { |
35 | | - const response = await fetch(url, { method: "HEAD" }) |
36 | | - if (response.status === 404) { |
37 | | - window.location.href = "/translate" |
38 | | - } else { |
39 | | - window.location.href = url |
40 | | - } |
41 | | - } catch (error) { |
42 | | - console.error("Failed to check URL status:", error) |
43 | | - window.location.href = "/translate" |
44 | | - } |
| 17 | +function emitLangChangeEvent(lang: string) { |
| 18 | + const event = new CustomEvent('langchange', { detail: { lang } }) |
| 19 | + document.dispatchEvent(event) |
45 | 20 | } |
46 | 21 |
|
47 | 22 | document.addEventListener("nav", () => { |
48 | | - const switchLang = async (e: Event) => { |
49 | | - const newLang = (e.target as HTMLSelectElement)?.value |
50 | | - document.documentElement.setAttribute("lang", newLang) |
51 | | - localStorage.setItem("lang", newLang) |
52 | | - emitLangChangeEvent(newLang) |
| 23 | + const switchLang = async (e: Event) => { |
| 24 | + const button = e.target as HTMLButtonElement |
| 25 | + const newLang = button.dataset.lang |
| 26 | + if (!newLang) return |
53 | 27 |
|
54 | | - const currentPath = getCurrentPathWithoutLang() |
55 | | - const newUrl = newLang === "en" ? `/${currentPath}` : `/i18n/${newLang}/${currentPath}` |
| 28 | + document.documentElement.setAttribute("lang", newLang) |
| 29 | + localStorage.setItem("lang", newLang) |
| 30 | + emitLangChangeEvent(newLang) |
56 | 31 |
|
57 | | - await navigateToUrl(newUrl) |
58 | | - } |
| 32 | + const currentPath = getCurrentPathWithoutLang() |
| 33 | + const newUrl = newLang === "en" ? `/${currentPath}` : `/i18n/${newLang}/${currentPath}` |
59 | 34 |
|
60 | | - const langSelect = document.querySelector("#language-select") as HTMLSelectElement |
61 | | - langSelect.addEventListener("change", switchLang) |
62 | | - window.addCleanup(() => langSelect.removeEventListener("change", switchLang)) |
63 | | - if (currentLang) { |
64 | | - langSelect.value = currentLang |
65 | | - } |
| 35 | + await navigateToUrl(newUrl) |
| 36 | + } |
| 37 | + |
| 38 | + const langButtons = document.querySelectorAll(".lang-button") |
| 39 | + langButtons.forEach(button => { |
| 40 | + button.addEventListener("click", switchLang) |
| 41 | + window.addCleanup(() => button.removeEventListener("click", switchLang)) |
| 42 | + }) |
66 | 43 | }) |
67 | 44 |
|
68 | 45 | document.addEventListener("DOMContentLoaded", () => { |
69 | | - const lang = localStorage.getItem("lang") ?? userLang |
70 | | - document.documentElement.setAttribute("lang", lang) |
71 | | - emitLangChangeEvent(lang) |
| 46 | + const lang = localStorage.getItem("lang") ?? userLang |
| 47 | + document.documentElement.setAttribute("lang", lang) |
| 48 | + emitLangChangeEvent(lang) |
| 49 | + |
| 50 | + // Update active button state |
| 51 | + const langButtons = document.querySelectorAll(".lang-button") |
| 52 | + langButtons.forEach(button => { |
| 53 | + if (button instanceof HTMLButtonElement) { |
| 54 | + button.classList.toggle('active', button.dataset.lang === lang) |
| 55 | + } |
| 56 | + }) |
72 | 57 | }) |
73 | 58 |
|
74 | 59 | document.addEventListener("langchange", (event) => { |
|
0 commit comments