back

أخبار

أخبار

ترجمة المحتوى بدعم الذكاء الاصطناعي في Drupal: تغيير جذري لمواقع الويب متعددة اللغات

by editor | 27.02.2025

AI-Powered Content Translation in Drupal: A Game-Changer for Multilingual Websites

In today's global digital landscape, multilingual websites are no longer a luxury but a necessity for organizations looking to reach international audiences. However, managing translations across multiple languages has traditionally been one of the most resource-intensive aspects of website maintenance. The manual translation process is not only time-consuming but also costly, requiring either in-house language specialists or external translation services.

Enter the AI Content Translation module for Drupal 10, a powerful solution that leverages OpenAI's advanced language models to provide seamless, automated translations for your website content. This blog post explores how this module can transform your multilingual content strategy while significantly reducing the resources required.

The Challenge of Multilingual Content

Before diving into the solution, let's understand the challenges website administrators face when managing multilingual content:

  1. Resource intensity - Traditional translation requires human translators, which is expensive and time-consuming
  2. Consistency issues - Maintaining consistent terminology and style across translations can be difficult
  3. Content delays - New content often has delayed availability in secondary languages
  4. Ongoing maintenance - Content updates require re-translation, creating a perpetual cycle of translation work
  5. Technical complexity - Managing translation workflows in CMS systems adds another layer of complexity

Introducing the AI Content Translation Module

The AI Content Translation module addresses these challenges by integrating OpenAI's powerful language models directly into Drupal's content management workflow. This module enables automated translation of content entities, including complex structures like paragraphs, fields, and now even image alt text.

Key Features

  • One-click translation - Translate entire pages including nested content with a single click
  • Full entity translation - Supports translation of all translatable content entities
  • Nested structure support - Handles complex content structures including paragraphs and referenced entities
  • Image accessibility translation - Translates ALT text and title attributes for images
  • Configurable AI parameters - Adjustable temperature and prompt settings for translation style control
  • Preservation of formatting - Maintains HTML formatting during translation
  • Detailed logging - Comprehensive logging system for tracking translation processes

How It Works

The module integrates directly with Drupal's existing content translation framework, adding an AI-powered option to the translation workflow. Here's how it works:

  1. Install and configure the module with your OpenAI API key
  2. Navigate to any translatable entity (node, paragraph, etc.)
  3. Select "AI Translate" for the target language
  4. The module recursively processes all translatable text fields, including nested entities
  5. AI-generated translations are created and stored as proper Drupal translations
  6. You can review and edit the translations before publishing

Technical Deep Dive

Under the hood, the module performs several sophisticated operations:

Entity Detection and Processing

The module recursively processes all content entities, identifying translatable fields and nested structures. It builds a comprehensive map of content relationships to ensure nothing is missed during translation.

protected function translateContentEntities(ContentEntityInterface $entity, $target_lang, $language_name, array &$processed_entities = [], ContentEntityInterface $root_entity = NULL) {
  // Set root entity to the current entity if not provided (first call)
  if ($root_entity === NULL) {
    $root_entity = $entity;
  }
  
  // Generate a unique ID for this entity to prevent recursion
  $entity_id = $entity->getEntityTypeId() . ':' . $entity->id();
  
  // Skip if already processed
  if (in_array($entity_id, $processed_entities)) {
    return;
  }
  
  // Mark this entity as processed
  $processed_entities[] = $entity_id;
  
  // Process all fields...
}

Image ALT Text Translation

One of the newest features is the ability to translate image alt text and title attributes, enhancing accessibility for multilingual audiences:

protected function translateImageAttributes($field, ContentEntityInterface $translation, $field_name, $language_name) {
  foreach ($field as $delta => $item) {
    $updated_item = [
      'target_id' => $item->target_id,
      'width' => $item->width ?? NULL,
      'height' => $item->height ?? NULL,
    ];
    
    // Translate alt text if it exists
    if (isset($item->alt) && $this->shouldTranslate($item->alt)) {
      $translated_alt = $this->translationService->translateText($item->alt, $language_name);
      $updated_item['alt'] = $translated_alt;
    } else {
      $updated_item['alt'] = $item->alt ?? '';
    }
    
    // Translate title text if it exists
    if (isset($item->title) && $this->shouldTranslate($item->title)) {
      $translated_title = $this->translationService->translateText($item->title, $language_name);
      $updated_item['title'] = $translated_title;
    } else {
      $updated_item['title'] = $item->title ?? '';
    }
    
    // Set the updated values back to the translation
    $translation->get($field_name)->set($delta, $updated_item);
  }
}

AI Integration

The module communicates with OpenAI's API, sending content for translation with carefully crafted system prompts:

$full_system_prompt = $system_prompt . ' Translate it to ' . $target_language . '.';

$response = $this->httpClient->post('https://api.openai.com/v1/chat/completions', [
  'headers' => [
    'Authorization' => 'Bearer ' . $api_key,
    'Content-Type' => 'application/json',
  ],
  'json' => [
    'model' => $model,
    'messages' => [
      [
        'role' => 'system',
        'content' => $full_system_prompt,
      ],
      [
        'role' => 'user',
        'content' => $text,
      ],
    ],
    'temperature' => (float) $temperature,
  ],
]);

Benefits for Different Stakeholders

For Content Editors

  • Instant translations - No more waiting for translation services
  • Focus on quality - Spend time refining translations rather than creating them from scratch
  • Consistency - AI maintains consistent terminology across translations

For Developers

  • Simple integration - Works with Drupal's existing content translation framework
  • Customizable - Configurable API settings and system prompts
  • Extensible - Well-structured code that can be extended for custom requirements

For Website Owners

  • Cost reduction - Dramatically lower translation costs
  • Faster time-to-market - Publish content in multiple languages simultaneously
  • Broader reach - Easily support more languages without proportional cost increases

Configuration Options

The module provides extensive configuration options to fine-tune the translation process:

  • API key management - Securely store and manage your OpenAI API credentials
  • Model selection - Choose between different OpenAI models (GPT-4, GPT-3.5 Turbo, etc.)
  • System prompt customization - Tailor the translation instructions for specific requirements
  • Temperature control - Adjust the creativity/determinism balance of translations
  • Timeout settings - Configure timeout parameters for handling large content volumes
  • Logging options - Set different logging levels for troubleshooting and auditing

Implementation Considerations

While the AI Content Translation module offers powerful capabilities, here are some important considerations:

  1. Human review - AI translations, while good, still benefit from human review for nuance and accuracy
  2. API costs - Be mindful of OpenAI API usage costs for high-volume translation needs
  3. Content sensitivity - Consider data privacy when sending sensitive content for translation
  4. Language pairs - Some language combinations may yield better results than others

Getting Started

To get started with the AI Content Translation module:

  1. Install the module using Composer: composer require drupal/ai_content_translation
  2. Enable the module: drush en ai_content_translation
  3. Configure your OpenAI API key at /admin/config/content/ai-content-translation
  4. Start translating content through the content translation interface

Conclusion

The AI Content Translation module for Drupal represents a significant advancement in multilingual content management. By leveraging the power of AI, it transforms a traditionally resource-intensive process into a streamlined, efficient workflow.

This doesn't mean human translators will become obsolete; rather, it shifts their role from creating basic translations to refining and culturally adapting content. This hybrid approach—combining AI's efficiency with human expertise—offers the best of both worlds for organizations seeking to create truly global digital experiences.

As AI language models continue to improve, we can expect even better translation quality and more sophisticated features. The future of multilingual content management is here, and it's powered by AI.

Have you implemented the AI Content Translation module on your Drupal site? Share your experiences in the comments below!

screenshot
Bottom
white-paper
Need Support with AI & Drupal?

Are you looking to leverage the power of AI in your Drupal website but not sure where to start? Our team of experts specializes in integrating cutting-edge AI technologies with Drupal to create smarter, more efficient websites.

Schedule a free consultation

  • أخبار
    أخبار
    Introduction to Fractional Leadership services
    editor
  • أخبار
    أخبار
    DrupalCon 2022 Prague - Putting faces to chat convos
    editor
  • أخبار
    أخبار
    Drupal Course for Students - Class of 2020
    admin