1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2023 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 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Cli\Commands; 21 22use Fisharebest\Localization\Translation; 23use Fisharebest\Webtrees\Webtrees; 24use Symfony\Component\Console\Command\Command; 25use Symfony\Component\Console\Input\InputInterface; 26use Symfony\Component\Console\Output\OutputInterface; 27use Symfony\Component\Console\Style\SymfonyStyle; 28 29use function basename; 30use function count; 31use function dirname; 32use function file_put_contents; 33use function glob; 34use function realpath; 35use function var_export; 36 37class CompilePoFiles extends Command 38{ 39 private const string PO_FILE_PATTERN = Webtrees::ROOT_DIR . 'resources/lang/*/*.po'; 40 41 protected function configure(): void 42 { 43 $this 44 ->setName(name: 'compile-po-files') 45 ->setDescription(description: 'Convert the PO files into PHP files'); 46 } 47 48 protected function execute(InputInterface $input, OutputInterface $output): int 49 { 50 $io = new SymfonyStyle(input: $input, output: $output); 51 52 $po_files = glob(pattern: self::PO_FILE_PATTERN); 53 54 if ($po_files === false || $po_files === []) { 55 $io->error('Failed to find any PO files matching ' . self::PO_FILE_PATTERN); 56 57 return Command::FAILURE; 58 } 59 60 $error = false; 61 62 foreach ($po_files as $po_file) { 63 $po_file = realpath($po_file); 64 $translation = new Translation(filename: $po_file); 65 $translations = $translation->asArray(); 66 $php_file = dirname(path: $po_file) . '/' . basename(path: $po_file, suffix: '.po') . '.php'; 67 $php_code = "<?php\n\nreturn " . var_export(value: $translations, return: true) . ";\n"; 68 $bytes = file_put_contents(filename: $php_file, data: $php_code); 69 70 if ($bytes === false) { 71 $io->error('Failed to write to ' . $php_file); 72 $error = true; 73 } else { 74 $io->success('Created ' . $php_file . ' with ' . count(value: $translations) . ' translations'); 75 } 76 } 77 78 return $error ? Command::FAILURE : Command::SUCCESS; 79 } 80} 81