Language Detection: Dynamic Multilingual Support
This article explains how we automatically detect and serve content in the user's preferred language.
The Problem: Serving Global Users
Users come from different countries speaking different languages:
-
India: Hindi, Bengali, Tamil, Telugu, Gujarati, Marathi, Kannada, Malayalam, Punjabi, Urdu
-
Europe: German, French, Spanish, Italian, Portuguese, Russian
-
Asia: Japanese, Korean, Chinese
We need to detect their language preference and serve translated content automatically.
Detection Priority
We check multiple sources in order:
1. URL parameter (?lang=hi)
2. Session (stored after first detection)
3. Cookie (translation_lang or lang)
4. Browser Accept-Language header (future)
5. Default (English)
Why This Order?
URL parameter first: Allows explicit language switching
Session second: Persists during browsing session
Cookie third: Persists across sessions (30 days)
Default last: Fallback for new users
Implementation
def get_locale():
# 1. Check URL parameter
lang_param = request.args.get("lang")
if lang_param in valid_langs:
return lang_param
# 2. Check session
if "translation_lang" in session:
return session["translation_lang"]
# 3. Check cookie
# ... (implementation details omitted)
Language Switching
Users can switch language via URL:
/p/Treo-N100-8-256?lang=hi
This triggers:
1. Set session: session["translation_lang"] = "hi"
2. Set cookie: translation_lang=hi; Max-Age=2592000 (30 days)
3. Redirect: Remove lang parameter from URL
Benefit: Clean URLs after language selection
Supported Languages
We support 20 languages:
Indian languages (10):
-
Hindi (hi)
-
Bengali (bn)
-
Tamil (ta)
-
Telugu (te)
-
Gujarati (gu)
-
Marathi (mr)
-
Kannada (kn)
-
Malayalam (ml)
-
Punjabi (pa)
-
Urdu (ur)
European languages (7):
-
German (de)
-
French (fr)
-
Spanish (es)
-
Italian (it)
-
Portuguese (pt)
-
Russian (ru)
Asian languages (3):
-
Japanese (ja)
-
Korean (ko)
-
Chinese (zh)
Content Translation
Different content types use different translation methods:
Static Templates (Flask-Babel)
Navigation, buttons, labels translated via .po files:
{{ _('Add to Cart') }} → "कार्ट में जोड़ें" (Hindi)
Dynamic Content (TranslationManager)
Product names, features translated via phrase tables:
"Mini PC" → "मिनी पीसी" (Hindi)
Long Content (DeepSeek AI)
Product descriptions, articles translated via AI:
"Compact office computer..." → "कॉम्पैक्ट ऑफिस कंप्यूटर..." (Hindi)
See: Translation System for details
Regional Formatting
Language selection also affects formatting:
Number Format
English: 1,234.56
European: 1.234,56 (reversed separators)
Indian: 1,23,456 (lakhs/crores grouping)
Date Format
English: Feb 18, 2026
European: 18.02.2026
Indian: 18/02/2026
Currency
English: ₹1,234
European: €1.234
International: $1,234
Language Selector UI
Users can switch language via dropdown:
Location: Top navigation bar
Display: Flag icon + language name
Behavior: Reload page with ?lang=<code>
SEO Considerations
URL Structure
We use single URL for all languages (no /hi/, /de/ prefixes):
Benefit: Simpler architecture, no duplicate content issues
Trade-off: Less SEO value per language
Hreflang Tags
We add alternate language links:
<link rel="alternate" hreflang="en" href="https://www.thinvent.in/p/Treo-N100">
<link rel="alternate" hreflang="hi" href="https://www.thinvent.in/p/Treo-N100?lang=hi">
<link rel="alternate" hreflang="de" href="https://www.thinvent.in/p/Treo-N100?lang=de">
Benefit: Google knows which language version to show
Fallback Strategy
If translation missing for selected language:
1. Try English: Most content has English version
2. Show original: Better than nothing
3. Queue for translation: Add to translation queue for batch processing
Performance
Caching
Translations are cached in memory:
Phrase tables: Loaded once at startup
AI translations: Cached in database
Benefit: No translation overhead per request
Lazy Loading
Translations loaded only when needed:
Product descriptions: Loaded on product page view
Query page content: Loaded on query page view
Benefit: Faster initial page load
References
Technical Concepts
-
Internationalization (i18n) - Wikipedia
-
Content negotiation - Wikipedia
Libraries
-
Flask-Babel - Template translation
-
Babel - Internationalization library
Related Articles
-
Translation System - Three-technology hybrid approach
-
Content AI Generation - DeepSeek translations
Summary
Language detection uses a priority system:
Detection order:
-
✅ URL parameter (
?lang=hi) -
✅ Session (during browsing)
-
✅ Cookie (30 days)
-
✅ Default (English)
Supported:
-
✅ 20 languages (10 Indian, 7 European, 3 Asian)
-
✅ Regional formatting (numbers, dates, currency)
-
✅ SEO hreflang tags
Translation methods:
-
✅ Flask-Babel (static templates)
-
✅ TranslationManager (product names, features)
-
✅ DeepSeek AI (descriptions, articles)
Performance:
-
✅ In-memory caching
-
✅ Lazy loading
-
✅ Database caching for AI translations
This enables automatic language detection and seamless multilingual experience.