File size: 4,168 Bytes
cf14c12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php

declare(strict_types=1);

namespace Presentation\Commands;

use Easy\Container\Attributes\Inject;
use Gettext\Generator\PoGenerator;
use Gettext\Loader\PoLoader;
use Gettext\Merge;
use Gettext\Translations;
use Plugin\Domain\PluginWrapper;
use Plugin\Domain\Repositories\PluginRepositoryInterface;
use Shared\Infrastructure\I18n\Twig\Scanner;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Twig\Environment;

#[AsCommand(name: 'app:locale:extract')]
class ExtractLocaleMessagesCommand extends Command
{
    public function __construct(
        private Environment $twig,
        private PluginRepositoryInterface $repo,

        #[Inject('config.dirs.root')]
        private string $rootDir,

        #[Inject('config.dirs.locale')]
        private string $localeDir,

        #[Inject('config.dirs.views')]
        private string $viewsDir,

        #[Inject('config.dirs.plugins')]
        private string $pluginsDir,
    ) {
        parent::__construct();
    }

    protected function execute(
        InputInterface $input,
        OutputInterface $output
    ): int {
        $this->extract(
            $this->rootDir,
            $this->localeDir,
            $this->viewsDir . '/*/*.twig',
        );

        /** @var PluginWrapper */
        foreach ($this->repo as $plugin) {
            $this->extract(
                $this->pluginsDir . '/' . $plugin->context->name->value,
                $this->pluginsDir . '/' . $plugin->context->name->value . '/locale',
                $this->pluginsDir . '/' . $plugin->context->name->value . '/*/*.twig',
                $plugin->context->type->value == 'theme' ? 'theme' : $plugin->context->name->value
            );
        }

        return Command::SUCCESS;
    }

    protected function extract(
        string $baseDir,
        string $localeDir,
        string $scanGlobPattern,
        string $domain = 'messages'
    ): int {
        //Create a new scanner, adding a translation for each domain:
        $scanner = new Scanner(
            $this->twig,
            $baseDir,
            Translations::create($domain)
        );

        //Set a default domain, so any translations with no domain specified, 
        // will be added to that domain
        $scanner->setDefaultDomain($domain);
        $scanner->addReferences(true);

        //Scan files
        foreach ($this->glob($scanGlobPattern) as $file) {
            $scanner->scanFile($file);
        }

        // Get possible languages
        $languages = array_map(
            fn ($dir) => basename($dir),
            glob($localeDir . '/*', GLOB_ONLYDIR)
        );

        //Save the translations in .po files
        $generator = new PoGenerator();
        $loader = new PoLoader();

        foreach ($scanner->getTranslations() as $domain => $translations) {
            foreach ($languages as $language) {
                if (!file_exists("{$localeDir}/{$language}/LC_MESSAGES")) {
                    mkdir("{$localeDir}/{$language}/LC_MESSAGES", 0777, true);
                }

                if (file_exists("{$localeDir}/{$language}/LC_MESSAGES/{$domain}.po")) {
                    $finalTranslations = $translations->mergeWith(
                        $loader->loadFile("{$localeDir}/{$language}/LC_MESSAGES/{$domain}.po"),
                        Merge::SCAN_AND_LOAD
                    );
                } else {
                    $finalTranslations = clone $translations;
                }

                $generator->generateFile(
                    $finalTranslations,
                    "{$localeDir}/{$language}/LC_MESSAGES/{$domain}.po"
                );
            }
        }

        return Command::SUCCESS;
    }

    private function glob($pattern, $flags = 0)
    {
        $files = glob($pattern, $flags);

        foreach (glob(dirname($pattern) . '/*', GLOB_ONLYDIR | GLOB_NOSORT) as $dir) {
            $files = array_merge($files, $this->glob($dir . '/' . basename($pattern), $flags));
        }

        return $files;
    }
}