mirror of
https://github.com/pezkuwichain/pezkuwi-telegram-miniapp.git
synced 2026-04-22 03:07: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",
|
"name": "pezkuwi-telegram-miniapp",
|
||||||
"version": "1.0.222",
|
"version": "1.0.224",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"description": "Pezkuwichain Telegram Mini App - Forum, Announcements, Rewards",
|
"description": "Pezkuwichain Telegram Mini App - Forum, Announcements, Rewards",
|
||||||
"author": "Pezkuwichain Team",
|
"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.
|
* Detect language priority:
|
||||||
* URL path is consumed once and persisted to localStorage, then cleaned from the URL
|
* 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.
|
* to prevent Telegram WebView cache issues with #tgWebAppData hash.
|
||||||
*/
|
*/
|
||||||
function detectLanguage(): LanguageCode {
|
function detectLanguage(): LanguageCode {
|
||||||
// 1. Check localStorage (persisted user preference)
|
// 1. Check URL path FIRST (bot sends /tr, /ar, /en etc.)
|
||||||
const stored = localStorage.getItem('app_language');
|
// This takes priority over localStorage because it represents the user's
|
||||||
if (stored && VALID_LANGS.includes(stored as LanguageCode)) {
|
// active language choice from the bot keyboard.
|
||||||
return stored as LanguageCode;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 2. Check URL path (bot sends /tr, /ar, /en etc.)
|
|
||||||
const firstSegment = window.location.pathname.split('/').filter(Boolean)[0];
|
const firstSegment = window.location.pathname.split('/').filter(Boolean)[0];
|
||||||
if (firstSegment && VALID_LANGS.includes(firstSegment as LanguageCode)) {
|
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);
|
localStorage.setItem('app_language', firstSegment);
|
||||||
window.history.replaceState(null, '', '/' + window.location.search + window.location.hash);
|
window.history.replaceState(null, '', '/' + window.location.search + window.location.hash);
|
||||||
return firstSegment as LanguageCode;
|
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
|
// 3. Check Telegram WebApp user language
|
||||||
try {
|
try {
|
||||||
const tg = (
|
const tg = (
|
||||||
|
|||||||
+3
-3
@@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"version": "1.0.222",
|
"version": "1.0.224",
|
||||||
"buildTime": "2026-02-25T16:16:47.708Z",
|
"buildTime": "2026-02-25T16:46:10.349Z",
|
||||||
"buildNumber": 1772036207713
|
"buildNumber": 1772037970349
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user