|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Zikula package. |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright Zikula - https://ziku.la/ |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Zikula\Bundle\CoreBundle\Command; |
|
15
|
|
|
|
|
16
|
|
|
use Symfony\Component\Console\Command\Command; |
|
17
|
|
|
use Symfony\Component\Console\Exception\InvalidArgumentException; |
|
18
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
19
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
20
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
21
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
|
22
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
23
|
|
|
use Symfony\Component\Finder\Finder; |
|
24
|
|
|
use Symfony\Component\HttpKernel\KernelInterface; |
|
25
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
26
|
|
|
|
|
27
|
|
|
class TranslationKeyToValueCommand extends Command |
|
28
|
|
|
{ |
|
29
|
|
|
protected static $defaultName = 'zikula:translation:keytovalue'; |
|
30
|
|
|
|
|
31
|
|
|
private $defaultTransPath; |
|
32
|
|
|
|
|
33
|
|
|
public function __construct(string $defaultTransPath = null) |
|
34
|
|
|
{ |
|
35
|
|
|
parent::__construct(); |
|
36
|
|
|
$this->defaultTransPath = $defaultTransPath; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
protected function configure() |
|
40
|
|
|
{ |
|
41
|
|
|
$this |
|
42
|
|
|
->setDescription('update translation files to remove null values and replace them with the key') |
|
43
|
|
|
->addArgument('bundle', InputArgument::OPTIONAL, 'The bundle name or directory where to load the messages') |
|
44
|
|
|
->setHelp( |
|
45
|
|
|
<<<'EOF' |
|
46
|
|
|
The <info>%command.name%</info> command transforms translation strings of a given |
|
47
|
|
|
bundle or the default translations directory. It sets null messages to the value of the key. |
|
48
|
|
|
|
|
49
|
|
|
It is recommended to run <info>php bin/console translation:extract</info> first. |
|
50
|
|
|
|
|
51
|
|
|
Example running against a Bundle (AcmeBundle) |
|
52
|
|
|
|
|
53
|
|
|
<info>php %command.full_name% AcmeBundle</info> |
|
54
|
|
|
|
|
55
|
|
|
Example running against default messages directory |
|
56
|
|
|
|
|
57
|
|
|
<info>php %command.full_name%</info> |
|
58
|
|
|
EOF |
|
59
|
|
|
) |
|
60
|
|
|
; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
|
64
|
|
|
{ |
|
65
|
|
|
$io = new SymfonyStyle($input, $output); |
|
66
|
|
|
|
|
67
|
|
|
/** @var KernelInterface $kernel */ |
|
68
|
|
|
$kernel = $this->getApplication()->getKernel(); |
|
|
|
|
|
|
69
|
|
|
$transPaths = []; |
|
70
|
|
|
if ($this->defaultTransPath) { |
|
71
|
|
|
$transPaths[] = $this->defaultTransPath; |
|
72
|
|
|
} |
|
73
|
|
|
$currentName = 'default directory'; |
|
74
|
|
|
// Override with provided Bundle info (this section copied from symfony's translation:update command) |
|
75
|
|
|
if (null !== $input->getArgument('bundle')) { |
|
76
|
|
|
try { |
|
77
|
|
|
$foundBundle = $kernel->getBundle($input->getArgument('bundle')); |
|
|
|
|
|
|
78
|
|
|
$bundleDir = $foundBundle->getPath(); |
|
79
|
|
|
$transPaths = [is_dir($bundleDir . '/Resources/translations') ? $bundleDir . '/Resources/translations' : $bundleDir . '/translations']; |
|
80
|
|
|
$currentName = $foundBundle->getName(); |
|
81
|
|
|
} catch (\InvalidArgumentException $e) { |
|
82
|
|
|
// such a bundle does not exist, so treat the argument as path |
|
83
|
|
|
$path = $input->getArgument('bundle'); |
|
84
|
|
|
$transPaths = [$path . '/translations']; |
|
|
|
|
|
|
85
|
|
|
if (!is_dir($transPaths[0]) && !isset($transPaths[1])) { |
|
86
|
|
|
throw new InvalidArgumentException(sprintf('"%s" is neither an enabled bundle nor a directory.', $transPaths[0])); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
$io->title('Translation Messages Key to Value Transformer'); |
|
91
|
|
|
$io->comment(sprintf('Transforming translation files for "<info>%s</info>"', $currentName)); |
|
92
|
|
|
$finder = new Finder(); |
|
93
|
|
|
$fs = new Filesystem(); |
|
94
|
|
|
foreach ($finder->files()->in($transPaths)->name(['*.yaml', '*.yml']) as $file) { |
|
95
|
|
|
$io->text(sprintf('<comment>Parsing %s</comment>', $file->getBasename())); |
|
96
|
|
|
$messages = Yaml::parseFile($file->getRealPath()); |
|
97
|
|
|
foreach ($messages as $key => $message) { |
|
98
|
|
|
if (null === $message) { |
|
99
|
|
|
$messages[$key] = $key; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
$io->text(sprintf('<info>Dumping %s</info>', $file->getBasename())); |
|
103
|
|
|
$fs->dumpFile($file->getRealPath(), Yaml::dump($messages)); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
$io->success('Success!'); |
|
107
|
|
|
|
|
108
|
|
|
return Command::SUCCESS; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|