Translator   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 53
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A translate() 0 8 2
A setup() 0 4 1
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * BEdita, API-first content management framework
6
 * Copyright 2023 Atlas Srl, Chialab Srl
7
 *
8
 * This file is part of BEdita: you can redistribute it and/or modify
9
 * it under the terms of the GNU Lesser General Public License as published
10
 * by the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details.
14
 */
15
namespace BEdita\I18n\Microsoft\Core;
16
17
use BEdita\I18n\Core\TranslatorInterface;
18
19
/**
20
 * Translator class that uses Microsoft.
21
 *
22
 * This class uses the Microsoft API to translate texts.
23
 * Pass a valid 'auth_key' to the options array to use this engine.
24
 * Example:
25
 * ```
26
 * use BEdita\I18n\Microsoft\Core\Translator;
27
 * [...]
28
 * $translator = new Translator();
29
 * $translator->setup(['auth_key' => 'your-auth-key']);
30
 * $translation = $translator->translate(['Hello world!'], 'en', 'it');
31
 * ```
32
 */
33
class Translator implements TranslatorInterface
34
{
35
    /**
36
     * The Microsoft API client.
37
     *
38
     * @var \BEdita\I18n\Microsoft\Core\TranslateClient
39
     */
40
    protected TranslateClient $microsoftClient;
41
42
    /**
43
     * The engine options.
44
     *
45
     * @var array
46
     */
47
    protected array $options = [];
48
49
    /**
50
     * Setup translator engine.
51
     *
52
     * @param array $options The options
53
     * @return void
54
     */
55
    public function setup(array $options = []): void
56
    {
57
        $this->options = $options;
58
        $this->microsoftClient = new TranslateClient($this->options);
59
    }
60
61
    /**
62
     * Translate an array of texts $texts from language source $from to language target $to
63
     *
64
     * @param array $texts The texts to translate
65
     * @param string $from The source language
66
     * @param string $to The target language
67
     * @param array $options The options
68
     * @return string The translation in json format, i.e.
69
     * {
70
     *     "translation": [
71
     *         "<translation of first text>",
72
     *         "<translation of second text>",
73
     *         [...]
74
     *         "<translation of last text>"
75
     *     ]
76
     * }
77
     */
78
    public function translate(array $texts, string $from, string $to, array $options = []): string
79
    {
80
        $translation = [];
81
        foreach ($texts as $text) {
82
            $translation[] = $this->microsoftClient->translate($text, $from, $to);
83
        }
84
85
        return (string)json_encode(compact('translation'));
86
    }
87
}
88