Drupal is a registered trademark of Dries Buytaert
Protected Pages 3.0.0 Major update available for module protected_pages (3.0.0). Commerce Core 3.3.8 Minor update available for module commerce (3.3.8). Search API HTML Element Filter 1.0.7 Minor update available for module search_api_html_element_filter (1.0.7). Layout Builder Reorder 2.0.1 Minor update available for module layout_builder_reorder (2.0.1). Ban 1.1.0 Minor update available for module ban (1.1.0). Field Formatter Range 2.0.0 Major update available for module field_formatter_range (2.0.0). Field Formatter Range 8.x-1.8 Minor update available for module field_formatter_range (8.x-1.8). Varbase Media Header 9.2.1 Minor update available for module varbase_media_header (9.2.1). Search API Solr 4.3.11 Module search_api_solr updated after 14 months of inactivity (4.3.11). Provus Mega Menu Module provus_mega_menu crossed 1,000 active installs.

Html Segmenter

1 sites No security coverage
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!

Activity

Total releases
1
First release
Jul 2025
Latest release
1 year ago
Releases (12 mo)
0 ▼ from 1
Maintenance
Dormant

Releases

Version Type Release date
1.0.0 Stable Jul 1, 2025