Why Translating HTML class, id, and data-* Attributes Breaks CSS and JS

Use an attribute allowlist to translate human-facing copy while preserving class, id, and data-* values that your code uses. Apply this rule to pages, components, and email templates; the production caveat is that some attributes contain visible or accessible copy, so neither “translate every attribute” nor “skip every attribute” works.

A browser doesn’t know that checkout and paiement mean the same thing. If a translation changes one side of that contract, the HTML may still look valid while a CSS selector stops matching, a JavaScript handler never runs, or a fragment link no longer lands on its target.

See what breaks when identifiers change

Suppose a checkout component uses a class for styling, an ID for navigation, and a data value for event routing. This French output translates both the copy and the hooks:

<a href="#checkout">Aller au paiement</a>

<section id="paiement" aria-labelledby="checkout-heading">
  <h2 id="titre-paiement">Paiement</h2>
  <button type="button"
          class="bouton bouton--principal"
          data-action="commencer-paiement">
    Passer au paiement
  </button>
</section>

The fragment #checkout no longer finds the section, and aria-labelledby points to a missing heading. The button still renders, but styles for .button--primary and delegated handlers for [data-action="start-checkout"] miss it. None of these failures requires a syntax error.

Keep the same French copy and restore the identifiers consumed by the rest of the application:

<a href="#checkout">Aller au paiement</a>

<section id="checkout" aria-labelledby="checkout-heading">
  <h2 id="checkout-heading">Paiement</h2>
  <button type="button"
          class="button button--primary"
          data-action="start-checkout">
    Passer au paiement
  </button>
</section>

The browser can resolve both references again, while the text nodes supply the French copy. Compare the localized output against the original by following the link, checking the section’s accessible name, and exercising the button. A screenshot catches missing styles but won’t tell you whether the click reached the correct handler.

Never translate a value that acts as a selector, reference, or machine-readable key. Rename it as a code change, not as a translation edit.

Decide which attributes may enter the translation catalog

Start with text nodes and an explicit list of human-facing attributes. Classify each attribute by what consumes it, not by whether its value looks like English. A value such as primary looks like a word, but it may be an enum that CSS, JavaScript, and tests all expect to remain unchanged.

FieldExampleLocalization rule
Selector tokensclass="button button--primary"Preserve all tokens. Translating just the modifier can remove the component’s variant styling.
IDs and their referencesid="email", for="email", href="#email"Preserve both the target and its references, including ID lists such as aria-describedby.
Machine datadata-action="start-checkout"Preserve event names, enums, analytics keys, and test hooks.
Human-facing copyalt="Red backpack", aria-label="Close"Translate nonempty copy where applicable. Keep an intentionally empty alt="" empty.
Hints and tooltipsplaceholder="Enter your email", title="Billing help"Translate when used as copy. A placeholder doesn’t replace a form label.
Language and directionlang="ar", dir="rtl"Set from the locale configuration rather than passing the values to a translator.

Separate data keys from displayed strings

Audit what reads your custom attributes before allowing any data-* value into a catalog. Start with CSS attribute selectors: they compare the serialized values, not their meaning.

.button--primary {
  background-color: #173c78;
  color: #fff;
}

[data-state="loading"] .button__spinner {
  display: inline-block;
}

Translating button--primary drops the first rule, even if the base class stays intact. Translating loading in the HTML makes the second rule stop matching until some runtime code writes the expected value back. Check both the initial render and the state after interaction; a script can accidentally mask a broken export.

When a component needs a localized string for a state change, give that string its own field. Here the template already contains the French strings, but the action and state values remain stable:

<button type="button"
        class="button button--primary"
        data-action="start-checkout"
        data-state="ready"
        data-busy-label="Traitement en cours…">
  <span class="button__label">Passer au paiement</span>
  <span class="button__spinner" aria-hidden="true" hidden></span>
</button>

Allowlist data-busy-label for this component only: its value is copy, while data-action and data-state drive behavior. The custom attribute itself is not a visible or accessible label. Verify that the component actually inserts its value into the label when the state changes.

function renderCheckoutState(button, pending, idleLabel) {
  const label = button.querySelector(".button__label");
  const spinner = button.querySelector(".button__spinner");

  button.dataset.state = pending ? "loading" : "ready";
  button.disabled = pending;
  spinner.hidden = !pending;
  label.textContent = pending ? button.dataset.busyLabel : idleLabel;
}

Capture the original localized label before entering the pending state and pass it as idleLabel on both transitions. JavaScript exposes data-busy-label as dataset.busyLabel; changing the attribute name breaks that lookup even if its value survives. Use textContent so punctuation such as < is rendered as text rather than parsed as markup.

Constrain extraction and reinsertion

Configure your extractor or translation export to expose text nodes and approved attribute values, not arbitrary strings from the serialized HTML. If the system supports per-component rules, scope exceptions such as data-busy-label to the component that owns them. For template-driven builds, a useful catalog shape is one message per editable field:

{
  "checkout.heading": "Paiement",
  "checkout.submit.label": "Passer au paiement",
  "checkout.submit.busyLabel": "Traitement en cours…",
  "checkout.close.accessibleName": "Fermer"
}

Use the message keys to bind values back to text nodes or known attributes. The catalog doesn’t contain class, id, or action values, so an editor can’t overwrite them through the normal translation workflow. Render values with the template engine’s context-aware escaping, and inspect the parsed output when a translation contains quotes, ampersands, or angle brackets.

Use translate="no" for a visible term that must stay unchanged within otherwise translatable copy. Keep the scope narrow:

<p>
  Consultez votre reçu dans
  <span translate="no">Acme Pay</span>.
</p>

<button type="button" data-action="close" aria-label="Fermer">
  <span aria-hidden="true">×</span>
</button>

On tools that honor it, translate="no" marks that element’s text and translatable attributes as not for translation, with the setting inherited by descendants unless overridden. Putting it on the entire button just to protect data-action could also prevent localization of aria-label. Verify what the exporter honors instead of relying on the markup alone.

Never freeze an entire control to protect one technical attribute. Its visible label or accessible name still needs to be available for translation.

Validate the localized output before publishing

Verify the HTML that actually ships, not just the catalog. For a web page, run checks against the rendered DOM after the component has mounted; for an email, check the final send preview or test message after CSS inlining and link rewriting. Email buttons should use links for navigation rather than depending on JavaScript event handlers.

const section = document.getElementById("checkout");
const heading = document.getElementById("checkout-heading");
const button = section?.querySelector('[data-action="start-checkout"]');

if (!section || !heading || !section.contains(heading) ||
    section.getAttribute("aria-labelledby") !== heading.id) {
  throw new Error("Checkout target or accessible-name reference is broken");
}

if (!button?.classList.contains("button--primary") ||
    !button.dataset.busyLabel?.trim()) {
  throw new Error("Checkout button hook or localized busy label is missing");
}

Run these checks in the browser console on a page that contains the checkout component, or adapt the assertions for a browser test. They make missing hooks fail explicitly instead of leaving you to notice a silent selector miss. They don’t prove that the visible copy is correct, that IDs are unique, or that the button works; test those separately.

Use HTML Translate to compare a localized HTML export against its source and spot changes to selector hooks alongside changes to copy.

  1. Check that each expected id occurs once per document and that same-page href="#…" references still land on the right element.
  2. Exercise states such as ready and loading; verify that both the selector hooks and the localized labels update as intended.
  3. Inspect accessible names and descriptions for aria-label, aria-labelledby, and aria-describedby. Also verify that clicking a label focuses its associated input where expected.
  4. Check wrapping and alignment with longer translated text. For a right-to-left locale, set the appropriate dir and fix the layout with logical CSS properties such as padding-inline, not translated class names.

Common mistakes and troubleshooting

  • The component looks almost right. The base class survived but a modifier changed. Compare the full token list and restore the exact modifier expected by the stylesheet.
  • The button works on the source page but not in another locale. Check the exact value used by closest('[data-action="start-checkout"]') or the equivalent handler. Restore the stable action key and localize the button text instead.
  • A link or label targets the wrong element. Check for duplicate id values, especially when two language variants render in the same DOM. Generate unique instance IDs and bind all references to them together.
  • The accessible name stays in the source language. Export human-facing values such as aria-label, but keep the identifier list in aria-labelledby unchanged. Translate the text of the referenced element.
  • The translated label displays as undefined. Confirm that data-busy-label wasn’t renamed or dropped and that the JavaScript reads dataset.busyLabel.
  • Analytics reports split by language unexpectedly. Restore machine identifiers such as data-event="checkout_started". Record the locale as its own property instead of translating the event name.

HTML File Translation Questions

Can you use the same ID on separate language pages?

Yes. An id needs to be unique within its document, not across the site. Using id="checkout" on separate locale pages is fine; duplicating it when rendering both variants in one document is not.

Can a site intentionally use localized anchors?

Yes, but treat them as a deliberate URL and navigation decision. Generate the anchor IDs and their href="#…" links together, account for incoming links that use older fragments, and update any code or accessibility references that depended on the original IDs.

Are all data attributes off-limits?

No. Preserve values read as keys or state, such as data-state="ready". A field such as data-busy-label can contain translated copy if the component explicitly treats it as display text and the extractor allowlists it.

Does translating a class affect SEO directly?

The spelling of an ordinary class token is not a localization signal. The practical risk is indirect: broken selectors or JavaScript can hide content, disrupt navigation, or stop a component from rendering as intended.

Should you translate element names or form field names?

Keep HTML tag names such as button and submission keys such as name="email" unchanged. Translate the visible label, help text, and any human-facing validation message without changing the key the server receives.

Translate a file →