Passed
Push — master ( 9bba7f...685a87 )
by Julito
09:22
created

UpdateVueTranslations::execute()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 30
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 20
nc 4
nop 2
dl 0
loc 30
rs 9.6
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\Command;
8
9
use Chamilo\CoreBundle\Repository\LanguageRepository;
10
use Symfony\Component\Console\Command\Command;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
14
use Symfony\Contracts\Translation\TranslatorInterface;
15
16
class UpdateVueTranslations extends Command
17
{
18
    /**
19
     * @var string|null
20
     */
21
    protected static $defaultName = 'chamilo:update_vue_translations';
22
23
    private LanguageRepository $languageRepository;
24
    private ParameterBagInterface $parameterBag;
25
    private TranslatorInterface $translator;
26
27
    public function __construct(LanguageRepository $languageRepository, ParameterBagInterface $parameterBag, TranslatorInterface $translator)
28
    {
29
        $this->languageRepository = $languageRepository;
30
        $this->parameterBag = $parameterBag;
31
        $this->translator = $translator;
32
33
        parent::__construct();
34
    }
35
36
    protected function configure(): void
37
    {
38
        $description = 'This command updates the json vue locale files based in the Symfony /translation folder';
39
        $this
40
            ->setDescription($description)
41
            ->setHelp($description)
42
        ;
43
    }
44
45
    protected function execute(InputInterface $input, OutputInterface $output): int
46
    {
47
        $languages = $this->languageRepository->findAll();
48
        $dir = $this->parameterBag->get('kernel.project_dir');
49
50
        $vueLocalePath = $dir.'/assets/vue/locales/';
51
        $englishJson = file_get_contents($vueLocalePath.'en.json');
52
        $translations = json_decode($englishJson, true);
53
54
        foreach ($languages as $language) {
55
            $iso = $language->getIsocode();
56
57
            if ('en_US' === $iso) {
58
                continue;
59
            }
60
61
            $newLanguage = [];
62
            foreach ($translations as $variable => $translation) {
63
                $translated = $this->translator->trans($variable, [], null, $iso);
64
                $newLanguage[$variable] = $translated;
65
            }
66
            $newLanguageToString = json_encode($newLanguage, JSON_PRETTY_PRINT);
67
            $fileToSave = $vueLocalePath.$iso.'.json';
68
            file_put_contents($fileToSave, $newLanguageToString);
69
            $output->writeln("json file generated for iso $iso: $fileToSave");
70
        }
71
        $output->writeln('');
72
        $output->writeln("Now you can commit the changes in $vueLocalePath ");
73
74
        return Command::SUCCESS;
75
    }
76
}
77