TranslateFileCommand   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 45
c 1
b 0
f 0
dl 0
loc 69
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A buildOptionParser() 0 29 1
A execute() 0 30 3
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
16
namespace BEdita\ImportTools\Command;
17
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
24
/**
25
 * TranslateFile command.
26
 *
27
 * $ bin/cake translate_file --help
28
 * $ bin/cake translate_file \
29
 *   -i input_file \
30
 *   -o output_file \
31
 *   -f source_language \
32
 *   -t dest_language \
33
 *   -e translator_engine
34
 *
35
 * Perform translation from a file using a translator engine.
36
 * The input file is translated from a source language to a destination language using a translator engine.
37
 * The output file is created with the translated content.
38
 * The translator engine is defined in the configuration.
39
 * The configuration must contain the translator engine class and options
40
 * I.e.:
41
 * 'Translators' => [
42
 *   'deepl' => [
43
 *      'name' => 'DeepL',
44
 *      'class' => '\BEdita\I18n\Deepl\Core\Translator',
45
 *      'options' => [
46
 *        'auth_key' => '************',
47
 *      ],
48
 *   ],
49
 * ],
50
 */
51
class TranslateFileCommand extends Command
52
{
53
    /**
54
     * @inheritDoc
55
     */
56
    protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser
57
    {
58
        $parser->addOption('input', [
59
            'short' => 'i',
60
            'help' => 'Input file path',
61
            'required' => true,
62
        ]);
63
        $parser->addOption('output', [
64
            'short' => 'o',
65
            'help' => 'Output file path',
66
            'required' => true,
67
        ]);
68
        $parser->addOption('from', [
69
            'short' => 'f',
70
            'help' => 'Source language',
71
            'required' => true,
72
        ]);
73
        $parser->addOption('to', [
74
            'short' => 't',
75
            'help' => 'Dest language',
76
            'required' => true,
77
        ]);
78
        $parser->addOption('translator', [
79
            'short' => 'e',
80
            'help' => 'Translator engine name',
81
            'required' => true,
82
        ]);
83
84
        return $parser;
85
    }
86
87
    /**
88
     * @inheritDoc
89
     */
90
    public function execute(Arguments $args, ConsoleIo $io): int
91
    {
92
        $i = (string)($args->getOption('input') ?? (string)$args->getOption('i'));
93
        if (!file_exists($i)) {
94
            $io->err(sprintf('Input file "%s" does not exist', $i));
95
96
            return self::CODE_ERROR;
97
        }
98
        $o = (string)($args->getOption('output') ?? $args->getOption('o'));
99
        $f = (string)($args->getOption('from') ?? $args->getOption('f'));
100
        $t = (string)($args->getOption('to') ?? $args->getOption('t'));
101
        $e = (string)($args->getOption('translator') ?? $args->getOption('e'));
102
        $io->out(sprintf('"%s" [%s] -> "%s" [%s] using "%s" engine.', $i, $f, $o, $t, $e));
103
        $cfg = (array)Configure::read(sprintf('Translators.%s', $e));
104
        if (empty($cfg)) {
105
            $io->err(sprintf('No translator engine "%s" is set in configuration', $e));
106
107
            return self::CODE_ERROR;
108
        }
109
        $class = $cfg['class'];
110
        $options = $cfg['options'];
111
        $translator = new $class();
112
        $translator->setup($options);
113
        $translation = $translator->translate([(string)file_get_contents($i)], $f, $t);
114
        $translation = json_decode($translation, true);
115
        $translation = $translation['translation'];
116
        file_put_contents($o, $translation);
117
        $io->out('Done. Bye!');
118
119
        return self::CODE_SUCCESS;
120
    }
121
}
122