Skip to content

Conversation

mnocon
Copy link
Contributor

@mnocon mnocon commented Sep 1, 2025

Adjusted for changes in ibexa/automated-translation#30

Also adjusted the highlight:

Please compare https://doc.ibexa.co/en/latest/multisite/languages/automated_translations/#add-a-custom-machine-translation-service and the current preview.

We should wait with merge untill 5.0.2 is out.

Copy link

github-actions bot commented Sep 1, 2025

Preview of modified files

Preview of modified Markdown:

@mnocon mnocon marked this pull request as ready for review September 3, 2025 09:13
@mnocon mnocon changed the title Adjusted automated translation code sample Adjusted automated translation code sample and highlight Sep 3, 2025
@mnocon mnocon added the Wait with merge PRs that shouldn't be merged instantly label Sep 3, 2025
Copy link

github-actions bot commented Sep 3, 2025

code_samples/ change report

Before (on target branch)After (in current PR)

code_samples/multisite/automated_translation/src/AutomatedTranslation/AiClient.php


code_samples/multisite/automated_translation/src/AutomatedTranslation/AiClient.php

docs/multisite/languages/automated_translations.md@99:``` php hl_lines="35-52"
docs/multisite/languages/automated_translations.md@99:``` php hl_lines="31-48"
docs/multisite/languages/automated_translations.md@100:[[= include_file('code_samples/multisite/automated_translation/src/AutomatedTranslation/AiClient.php') =]]
docs/multisite/languages/automated_translations.md@101:```

001⫶<?php declare(strict_types=1);
002⫶
003⫶namespace App\AutomatedTranslation;
004⫶
docs/multisite/languages/automated_translations.md@100:[[= include_file('code_samples/multisite/automated_translation/src/AutomatedTranslation/AiClient.php') =]]
docs/multisite/languages/automated_translations.md@101:```

001⫶<?php declare(strict_types=1);
002⫶
003⫶namespace App\AutomatedTranslation;
004⫶
005⫶use Ibexa\AutomatedTranslation\Exception\ClientNotConfiguredException;
006⫶use Ibexa\Contracts\AutomatedTranslation\Client\ClientInterface;
005⫶use Ibexa\Contracts\AutomatedTranslation\Client\ClientInterface;
006⫶use Ibexa\Contracts\AutomatedTranslation\Exception\ClientNotConfiguredException;
007⫶use Ibexa\Contracts\ConnectorAi\Action\DataType\Text;
008⫶use Ibexa\Contracts\ConnectorAi\Action\RuntimeContext;
009⫶use Ibexa\Contracts\ConnectorAi\ActionConfigurationServiceInterface;
010⫶use Ibexa\Contracts\ConnectorAi\ActionServiceInterface;
011⫶
012⫶final class AiClient implements ClientInterface
013⫶{
014⫶ /** @var array<string> */
015⫶ private array $supportedLanguages;
016⫶
017⫶ public function __construct(
018⫶ private readonly ActionServiceInterface $actionService,
019⫶ private readonly ActionConfigurationServiceInterface $actionConfigurationService
020⫶ ) {
021⫶ }
022⫶
023⫶ public function setConfiguration(array $configuration): void
024⫶ {
025⫶ if (!array_key_exists('languages', $configuration)) {
026⫶ throw new ClientNotConfiguredException('List of supported languages is missing in the configuration under the "languages" key');
027⫶ }
028⫶ $this->supportedLanguages = $configuration['languages'];
029⫶ }
030⫶
007⫶use Ibexa\Contracts\ConnectorAi\Action\DataType\Text;
008⫶use Ibexa\Contracts\ConnectorAi\Action\RuntimeContext;
009⫶use Ibexa\Contracts\ConnectorAi\ActionConfigurationServiceInterface;
010⫶use Ibexa\Contracts\ConnectorAi\ActionServiceInterface;
011⫶
012⫶final class AiClient implements ClientInterface
013⫶{
014⫶ /** @var array<string> */
015⫶ private array $supportedLanguages;
016⫶
017⫶ public function __construct(
018⫶ private readonly ActionServiceInterface $actionService,
019⫶ private readonly ActionConfigurationServiceInterface $actionConfigurationService
020⫶ ) {
021⫶ }
022⫶
023⫶ public function setConfiguration(array $configuration): void
024⫶ {
025⫶ if (!array_key_exists('languages', $configuration)) {
026⫶ throw new ClientNotConfiguredException('List of supported languages is missing in the configuration under the "languages" key');
027⫶ }
028⫶ $this->supportedLanguages = $configuration['languages'];
029⫶ }
030⫶
031⫶    public function translate(string $payload, ?string $from, string $to): string
032⫶ {
033⫶ $action = new TranslateAction(new Text([$payload]));
034⫶ $action->setRuntimeContext(
031❇️    public function translate(string $payload, ?string $from, string $to): string
032❇️ {
033❇️ $action = new TranslateAction(new Text([$payload]));
034❇️ $action->setRuntimeContext(
035❇️            new RuntimeContext(
036❇️ [
037❇️ 'from' => $from,
038❇️ 'to' => $to,
039❇️ ]
040❇️ )
041❇️ );
042❇️ $actionConfiguration = $this->actionConfigurationService->getActionConfiguration('translate');
043❇️ $actionResponse = $this->actionService->execute($action, $actionConfiguration)->getOutput();
044❇️
045❇️ assert($actionResponse instanceof Text);
046❇️
047❇️ return $actionResponse->getText();
048❇️ }
035❇️            new RuntimeContext(
036❇️ [
037❇️ 'from' => $from,
038❇️ 'to' => $to,
039❇️ ]
040❇️ )
041❇️ );
042❇️ $actionConfiguration = $this->actionConfigurationService->getActionConfiguration('translate');
043❇️ $actionResponse = $this->actionService->execute($action, $actionConfiguration)->getOutput();
044❇️
045❇️ assert($actionResponse instanceof Text);
046❇️
047❇️ return $actionResponse->getText();
048❇️ }
049❇️
050❇️ public function supportsLanguage(string $languageCode): bool
051❇️ {
052❇️ return in_array($languageCode, $this->supportedLanguages, true);
049⫶
050⫶ public function supportsLanguage(string $languageCode): bool
051⫶ {
052⫶ return in_array($languageCode, $this->supportedLanguages, true);
053⫶    }
054⫶
055⫶ public function getServiceAlias(): string
056⫶ {
057⫶ return 'aiclient';
058⫶ }
059⫶
060⫶ public function getServiceFullName(): string
061⫶ {
062⫶ return 'Custom AI Automated Translation';
063⫶ }
064⫶}

053⫶    }
054⫶
055⫶ public function getServiceAlias(): string
056⫶ {
057⫶ return 'aiclient';
058⫶ }
059⫶
060⫶ public function getServiceFullName(): string
061⫶ {
062⫶ return 'Custom AI Automated Translation';
063⫶ }
064⫶}

Download colorized diff

@mnocon mnocon requested a review from a team September 3, 2025 12:33
@ezrobot ezrobot requested review from adriendupuis, dabrt and julitafalcondusza and removed request for a team September 3, 2025 12:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Wait with merge PRs that shouldn't be merged instantly
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants