Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
TranslationGenerator
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 3
12
0.00% covered (danger)
0.00%
0 / 1
 generateTranslation
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 getExpectedKeys
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getLoggerName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2declare(strict_types=1);
3
4namespace App\Service\Api\Anthropic;
5
6/**
7 * Class TranslationGenerator
8 *
9 * This class is responsible for generating translations of text using the Anthropic
10 * API service. It interacts with the AI prompts table to retrieve prompt data
11 * and uses the AnthropicApiService to send requests and parse responses.
12 */
13class TranslationGenerator extends AbstractAnthropicGenerator
14{
15    /**
16     * Generates translations for a given set of strings from one locale to another.
17     *
18     * This method prepares a payload with the provided strings and locale information,
19     * sends a request to the Anthropic API service for translation, and processes the response
20     * to ensure it contains the expected keys.
21     *
22     * @param array $strings An array of strings to be translated.
23     * @param string $localeFrom The locale code of the source language (e.g., 'en_US').
24     * @param string $localeTo The locale code of the target language (e.g., 'fr_FR').
25     * @return array An array containing the translated strings with expected keys.
26     */
27    public function generateTranslation(array $strings, string $localeFrom, string $localeTo): array
28    {
29        $promptData = $this->getPromptData('i18n_batch_translation');
30        $payload = $this->createPayload($promptData, [
31            'strings' => $strings,
32            'localeFrom' => $localeFrom,
33            'localeTo' => $localeTo,
34        ]);
35
36        $timeOut = 45;
37
38        $result = $this->sendApiRequest($payload, $timeOut);
39
40        return $this->ensureExpectedKeys($result);
41    }
42
43    /**
44     * Gets the expected keys for the API response.
45     *
46     * @return array Array of expected response keys.
47     */
48    protected function getExpectedKeys(): array
49    {
50        return ['strings'];
51    }
52
53    /**
54     * Gets the logger name for this generator.
55     *
56     * @return string The logger name.
57     */
58    protected function getLoggerName(): string
59    {
60        return 'Translation Generator';
61    }
62}