mirror of
https://github.com/pezkuwichain/pezkuwi-telegram-miniapp.git
synced 2026-04-21 23:37:55 +00:00
fix: prioritize URL path over localStorage for language detection
Bot keyboard sends language via URL path (/tr, /ar, etc.). Previously localStorage was checked first, so once a user had 'en' saved, clicking a different language button in the bot had no effect. Now URL path always wins over localStorage.
This commit is contained in:
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "pezkuwi-telegram-miniapp",
|
||||
"version": "1.0.222",
|
||||
"version": "1.0.224",
|
||||
"type": "module",
|
||||
"description": "Pezkuwichain Telegram Mini App - Forum, Announcements, Rewards",
|
||||
"author": "Pezkuwichain Team",
|
||||
|
||||
+16
-10
@@ -68,26 +68,32 @@ const TG_LANG_MAP: Record<string, LanguageCode> = {
|
||||
};
|
||||
|
||||
/**
|
||||
* 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
|
||||
* Detect language priority:
|
||||
* 1. URL path (bot sends /tr, /ar — always wins, user just clicked a language button)
|
||||
* 2. localStorage (persisted preference from previous session)
|
||||
* 3. Telegram WebApp user language (fallback)
|
||||
* 4. Default (krd)
|
||||
*
|
||||
* URL path is consumed once, 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)
|
||||
const stored = localStorage.getItem('app_language');
|
||||
if (stored && VALID_LANGS.includes(stored as LanguageCode)) {
|
||||
return stored as LanguageCode;
|
||||
}
|
||||
|
||||
// 2. Check URL path (bot sends /tr, /ar, /en etc.)
|
||||
// 1. Check URL path FIRST (bot sends /tr, /ar, /en etc.)
|
||||
// This takes priority over localStorage because it represents the user's
|
||||
// active language choice from the bot keyboard.
|
||||
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;
|
||||
}
|
||||
|
||||
// 2. Check localStorage (persisted user preference from previous session)
|
||||
const stored = localStorage.getItem('app_language');
|
||||
if (stored && VALID_LANGS.includes(stored as LanguageCode)) {
|
||||
return stored as LanguageCode;
|
||||
}
|
||||
|
||||
// 3. Check Telegram WebApp user language
|
||||
try {
|
||||
const tg = (
|
||||
|
||||
+3
-3
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"version": "1.0.222",
|
||||
"buildTime": "2026-02-25T16:16:47.708Z",
|
||||
"buildNumber": 1772036207713
|
||||
"version": "1.0.224",
|
||||
"buildTime": "2026-02-25T16:46:10.349Z",
|
||||
"buildNumber": 1772037970349
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user