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