Skip to main content
Drupal is a registered trademark of Dries Buytaert
Release: OpenID Connect / OAuth client 3.0.0-alpha9 New alpha version released for module openid_connect (3.0.0-alpha9). Release: Opensolr Search 4.5.0 Minor update available for module opensolr_search (4.5.0). Release: Timelinr 1.0.1 Minor update available for module timelinr (1.0.1). Usage Milestone: Simplify Module simplify crossed 10,000 active installs. Usage Milestone: Views Reference Filter Module entityreference_filter crossed 10,000 active installs. Usage Milestone: Dropdown Language Module dropdown_language crossed 10,000 active installs. Usage Milestone: Paragraphs Browser Module paragraphs_browser crossed 10,000 active installs. Usage Milestone: OpenAPI Module openapi crossed 10,000 active installs. Usage Milestone: Decoupled Router Module decoupled_router crossed 10,000 active installs. Module Revived: Entityqueue Buttons 1.1.2 Module entityqueue_buttons updated after 8 months of inactivity (1.1.2).

Html Segmenter

4 sites No security coverage Drupal 10–11
View on drupal.org

This module extracts translatable text from HTML content, allowing translation services to process only the meaningful parts while preserving the original structure. It can then reassemble the translated content back into the HTML.

Html Segmenter is a utility module for Drupal that helps extract translatable text segments from HTML-rich content, such as long-form body fields or rich text areas. Originally part of the WEB-T module, it has been improved and generalized to support a wide range of use cases and to work cleanly as a standalone dependency.

This module enables translation providers, AI services, or other modules to process only the meaningful plain text content—ignoring HTML tags, links, and dynamic tokens—without breaking the structure of the original document.

Html Segmenter is designed to be reusable and easily integrated with other modules that need HTML field segmentation (e.g., 'ai_translate', 'WEB-T', custom migration pipelines, or content moderation workflows).

Features

- extractTranslatableHtmlValues(), isolates raw translatable text parts from HTML.
- mergeTranslatedHtmlValues(), reassembles translated strings back into the original HTML structure.
- Preserves links, inline tokens, and non-translatable placeholders
- Can be used standalone or as a service by other modules
- Returns both segmented input and optional reintegration after translation
- Ideal for AI-based or external machine translation systems

Usage

Inject or fetch the service:

// Get the service.
$segmenter = \Drupal::service('html_segmenter.segmenter');

// Extract translatable segments from HTML.
$html = 'Hello <img alt="greeting" title="wave"> <b>world</b>!';
$segments = $segmenter->extractTranslatableHtmlValues([$html]);
// $segments might be: ['Hello ', 'greeting', 'wave', 'world', '!']

// After translating the segments (e.g., with an AI service):
$translations = ['Ciao ', 'saluto', 'onda', 'mondo', '!'];

// Merge the translations back into the original HTML.
$merged = $segmenter->mergeTranslatedHtmlValues([$html], $segments, $translations);
// $merged will be: ['Ciao <img alt="saluto" title="onda"> <b>mondo</b>!']

Additional Requirements

No external libraries required
No contrib dependencies

Recommended modules

AI Translate -- now part of Drupal AI module for AI-based translation pipelines
AI Provider WEB-T -- Drupal AI provider to make use of WEB-T e-Translation Services

Similar projects

simplehtmldom API provides low-level DOM parsing, but not focused on segmentation for translation
Html Segmenter is focused and reusable, making it ideal as a shared tool for multilingual and AI-driven projects.

Supporting this Module

This module was created as part of ongoing multilingual and AI development efforts. Contributions and feedback welcome!

Depends on

Dependencies of the latest stable release

No dependencies recorded for this project.

Required by

Tracked projects that depend on this one

No tracked projects depend on this one yet.

Activity

Tracked releases
1
Tracked since
Jun 2025
Latest release
1 year ago
Releases (12 mo)
0 ▼ from 1
Maintenance
Dormant

Releases

Version Type Core Release date
1.0.0 Stable 10–11 Jun 30, 2025