xref: /webtrees/app/Cli/Commands/CompilePoFiles.php (revision b9a4a6c608ac9f9f3bf6c12fbe1fbe871f30fedb)
1*b9a4a6c6SGreg Roach<?php
2*b9a4a6c6SGreg Roach
3*b9a4a6c6SGreg Roach/**
4*b9a4a6c6SGreg Roach * webtrees: online genealogy
5*b9a4a6c6SGreg Roach * Copyright (C) 2023 webtrees development team
6*b9a4a6c6SGreg Roach * This program is free software: you can redistribute it and/or modify
7*b9a4a6c6SGreg Roach * it under the terms of the GNU General Public License as published by
8*b9a4a6c6SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9*b9a4a6c6SGreg Roach * (at your option) any later version.
10*b9a4a6c6SGreg Roach * This program is distributed in the hope that it will be useful,
11*b9a4a6c6SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12*b9a4a6c6SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13*b9a4a6c6SGreg Roach * GNU General Public License for more details.
14*b9a4a6c6SGreg Roach * You should have received a copy of the GNU General Public License
15*b9a4a6c6SGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16*b9a4a6c6SGreg Roach */
17*b9a4a6c6SGreg Roach
18*b9a4a6c6SGreg Roachdeclare(strict_types=1);
19*b9a4a6c6SGreg Roach
20*b9a4a6c6SGreg Roachnamespace Fisharebest\Webtrees\Cli\Commands;
21*b9a4a6c6SGreg Roach
22*b9a4a6c6SGreg Roachuse Fisharebest\Localization\Translation;
23*b9a4a6c6SGreg Roachuse Fisharebest\Webtrees\Webtrees;
24*b9a4a6c6SGreg Roachuse Symfony\Component\Console\Command\Command;
25*b9a4a6c6SGreg Roachuse Symfony\Component\Console\Input\InputInterface;
26*b9a4a6c6SGreg Roachuse Symfony\Component\Console\Output\OutputInterface;
27*b9a4a6c6SGreg Roachuse Symfony\Component\Console\Style\SymfonyStyle;
28*b9a4a6c6SGreg Roach
29*b9a4a6c6SGreg Roachuse function basename;
30*b9a4a6c6SGreg Roachuse function count;
31*b9a4a6c6SGreg Roachuse function dirname;
32*b9a4a6c6SGreg Roachuse function file_put_contents;
33*b9a4a6c6SGreg Roachuse function glob;
34*b9a4a6c6SGreg Roachuse function realpath;
35*b9a4a6c6SGreg Roachuse function var_export;
36*b9a4a6c6SGreg Roach
37*b9a4a6c6SGreg Roachclass CompilePoFiles extends Command
38*b9a4a6c6SGreg Roach{
39*b9a4a6c6SGreg Roach    private const PO_FILE_PATTERN = Webtrees::ROOT_DIR . 'resources/lang/*/*.po';
40*b9a4a6c6SGreg Roach
41*b9a4a6c6SGreg Roach    protected function configure(): void
42*b9a4a6c6SGreg Roach    {
43*b9a4a6c6SGreg Roach        $this
44*b9a4a6c6SGreg Roach            ->setName(name: 'compile-po-files')
45*b9a4a6c6SGreg Roach            ->setDescription(description: 'Convert the PO files into PHP files');
46*b9a4a6c6SGreg Roach    }
47*b9a4a6c6SGreg Roach
48*b9a4a6c6SGreg Roach    protected function execute(InputInterface $input, OutputInterface $output): int
49*b9a4a6c6SGreg Roach    {
50*b9a4a6c6SGreg Roach        $io = new SymfonyStyle(input: $input, output: $output);
51*b9a4a6c6SGreg Roach
52*b9a4a6c6SGreg Roach        $po_files = glob(pattern: self::PO_FILE_PATTERN);
53*b9a4a6c6SGreg Roach
54*b9a4a6c6SGreg Roach        if ($po_files === false || $po_files === []) {
55*b9a4a6c6SGreg Roach            $io->error('Failed to find any PO files matching ' . self::PO_FILE_PATTERN);
56*b9a4a6c6SGreg Roach
57*b9a4a6c6SGreg Roach            return Command::FAILURE;
58*b9a4a6c6SGreg Roach        }
59*b9a4a6c6SGreg Roach
60*b9a4a6c6SGreg Roach        $error = false;
61*b9a4a6c6SGreg Roach
62*b9a4a6c6SGreg Roach        foreach ($po_files as $po_file) {
63*b9a4a6c6SGreg Roach            $po_file      = realpath($po_file);
64*b9a4a6c6SGreg Roach            $translation  = new Translation(filename: $po_file);
65*b9a4a6c6SGreg Roach            $translations = $translation->asArray();
66*b9a4a6c6SGreg Roach            $php_file     = dirname(path: $po_file) . '/' . basename(path: $po_file, suffix: '.po') . '.php';
67*b9a4a6c6SGreg Roach            $php_code     = "<?php\n\nreturn " . var_export(value: $translations, return: true) . ";\n";
68*b9a4a6c6SGreg Roach            $bytes        = file_put_contents(filename: $php_file, data: $php_code);
69*b9a4a6c6SGreg Roach
70*b9a4a6c6SGreg Roach            if ($bytes === false) {
71*b9a4a6c6SGreg Roach                $io->error('Failed to write to ' . $php_file);
72*b9a4a6c6SGreg Roach                $error = true;
73*b9a4a6c6SGreg Roach            } else {
74*b9a4a6c6SGreg Roach                $io->success('Created ' . $php_file . ' with ' . count(value: $translations) . ' translations');
75*b9a4a6c6SGreg Roach            }
76*b9a4a6c6SGreg Roach        }
77*b9a4a6c6SGreg Roach
78*b9a4a6c6SGreg Roach        return $error ? Command::FAILURE : Command::SUCCESS;
79*b9a4a6c6SGreg Roach    }
80*b9a4a6c6SGreg Roach}
81