Passed
Pull Request — main (#9)
by Dante
01:08
created

TranslateObjectsCommand::translatableFields()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 11
rs 10
cc 2
nc 2
nop 1
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * BEdita, API-first content management framework
6
 * Copyright 2024 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\ImportTools\Command;
16
17
use BEdita\Core\Utility\LoggedUser;
18
use Cake\Command\Command;
19
use Cake\Console\Arguments;
20
use Cake\Console\ConsoleIo;
21
use Cake\Console\ConsoleOptionParser;
22
use Cake\Core\Configure;
23
use Cake\Core\InstanceConfigTrait;
24
use Cake\Database\Expression\QueryExpression;
25
use Cake\Utility\Hash;
26
27
/**
28
 * TranslateObjects command.
29
 * Translate objects from a language to another using a translator engine.
30
 * The translator engine is defined in the configuration.
31
 * The configuration must contain the translator engine class and options
32
 * I.e.:
33
 * 'Translators' => [
34
 *   'deepl' => [
35
 *      'name' => 'DeepL',
36
 *      'class' => '\BEdita\I18n\Deepl\Core\Translator',
37
 *      'options' => [
38
 *        'auth_key' => '************',
39
 *      ],
40
 *   ],
41
 * ],
42
 */
43
class TranslateObjectsCommand extends Command
44
{
45
    use InstanceConfigTrait;
46
47
    protected $ok;
48
    protected $error;
49
    protected $io;
50
    protected $dryRun;
51
    protected $defaultStatus;
52
    protected $translatableFields = [];
53
    protected $translator;
54
    protected $langsMap = [
55
        'en' => 'en-US',
56
        'it' => 'it-IT',
57
        'de' => 'de-DE',
58
    ];
59
60
    /**
61
     * @inheritDoc
62
     */
63
    public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser
64
    {
65
        $parser = parent::buildOptionParser($parser);
66
        $parser->addOption('from', [
67
            'short' => 'f',
68
            'help' => 'Language to translate from',
69
            'required' => true,
70
            'choices' => ['en', 'it', 'de'],
71
        ]);
72
        $parser->addOption('to', [
73
            'short' => 't',
74
            'help' => 'Language to translate to',
75
            'required' => true,
76
            'choices' => ['en', 'it', 'de'],
77
        ]);
78
        $parser->addOption('engine', [
79
            'short' => 'e',
80
            'help' => 'Translator engine',
81
            'default' => 'deepl',
82
            'required' => true,
83
            'choices' => ['aws', 'deepl', 'google', 'microsoft'],
84
        ]);
85
        $parser->addOption('status', [
86
            'short' => 's',
87
            'help' => 'Status for new translations',
88
            'choices' => ['draft', 'on', 'off'],
89
            'default' => 'draft',
90
        ]);
91
        $parser->addOption('dry-run', [
92
            'short' => 'd',
93
            'help' => 'Dry run',
94
            'boolean' => true,
95
            'default' => false,
96
        ]);
97
98
        return $parser;
99
    }
100
101
    /**
102
     * @inheritDoc
103
     */
104
    public function execute(Arguments $args, ConsoleIo $io)
105
    {
106
        $this->io = $io;
107
        $this->dryRun = $args->getOption('dry-run');
108
109
        // parameter engine
110
        $engine = $args->getOption('engine') ?? 'deepl';
111
112
        // setup translator engine
113
        $cfg = Configure::read(sprintf('Translators.%s', $engine));
114
        if (empty($cfg)) {
115
            $this->io->abort(sprintf('Translator %s not found', $engine));
116
        }
117
        $class = (string)Hash::get($cfg, 'class');
118
        $options = (array)Hash::get($cfg, 'options');
119
        $this->translator = new $class();
120
        $this->translator->setup($options);
121
122
        // parameters: lang from, lang to
123
        $from = $args->getOption('from');
124
        $to = $args->getOption('to');
125
        $this->io->out(sprintf('Translating objects from %s to %s [dry-run %s]', $from, $to, $this->dryRun ? 'yes' : 'no'));
126
        $to = $this->langsMap[$to];
127
        if ($this->io->ask('Do you want to continue [Y/n]?', 'n') !== 'Y') {
128
            $this->io->abort('Bye');
129
        }
130
        $this->ok = $this->error = 0;
131
        $this->defaultStatus = $args->getOption('status');
132
        $this->processObjects($from, $to);
133
        $this->io->out(sprintf('Processed %d objects (%d errors)', $this->ok + $this->error, $this->error));
134
        $this->io->out('Done');
135
    }
136
137
    /**
138
     * Process objects to translate.
139
     *
140
     * @param string $from The language to translate from
141
     * @param string $to The language to translate to
142
     * @return void
143
     */
144
    private function processObjects(string $from, string $to)
145
    {
146
        $conditions = [];
147
        foreach ($this->objectsIterator($conditions, $from, $to) as $object) {
148
            try {
149
                $this->io->verbose(sprintf('Translating object %s', $object->id));
150
                if (!$this->dryRun) {
151
                    $this->translate($object, $from, $to);
152
                }
153
                $this->io->verbose(sprintf('Translated object %s', $object->id));
154
                $this->ok++;
155
            } catch (\Exception $e) {
156
                $this->io->error(sprintf('Error translating object %s: %s', $object->id, $e->getMessage()));
157
                $this->error++;
158
            }
159
        }
160
    }
161
162
    /**
163
     * Get objects as iterable.
164
     *
165
     * @param array $conditions The conditions to filter objects.
166
     * @param string $lang The language to use to find objects.
167
     * @param string $to The language to translate objects to.
168
     * @return iterable
169
     */
170
    private function objectsIterator(array $conditions, string $lang, string $to): iterable
171
    {
172
        $table = $this->fetchTable('objects');
173
        $conditions = array_merge(
174
            $conditions,
175
            [
176
                $table->aliasField('deleted') => 0,
177
                $table->aliasField('lang') => $lang,
178
            ]
179
        );
180
        $query = $table->find('all')
181
            ->where($conditions)
182
            ->notMatching('Translations', function ($q) use ($to) {
183
                return $q->where(['Translations.lang' => $to]);
184
            })
185
            ->orderAsc($table->aliasField('id'))
186
            ->limit(500);
187
        $lastId = 0;
188
        while (true) {
189
            $q = clone $query;
190
            $q = $q->where(fn (QueryExpression $exp): QueryExpression => $exp->gt($table->aliasField('id'), $lastId));
191
            $results = $q->all();
192
            if ($results->isEmpty()) {
193
                break;
194
            }
195
196
            foreach ($results as $entity) {
197
                $lastId = $entity->id;
198
199
                yield $entity;
200
            }
201
        }
202
    }
203
204
    /**
205
     * Translate object translatable fields and store translation in translations table.
206
     *
207
     * @param \BEdita\Core\Model\Entity\ObjectEntity $object The object to translate
208
     * @param string $from The language to translate from
209
     * @param string $to The language to translate to
210
     * @return void
211
     */
212
    protected function translate($object, $from, $to): void
213
    {
214
        $translatableFields = $this->translatableFields($object->type);
215
        if (empty($translatableFields)) {
216
            return;
217
        }
218
        $translatedFields = [];
219
        foreach ($translatableFields as $field) {
220
            if (empty($object->get($field))) {
221
                continue;
222
            }
223
            $translatedFields[$field] = $this->singleTranslation($object->get($field), $from, $to);
224
        }
225
        $translation = [
226
            'object_id' => $object->id,
227
            'lang' => array_flip($this->langsMap)[$to],
228
            'translated_fields' => json_encode($translatedFields),
229
            'status' => $this->defaultStatus,
230
        ];
231
        $table = $this->fetchTable('Translations');
232
        $entity = $table->newEntity($translation);
233
        $entity->set('translated_fields', json_decode($translation['translated_fields'], true));
234
        $entity->set('status', $this->defaultStatus);
235
        LoggedUser::setUserAdmin();
236
        $table->saveOrFail($entity);
237
    }
238
239
    /**
240
     * Get translatable fields by object type.
241
     *
242
     * @param string $type The object type
243
     * @return array
244
     */
245
    protected function translatableFields(string $type): array
246
    {
247
        if (array_key_exists($type, $this->translatableFields)) {
248
            return $this->translatableFields[$type];
249
        }
250
        /** @var \BEdita\Core\Model\Entity\ObjectType $objectType */
251
        $objectType = $this->fetchTable('ObjectTypes')->find()->where(['name' => $type])->firstOrFail();
252
        $schema = $objectType->get('schema');
253
        $this->translatableFields[$type] = (array)Hash::get($schema, 'translatable');
254
255
        return $this->translatableFields[$type];
256
    }
257
258
    /**
259
     * Translate a single text.
260
     *
261
     * @param mixed $text The text to translate
262
     * @param string $from The language to translate from
263
     * @param string $to The language to translate to
264
     * @return string
265
     */
266
    protected function singleTranslation($text, string $from, string $to): string
267
    {
268
        $response = $this->translator->translate([$text], $from, $to);
269
        $response = json_decode($response, true);
270
271
        return (string)Hash::get($response, 'translation.0');
272
    }
273
}
274