Course contentsShow
← Web Development
Lesson 1505 of 2,98227. REST API Design for FrontendFree lesson

Accept-Language for Internationalization

Send Accept-Language headers to request localized content from internationalized APIs.

Accept-Language for Internationalization

What you'll learn: How to request content in your preferred language from REST APIs using the Accept-Language header.

The Core Idea

When a REST API supports multiple languages (internationalization, often abbreviated as i18n), you can tell it which language you prefer by sending an Accept-Language header with your request. The server then tries to respond with content in that language.

Think of it like walking into an international hotel: you tell the front desk "I speak Spanish and English" (in order of preference), and they'll try to help you in Spanish first, falling back to English if Spanish isn't available.

How Accept-Language Works

The Accept-Language header contains one or more language codes, optionally with quality values (0 to 1) indicating preference:

Accept-Language: es-MX, es;q=0.9, en;q=0.8, fr;q=0.5

This says: "I prefer Mexican Spanish, then general Spanish (90% preference), then English (80%), then French (50%)."

More common patterns:

Accept-Language: en-US
Accept-Language: fr-FR, fr;q=0.9, en;q=0.8

Sending with Fetch

Using the headers you learned earlier:

fetch('https://api.example.com/products/123', {
  headers: {
    'Accept-Language': 'de-DE, de;q=0.9, en;q=0.7'
  }
})
.then(response => response.json())
.then(data => console.log(data)); // German product details

The API should return localized content—product names, descriptions, error messages—in German if available.

Fallback Behavior

Well-designed APIs provide a default language when your requested language isn't available. The server might also tell you which language it used via a Content-Language response header.

Key Takeaway: Use the Accept-Language header to request localized content from internationalized APIs, allowing users to receive responses in their preferred language.