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