mirror of
https://github.com/pezkuwichain/pezkuwi-telegram-miniapp.git
synced 2026-04-21 23:37:55 +00:00
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.
This commit is contained in:
+1
-1
@@ -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",
|
||||
|
||||
@@ -14,6 +14,9 @@ const translations: Record<LanguageCode, Translations> = { 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';
|
||||
}
|
||||
|
||||
|
||||
+17
-5
@@ -68,9 +68,9 @@ const TG_LANG_MAP: Record<string, LanguageCode> = {
|
||||
};
|
||||
|
||||
/**
|
||||
* 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
|
||||
|
||||
+3
-3
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user