Course contentsShow
← Web Development
Lesson 2498 of 2,98245. InternationalizationFree lesson

The navigator.language and navigator.languages APIs

Access user language preferences in JavaScript using browser-provided language detection APIs.

The navigator.language and navigator.languages APIs

You'll learn how to detect a user's preferred language(s) directly in the browser using JavaScript's built-in language detection APIs.

What These APIs Do

While the Accept-Language header tells servers about language preferences, client-side JavaScript can access the same information through the browser's navigator object. These APIs reveal what language(s) the user has configured in their browser settings, allowing you to adapt your interface without a server round-trip.

Think of navigator.language and navigator.languages as reading the "preferred languages" list from your browser's settings panel — the same list that influences what languages websites and applications should display by default.

The Two APIs

navigator.language returns a single string representing the user's top language preference — their most preferred locale. This is typically formatted as a language tag like "en-US", "fr-CA", or "ja".

navigator.languages returns a read-only array of language tags, ordered by user preference. The first item matches navigator.language, and subsequent entries reflect fallback preferences the user has configured.

// Check user's primary language
console.log(navigator.language); // e.g., "es-MX"

// Check all preferred languages in order
console.log(navigator.languages);
// e.g., ["es-MX", "es", "en-US", "en"]

When to Use Them

These APIs shine when you need immediate, client-side language detection — perhaps to load the right translation file, format dates/numbers appropriately, or select default content before any server communication occurs. They're especially valuable in single-page applications or progressive web apps that handle internationalization entirely in JavaScript.

Key Takeaway: Use navigator.language for the user's top language choice and navigator.languages to understand their full preference hierarchy, enabling respectful, user-centric localization directly in your JavaScript code.