Translate text impementation

Created on 1 July 2025, 2 days ago

Problem/Motivation

Gemini seems really great on keeping the style also on translations. Rather use it.

Proposed resolution

Solution in merge code to follow.
Note: this integrates and solves 🐛 Gemini API error with empty stopSequences value Active
Gemini does not offer (yet) a specialized translateText method like DeepL does, so the chat method used by ai_translate can be used. Maybe this is superfluous, nonetheless it was a good exercise and Gemini can now be used with:

$translator = \Drupal::service('ai.provider')->createInstance('gemini');
$input = new TranslateTextInput($text, 'en', 'it');
/** @var \Drupal\ai\OperationType\TranslateText\TranslateTextOutput $translation */
$translation = $translator->translateText($input, 'models/gemini-1.5-pro', []);
return $translation->getNormalized();

or if set as default in AI settings with:

$providerConfig = $this->aiProviderManager->getDefaultProviderForOperationType('translate_text');
$provider = $this->aiProviderManager->createInstance($providerConfig['provider_id'], $providerConfig);
$translation = $provider->translateText(
  new TranslateTextInput($input_text, $langFrom?->getId(), $langTo->getId()),
  $providerConfig['model_id']
);

Remaining tasks

Add tests?

Feature request
Status

Active

Version

1.0

Component

Code

Created by

🇮🇹Italy viappidu

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024