CI/CD Check for Localized Static API Docs: Catching Broken IDs and Mutated Code Blocks

Your localized API page can look correct in a browser and still ship a request example with a translated parameter—or a navigation link that points to an ID that no longer exists. Neither failure needs a runtime test: both are detectable in CI before the page is deployed.
Problem
Localization changes the text around API examples, but code blocks and fragment targets often need to stay stable. A translation pass can alter a token inside a <pre> without making the page visibly broken. Separately, a translated heading may keep its visible meaning while its id changes, leaving an existing same-page link stranded.
Visual review is a weak check for both cases. It won’t reliably catch a changed character in a request, and it can miss a broken fragment if nobody clicks that exact link. The fix is to compare the parsed HTML content in CI, where you can check every localized page on every change.
Keep the check narrow: compare code-block text to the canonical page, verify document IDs and same-page fragments, and require a clear no-translation boundary on each code block. The full walkthrough on localizing HTML documentation covers the surrounding content decisions; this check focuses on catching regressions in the built pages.
Fix
Run a static check against each locale’s corresponding canonical HTML page. The example below uses Beautiful Soup to parse both documents. It compares each <pre> block’s parsed text exactly—no trimming or whitespace collapsing—because whitespace inside examples may matter. Pin the parser as a development dependency so CI uses a predictable version.
from pathlib import Path
from urllib.parse import unquote
from bs4 import BeautifulSoup
def validate(page_path, canonical_path):
page = BeautifulSoup(Path(page_path).read_text(), "html.parser")
canonical = BeautifulSoup(Path(canonical_path).read_text(), "html.parser")
errors = []
actual = page.find_all("pre")
expected = canonical.find_all("pre")
for n in range(max(len(actual), len(expected))):
if n >= len(actual) or n >= len(expected):
errors.append(f"CODE_BLOCK_MUTATED: {page_path} block={n + 1}")
continue
if actual[n].get_text() != expected[n].get_text():
errors.append(f"CODE_BLOCK_MUTATED: {page_path} block={n + 1}")
if actual[n].get("translate", "").lower() != "no":
errors.append(f"MISSING_TRANSLATE_NO: {page_path} block={n + 1}")
ids = [tag["id"] for tag in page.find_all(attrs={"id": True})
if tag["id"]]
for value in set(ids):
if ids.count(value) > 1:
errors.append(f"DUPLICATE_ID: {page_path} id={value}")
targets = set(ids)
for tag in page.find_all("a", href=True):
href = tag["href"]
if href.startswith("#"):
target = unquote(href[1:])
if not target or target not in targets:
errors.append(
f"BROKEN_FRAGMENT: {page_path} href={href} target={target}"
)
return errors
Call validate for every locale-to-canonical page pair in your build, collect the returned errors, and exit nonzero if the list is not empty. The block numbering is one-based so a failure points to the block as a person would count it. If your repository uses a different error format, keep the same information: page, block or target, and failure type.
The check deliberately requires translate="no" on the <pre> itself. That is a simple project convention: it makes the protection visible at the boundary of each example and avoids relying on an ancestor’s state. If your markup allows inherited translation settings instead, adapt the check to walk the ancestors and determine the effective state; don’t treat an absent attribute on <pre> as proof that the block is protected.
Production rule: compare parsed code text exactly unless you have explicitly defined a safe normalization. Silently collapsing whitespace can hide a changed command or example.
Check non-empty IDs for duplicates within each document, then resolve every same-page fragment against that document’s ID set. The example decodes percent-encoded fragment text before comparison. It intentionally doesn’t try to validate links to other pages; those need a site-wide link check and are a separate failure class.
Run this after localization output is generated and before deployment, not only against source templates. That is the HTML your readers will copy from and the browser will navigate. Keep the canonical snippet source under version control, and review changes to it as API-example changes—not as translation edits. You can adapt the same CI pattern from the translation-checker repository to fit your page mapping and build commands.
When the check fails, treat it as a release blocker until someone confirms the intended change and updates the canonical source or localized markup. That keeps a locale from quietly becoming a different API reference than the one you tested.
Comments
Join the discussionNo comments yet. Be the first to comment.
