aboutsummaryrefslogtreecommitdiff
path: root/cli/Commands/Plugins/I18N/I18NExtract.php
blob: cc037db500871a2346225d45e0a52df3bb871920 (plain)
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
<?php
namespace Studip\Cli\Commands\Plugins\I18N;

use Exception;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;

final class I18NExtract extends I18NCommand
{
    protected static $defaultName = 'plugin:i18n:extract';

    protected function configure(): void
    {
        parent::configure();

        $this->setDescription('Extract localizable strings.');
        $this->setHelp('This command extracts the localizable string from php files into a .pot file.');
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        try {
            $folder = $this->getPluginFolder($input);
            $manifest = $this->getPluginManifest($folder);
            $localedomain = $this->getPluginLocaleDomainByFolder($folder);

            // Create locale directories if they don't exist
            foreach ($this->getSystemLanguages() as $lang) {
                $lang = explode('_', $lang)[0];
                $language_dir = "{$folder}/locale/{$lang}/LC_MESSAGES";
                if (!file_exists($language_dir)) {
                    mkdir($language_dir, 0755, true);
                }
            }

            // Extract translations into pot file
            $main_lang = $this->getSystemLanguages()[0];
            $pot_file  = "{$folder}/locale/{$main_lang}/LC_MESSAGES/{$localedomain}.pot";
            file_put_contents($pot_file, '');

            $command_line = implode(' | ', [
                'find "${:FOLDER}" -iname "*.php"',
                'xargs xgettext --keyword=_n:1,2 --from-code=UTF-8 -j -n --language=PHP --add-location=never --package-name="${:PACKAGENAME}" -o "${:POTFILE}"',
            ]);

            $process = Process::fromShellCommandline($command_line);

            $process->mustRun(null, [
                'FOLDER'      => $folder,
                'PACKAGENAME' => $manifest['pluginclassname'],
                'POTFILE'     => $pot_file,
            ]);

            $out = $process->getOutput();
            if ($out) {
                $output->writeln($out, OutputInterface::VERBOSITY_VERBOSE);
            }

            // Merge pot into exisiting po files
            foreach ($this->getSystemLanguages() as $lang) {
                if ($lang === $main_lang) {
                    continue;
                }

                $lang = explode('_', $lang)[0];
                $po_file = "{$folder}/locale/{$lang}/LC_MESSAGES/{$localedomain}.po";

                if (!file_exists($po_file)) {
                    continue;
                }

                $command_line = 'msgmerge --backup=off --update "${:POFILE}" "${:POTFILE}"';
                $process = Process::fromShellCommandline($command_line);
                $process->run(null, [
                    'POFILE'  => $po_file,
                    'POTFILE' => $pot_file,
                ]);

                if (!$process->isSuccessful()) {
                    $output->writeln(
                        "<error>Update of po file failed: {$out}</error>",
                        OutputInterface::VERBOSITY_VERBOSE
                    );
                    continue;
                }

                $out = $process->getOutput();
                if ($out) {
                    $output->writeln($out, OutputInterface::VERBOSITY_VERBOSE);
                }
            }

            $output->writeln("Translation strings have been extracted successfully.");
            return Command::SUCCESS;
        } catch (ProcessFailedException $e) {
            $output->writeln("<error>Could not execute shell command</error>");
            $output->writeln($e->getmessage());
            return Command::FAILURE;
        } catch (Exception $e) {
            $output->writeln("<error>{$e->getMessage()}</error>");
            return Command::FAILURE;
        }
    }
}