Handling Unsupported Locales
You'll learn how to gracefully handle locale requests your application doesn't support, ensuring users always see content in a usable language.
The Problem
Even with the best locale detection strategy (using Accept-Language, navigator.language, URL patterns, cookies, or localStorage), users will sometimes request languages your application hasn't been translated into. Without a plan, this leads to broken content, missing translations, or confusing mixed-language interfaces.
Graceful Degradation
Graceful degradation means falling back to a supported locale rather than failing. Think of it like a restaurant menu: if they don't have your first choice, you don't leave hungry—you order something similar instead.
Your fallback strategy should prioritize the closest match:
- Exact match:
en-US→en-US✓ - Language-only fallback:
en-GB→en-US(same language, different region) - Default locale:
zh-CN→en-US(unsupported language → your app's default)
Implementation Pattern
When processing a user's preferred locale:
const supportedLocales = ['en-US', 'es-ES', 'fr-FR'];
const userPreference = 'en-CA'; // From detection chain
function resolveLocale(requested, supported, defaultLocale = 'en-US') {
// Try exact match
if (supported.includes(requested)) return requested;
// Try language-only match (en-CA → en-US)
const language = requested.split('-')[0];
const languageMatch = supported.find(loc => loc.startsWith(language));
if (languageMatch) return languageMatch;
// Fall back to default
return defaultLocale;
}
const activeLocale = resolveLocale(userPreference, supportedLocales);
// Result: 'en-US' (graceful fallback from en-CA)
Always log unsupported locale requests—they're valuable data for prioritizing future translations.
Key Takeaway: Never leave users with broken content; implement a clear fallback chain that tries exact match → language match → default locale, ensuring someone always sees functional, translated content.