From fad8291c90c1e969b5a639e860ed98580e4007ea Mon Sep 17 00:00:00 2001 From: Kurdistan Tech Ministry Date: Wed, 25 Feb 2026 19:16:47 +0300 Subject: [PATCH] fix: restore URL path language detection for Telegram bot links Bot sends users to /tr, /ar, /en etc. Previous commit removed URL path detection entirely, breaking language selection from bot. Now URL path is checked as secondary source (after localStorage), consumed once, persisted to localStorage, and cleaned from the URL to prevent WebView cache issues. --- package.json | 2 +- src/components/ErrorBoundary.tsx | 3 +++ src/i18n/index.tsx | 22 +++++++++++++++++----- src/version.json | 6 +++--- 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index 5ac87b5..74af6ae 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "pezkuwi-telegram-miniapp", - "version": "1.0.221", + "version": "1.0.222", "type": "module", "description": "Pezkuwichain Telegram Mini App - Forum, Announcements, Rewards", "author": "Pezkuwichain Team", diff --git a/src/components/ErrorBoundary.tsx b/src/components/ErrorBoundary.tsx index 4d8d7ab..09b8826 100644 --- a/src/components/ErrorBoundary.tsx +++ b/src/components/ErrorBoundary.tsx @@ -14,6 +14,9 @@ const translations: Record = { krd, en, tr, ckb, fa, function detectLang(): LanguageCode { const stored = localStorage.getItem('app_language'); if (stored && stored in translations) return stored as LanguageCode; + // Fallback: check URL path (bot link like /tr, /ar) + const seg = window.location.pathname.split('/').filter(Boolean)[0]; + if (seg && seg in translations) return seg as LanguageCode; return 'krd'; } diff --git a/src/i18n/index.tsx b/src/i18n/index.tsx index aa8266d..a7cc3a3 100644 --- a/src/i18n/index.tsx +++ b/src/i18n/index.tsx @@ -68,9 +68,9 @@ const TG_LANG_MAP: Record = { }; /** - * Detect language: localStorage first, then Telegram user language, then default. - * CRITICAL: Never use URL path for language in Telegram MiniApps! - * Telegram WebView caches URL and strips #tgWebAppData hash on next open. + * Detect language: localStorage first, then URL path (bot link), then Telegram user language, then default. + * URL path is consumed once and persisted to localStorage, then cleaned from the URL + * to prevent Telegram WebView cache issues with #tgWebAppData hash. */ function detectLanguage(): LanguageCode { // 1. Check localStorage (persisted user preference) @@ -79,7 +79,16 @@ function detectLanguage(): LanguageCode { return stored as LanguageCode; } - // 2. Check Telegram WebApp user language + // 2. Check URL path (bot sends /tr, /ar, /en etc.) + const firstSegment = window.location.pathname.split('/').filter(Boolean)[0]; + if (firstSegment && VALID_LANGS.includes(firstSegment as LanguageCode)) { + // Persist to localStorage and clean URL path to avoid WebView cache issues + localStorage.setItem('app_language', firstSegment); + window.history.replaceState(null, '', '/' + window.location.search + window.location.hash); + return firstSegment as LanguageCode; + } + + // 3. Check Telegram WebApp user language try { const tg = ( window as unknown as { @@ -89,7 +98,10 @@ function detectLanguage(): LanguageCode { const tgLang = tg?.WebApp?.initDataUnsafe?.user?.language_code; if (tgLang) { const mapped = TG_LANG_MAP[tgLang]; - if (mapped) return mapped; + if (mapped) { + localStorage.setItem('app_language', mapped); + return mapped; + } } } catch { // Telegram WebApp not available diff --git a/src/version.json b/src/version.json index 7ff6f20..d5ead53 100644 --- a/src/version.json +++ b/src/version.json @@ -1,5 +1,5 @@ { - "version": "1.0.221", - "buildTime": "2026-02-23T21:08:35.712Z", - "buildNumber": 1771880915713 + "version": "1.0.222", + "buildTime": "2026-02-25T16:16:47.708Z", + "buildNumber": 1772036207713 }