A page's language is a decision you record, not a guess you make. Read two signals from the same response: the Content-Language header, which states the audience the response was assembled for, and the document's lang attribute, which states the language of the text. When they agree, record that tag. When they disagree, mark the page as a conflict and settle it against a sample of visible text before the content reaches a model. When neither signal is present, record unknown rather than defaulting to English. An hreflang set tells you where other locale versions live, not what language the page in front of you is in.
Two claims, not one
A single response can carry two different statements about language, and they answer different questions.
Content-Language is defined by RFC 9110 as describing "the natural language(s) of the intended audience for the representation", with the explicit warning that this "might not be equivalent to all the languages used within the representation". It is a statement about who the response was assembled for, which is why a beginner's Latin primer written for English readers is correctly tagged en even though its text is Latin.
The lang attribute is defined by the HTML Standard as "the primary language for the element's contents and for any of the element's attributes that contain text". Its value must be a valid BCP 47 language tag or the empty string, and the empty string means the language is explicitly unknown. It also inherits: an element without lang takes the language of its parent, so one wrapper element can relabel an entire subtree.
Neither statement is a measurement. A page can declare a language it does not use, and a server can send one static header for a site with fifty translations. What you get is a claim plus the conditions under which the claim can be checked.
| Signal | What it proves | What it cannot prove |
|---|---|---|
Content-Language header |
The audience the server says the representation targets | That the body text is written in that language |
<html lang> |
The language the author declared for the document's text | That the text was actually written in it |
hreflang alternates |
Which URLs the publisher offers for other locales, and the code each one targets | The language of the page you already fetched |
| A sample of visible text | The language a reader would actually see | A formal tag, or anything hidden or rendered later |
Read the signals in a fixed order
The order matters more than the parser. This function returns one of four decisions and keeps the raw values that produced it:
from html.parser import HTMLParser
from urllib.parse import urljoin
class HeadSignals(HTMLParser):
"""Collect only the language signals a single response can prove."""
def __init__(self, page_url):
super().__init__()
self.page_url = page_url
self.in_head = False
self.document_lang = None
self.alternates = []
def handle_starttag(self, tag, attrs):
attrs = dict(attrs)
if tag == "html" and self.document_lang is None:
self.document_lang = attrs.get("lang")
elif tag == "head":
self.in_head = True
elif tag == "body":
self.in_head = False
elif tag == "link" and self.in_head:
if "alternate" in (attrs.get("rel") or "").lower().split() and attrs.get("hreflang"):
self.alternates.append({
"hreflang": attrs["hreflang"],
"url": urljoin(self.page_url, attrs.get("href") or ""),
})
def decide_language(url, status, headers, html):
parser = HeadSignals(url)
parser.feed(html)
header = (headers.get("Content-Language") or "").split(",")[0].strip() or None
document = parser.document_lang or None
record = {
"url": url,
"status": status,
"content_language": header,
"document_lang": document,
"alternates": parser.alternates,
}
if status >= 400:
record.update(decision="unknown", reason="error response, no usable body")
elif header and document and header.lower() != document.lower():
record.update(decision="conflict", reason="header and document disagree")
elif document:
record.update(decision="use-document-lang", reason="document lang is the authoring claim")
elif header:
record.update(decision="use-header", reason="document lang absent")
else:
record.update(decision="unknown", reason="no language signal in the response")
return record
Two things in that code matter. urljoin resolves root-relative hrefs, because a published alternate set is not required to be absolute. And a disagreement is its own outcome rather than a silent tie-break, so the branch you cannot automate stays visible in the record.
Run against four public pages on 2026-10-22, it produced three distinct decisions:
| URL | Content-Language |
<html lang> |
Decision |
|---|---|---|---|
https://www.php.net/manual/fr/index.php |
en |
fr |
conflict |
https://www.gnu.org/home.en.html |
en |
en |
use document lang |
https://www.w3.org/International/ |
absent | en-EU |
use document lang |
https://www.debian.org/intro/about |
en |
en |
use document lang |
The attribute wins the tie-break because the HTML Standard reaches HTTP only as a final fallback. Language determination for a node uses lang, then an ancestor's lang, then a pragma-set default, and only then language information from a higher-level protocol. In the third row the header is missing, so the attribute is all there is; in the fourth the attribute settles it and the header only corroborates.
When the header and the document disagree
The French PHP manual is a useful example because it is a large site with a real multilingual set. Its French page returns Content-Language: en while declaring lang="fr" and serving a French title and body. Its English page returns en in both places.
A single response cannot tell you why. The header may be a constant set once for the whole manual, it may describe the site's editorial audience rather than the page's text, or it may simply be wrong. What the response does prove is narrower and still useful: on this site the header cannot be used as the language of the text, and a pipeline that trusted it would hand French content to an English-only step without any error.
That is the practical reason to record conflicts instead of resolving them silently. A conflict is the cheapest early warning that a site serves several audiences from one template. Log it, then sample the text: take the first few hundred characters of visible content, check that it reads in the expected language, and write down which evidence decided the outcome.
Find the right locale version without trusting the URL
Once you know the page is not in the language you need, the alternate set tells you where to look. Google's documentation on localized versions sets the rules that make a set usable: every version must list itself as well as the others, alternate URLs must be fully qualified, and if two pages do not both point at each other the tags are ignored. The same page states plainly that Google does not use hreflang or the lang attribute to detect a page's language, so treat the set as a directory, not as a verdict.
Three details in real markup will break a naive reader. Relative hrefs are common: the GNU project's English home page publishes sixteen alternates whose x-default points at /home.html, which only becomes a URL after resolution. x-default is not a fixed English fallback: on both php.net manual pages it points at the page itself, so following it on the French page keeps you on the French page. And published codes are not always in registered form: php.net publishes pt_BR with an underscore, while BCP 47 spells the separator as a hyphen and the region subtag as two letters or three digits. Normalize case and separator before comparing, or pt_BR and pt-BR become two different locales in your index.
Language identity is not the same question as URL identity. If you also need to decide which address to cite after a redirect, that decision has its own evidence requirements, covered in canonical URL vs final URL.
What the fetch response will not tell you
If your pipeline reads converted text instead of HTML, the conversion usually takes the signal with it. A keyless read of that same French page through the single-page Fetch API returned status 200, a French title, and 19,717 characters of Markdown. The response contained ok, requested_url, results, robots_permision, status_code, total_ms and timestamp. It contained no language field, and the Markdown had no <html lang> left in it.
Treat that as a division of labor rather than a defect: conversion answers what the text says, and language identity has to come from the raw response or its headers, captured at the moment of the fetch. If your tool returns only Markdown, you need a second channel for headers, or you accept that the language decision rests on the text sample alone.
Where this rule will keep changing
The rule above assumes a server-rendered document whose head is present in the first response. Two trends break that assumption. Client-side rendering can set lang after the initial HTML arrives, so a fetch that stops at the first byte sees an empty or inherited value while a browser sees a correct one. And content negotiation means the same URL can return different languages depending on Accept-Language, which makes any stored decision incomplete unless you also store the request headers that produced it.
There is a quieter problem underneath both: most pipelines collapse unknown into a default. The HTML Standard keeps "explicitly unknown" as a real state, and the header's own specification notes that an absent Content-Language may mean the sender does not know the intended audience. An agent that writes en whenever it is unsure has destroyed the only information it had. Whether to surface that uncertainty to the model, to the user, or to a retry step is a product decision, and it is the one part of this workflow that no standard settles.
Frequently asked questions
Is the Content-Language header enough to decide a page's language?
No. RFC 9110 defines it as the language of the intended audience, and warns it may not match every language used in the representation. A French page on a site that sends one static header for all locales will report en and still contain French text. Use the header as one claim, keep it in your record, and check it against the document's lang attribute before you rely on it.
Does hreflang tell me what language a page is in?
No. An alternate set describes which URLs a publisher offers for other locales and which code each targets. Google states that it does not use hreflang or the lang attribute to detect page language. The set is still valuable, because it is the fastest way to find the version you need, but the language of the page you already have comes from that page's own signals.
What should an agent do when lang and Content-Language disagree?
Record the conflict, keep both raw values, and settle it against a sample of visible text. If the pipeline must pick one automatically, the document's lang attribute is the better default, because the HTML Standard falls back to HTTP language information only after the attribute and its ancestors. Do not overwrite the disagreement with the chosen value; the disagreement is the part that predicts future failures.
How do I check the language of a page that renders with JavaScript?
Fetch the rendered DOM rather than the initial response, then read <html lang> from the result. If lang is set by a script, the first HTML response may contain an empty or inherited value. Capture the rendered value and the render time together, because the two can diverge when a page swaps language through client-side routing after the initial load.
Is lang="en-US" the same as lang="en" for my pipeline?
They are different tags and should stay different in storage. en-US names a regional variant while en names the language without a region, so a strict comparison treats them as distinct. If your task only needs the language, compare the primary subtag and keep the full tag alongside it. Normalizing case and the separator first prevents a published pt_BR from becoming a separate locale.
What should I store when the language cannot be determined?
Store unknown as an explicit value, along with the URL, the response status, both signal fields exactly as received, and the timestamp. An absent header may mean the sender does not know the intended audience, and an empty lang means the language is explicitly unknown. Keeping that state lets a later step retry, translate, or ask a human, which a silent default to English makes impossible.






