From 8cbf0b0f2610c28d0c3720231b2310105e971c55 Mon Sep 17 00:00:00 2001 From: Michael Czechowski Date: Thu, 15 Jan 2026 17:47:20 +0100 Subject: [PATCH] feat: add URL-based language switching (#de, #pl, #es, #ar, #uk) - Visit #de to switch to German and go to home - Visit #pl for Polish, #es for Spanish, #ar for Arabic, #uk for Ukrainian - Language is persisted to localStorage - URL is cleaned up after switching (hash removed) --- src/app.js | 7 +++++++ src/helpers/router.js | 14 +++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/app.js b/src/app.js index 15e4d2e..3890392 100644 --- a/src/app.js +++ b/src/app.js @@ -1937,6 +1937,13 @@ function handleRoute(shouldUpdateUrl = true) { case RouteType.LESSON: navigateToLesson(route.moduleId, route.lessonIndex, shouldUpdateUrl); break; + case RouteType.LANGUAGE: + // Switch language and redirect to home + setLanguage(route.lang); + applyTranslations(); + history.replaceState(null, "", window.location.pathname); + showLandingPage(); + return; // Skip updateNavHighlight/updatePageMeta since we're redirecting default: showLandingPage(); } diff --git a/src/helpers/router.js b/src/helpers/router.js index 2dfae9e..4b54b4e 100644 --- a/src/helpers/router.js +++ b/src/helpers/router.js @@ -4,6 +4,7 @@ * * Route formats: * - # -> Home landing page + * - #de, #pl, #ar -> Switch language and go to home * - #css -> CSS section landing * - #html -> HTML section landing * - #tailwind -> Tailwind section landing @@ -18,7 +19,8 @@ export const RouteType = { HOME: "home", SECTION: "section", REFERENCE: "reference", - LESSON: "lesson" + LESSON: "lesson", + LANGUAGE: "language" }; /** @@ -26,6 +28,11 @@ export const RouteType = { */ const SECTIONS = ["css", "html", "tailwind"]; +/** + * Valid language codes for URL-based switching + */ +const LANGUAGES = ["en", "de", "pl", "es", "ar", "uk"]; + /** * Parse current URL hash into route info * @returns {{ type: string, moduleId?: string, lessonIndex?: number, sectionId?: string, refId?: string } | null} @@ -44,6 +51,11 @@ export function parseHash() { if (parts.length === 1) { const segment = parts[0]; + // Language switching (e.g., #de, #pl, #ar) + if (LANGUAGES.includes(segment)) { + return { type: RouteType.LANGUAGE, lang: segment }; + } + // Section landing pages if (SECTIONS.includes(segment)) { return { type: RouteType.SECTION, sectionId: segment };