Curriculum Series

How do you serve content in multiple languages?

How do you serve content in multiple languages?

How do you serve content in multiple languages?

HTML

The short answer

Use a combination of the HTTP Accept-Language header (to detect the user's preferred language), URL-based language routing (like /en/ or /es/), translation files with a library like i18next, and proper HTML attributes (lang, hreflang). Let users switch languages manually — never rely only on automatic detection.

Detecting the user's language

From the HTTP header:

javascript
// Server-side (Express)
app.get('/', (req, res) => {
const language = req.headers['accept-language']; // "en-US,en;q=0.9,es;q=0.8"
const preferred = language.split(',')[0].split('-')[0]; // "en"
res.redirect(`/${preferred}/`);
});

From the browser:

javascript
const language = navigator.language; // "en-US"
const languages = navigator.languages; // ["en-US", "en", "es"]

Translation files

Store translations as key-value pairs:

javascript
// en.json
{ "greeting": "Hello", "cart": "Shopping Cart", "checkout": "Checkout" }
// es.json
{ "greeting": "Hola", "cart": "Carrito de Compras", "checkout": "Pagar" }

Use a library like i18next to load and display them:

javascript
const { t } = useTranslation();
return <h1>{t('greeting')}</h1>; // "Hello" or "Hola"

Important HTTP headers

Content-Language — tells the browser what language the response is in:

javascript
Content-Language: es

hreflang — tells search engines about alternate language versions (covered in detail in the multilingual site concerns question).

Best practices

  • Always provide a manual language switcher — do not rely only on auto-detection
  • Handle pluralization rules (different per language)
  • Test with actual translations, not just English in both slots

Interview Tip

Cover: language detection (Accept-Language header), URL structure (subdirectories), translation files, and SEO (hreflang). The most important point is to always let users switch manually — automatic detection is a starting point, not the final answer.

Why interviewers ask this

This tests practical knowledge of internationalization. Companies with global users need developers who can build multilingual experiences correctly, from server-side detection to client-side rendering to SEO.

Finished reading?

Mark this topic as solved to track your progress.