How to Localize HTML5 details, summary, and dialog Elements
A localized disclosure can look correct on the page and still have the wrong accessible label. If you put the question in a paragraph before <summary>, the browser may display it above the toggle—but the toggle itself is labeled “More information.” In a list of disclosures, that gives you several controls with the same name and no clue what each one reveals.
Problem
<summary> is not just a visual heading. It labels the native disclosure control, so its text needs to identify the content even when that content is hidden. Moving the question outside the summary can seem harmless in one language, especially if CSS makes the pieces look like a single component. After localization, a longer question or a different word order can expose the split: the visible question and the control’s announced name no longer match.
There is also a markup constraint: the summary belongs as the first child of its details element. Browsers can recover from incorrect placement, but relying on that recovery means relying on inconsistent rendering and accessibility behavior. A screen reader user may encounter the toggle without the context a sighted user gets from nearby text.
<details> <p>When will my order arrive?</p> <summary>More information</summary> <p>The estimated date appears at checkout.</p> </details> Translating both strings doesn’t fix the relationship. The first paragraph still isn’t the label for the disclosure, and “More information” remains vague. This is easy to miss in a translation review that checks only the fully rendered page, or only the expanded state.
Fix
Make the complete question the summary text, then put the answer in the content the control reveals. The summary should make sense in both states: a question such as “When will my order arrive?” still identifies the disclosure after it has been opened. A state-specific label such as “Show answer” becomes misleading when the answer is already visible.
<details lang="en"> <summary>When will my order arrive?</summary> <p>The estimated date appears at checkout.</p> </details> For a localized page, translate the question as one complete phrase instead of splitting it around the markup. Translators may need to change word order to produce a natural label; keeping the sentence intact gives them room to do that. If the whole page has the same language, set the language on the page rather than repeating it on each disclosure. If a component can appear in a different language from its surroundings, put the correct language on that component.
Keep the browser’s built-in disclosure behavior. You don’t need a click handler to open and close the element, or a manually maintained aria-expanded value. The native control already exposes its state. Adding a second state mechanism creates an opportunity for the visual state and announced state to drift apart.
Production rule: the first child of each
must be its . Don’t rely on browser recovery to repair the structure.
Test both states with a keyboard. Tab to the summary, confirm the question is announced as the control’s name, and use Enter or Space to open it. Then verify that the answer is reachable and the disclosure can be closed again. Repeat this check with the localized text: translated labels can be longer, change word order, or reveal that two different questions were given the same generic summary.
For a focused reference on the surrounding localization concerns, see the HTML5 disclosure and dialog localization walkthrough. If you manage translation strings with the htmltranslate project, keep each summary label as a complete message so translators can adapt it without separating it from the control it names.
The key review question is simple: with the disclosure closed, can someone identify what will open from the summary alone? If not, fix the label or the structure before shipping. A disclosure that only makes sense after it has been opened has already failed the state where its label matters most.
原文:https://www.a8q9.cn/en/article/how-to-localize-html5-details-summary-and-dialog-elements-434d97.html