1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Spiral Framework. |
4
|
|
|
* |
5
|
|
|
* @license MIT |
6
|
|
|
* @author Anton Titov (Wolfy-J) |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace Spiral\Command\Translator; |
11
|
|
|
|
12
|
|
|
use Spiral\Console\Command; |
13
|
|
|
use Spiral\Core\Container\SingletonInterface; |
14
|
|
|
use Spiral\Translator\Catalogue\CatalogueManager; |
15
|
|
|
use Spiral\Translator\CatalogueInterface; |
16
|
|
|
use Spiral\Translator\Config\TranslatorConfig; |
17
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
18
|
|
|
use Symfony\Component\Console\Input\InputOption; |
19
|
|
|
use Symfony\Component\Translation\MessageCatalogue; |
20
|
|
|
|
21
|
|
|
final class ExportCommand extends Command implements SingletonInterface |
22
|
|
|
{ |
23
|
|
|
const NAME = "i18n:export"; |
24
|
|
|
const DESCRIPTION = 'Dump given locale using specified dumper and path'; |
25
|
|
|
|
26
|
|
|
const ARGUMENTS = [ |
27
|
|
|
['locale', InputArgument::REQUIRED, 'Locale to be dumped'], |
28
|
|
|
['path', InputArgument::REQUIRED, 'Export path'] |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
const OPTIONS = [ |
32
|
|
|
['dumper', 'd', InputOption::VALUE_OPTIONAL, 'Dumper name', 'php'], |
33
|
|
|
['fallback', 'f', InputOption::VALUE_NONE, 'Merge messages from fallback catalogue'], |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param TranslatorConfig $config |
38
|
|
|
* @param CatalogueManager $manager |
39
|
|
|
*/ |
40
|
|
|
public function perform(TranslatorConfig $config, CatalogueManager $manager) |
41
|
|
|
{ |
42
|
|
|
if (!$config->hasDumper($this->option('dumper'))) { |
43
|
|
|
$this->writeln("<fg=red>Undefined dumper '{$this->option('dumper')}'.</fg=red>"); |
44
|
|
|
|
45
|
|
|
return; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$mc = $this->getMessageCatalogue( |
49
|
|
|
$config, |
50
|
|
|
$manager, |
51
|
|
|
$manager->get($this->argument('locale')) |
|
|
|
|
52
|
|
|
); |
53
|
|
|
|
54
|
|
|
if ($this->isVerbose() && !empty($mc->getDomains())) { |
55
|
|
|
$this->sprintf("<info>Exporting domain(s):</info> %s\n", |
56
|
|
|
join(',', $mc->getDomains()) |
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$dumper = $config->getDumper($this->option('dumper')); |
61
|
|
|
|
62
|
|
|
$dumper->dump($mc, [ |
63
|
|
|
'path' => $this->argument('path'), |
64
|
|
|
'default_locale' => $config->getDefaultLocale(), |
65
|
|
|
'xliff_version' => '2.0' // forcing default version for xliff dumper only |
66
|
|
|
]); |
67
|
|
|
|
68
|
|
|
$this->writeln("Export successfully completed using <info>" . get_class($dumper) . "</info>"); |
69
|
|
|
$this->writeln("Output: <comment>" . realpath($this->argument('path')) . "</comment>"); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param TranslatorConfig $config |
74
|
|
|
* @param CatalogueManager $manager |
75
|
|
|
* @param CatalogueInterface $catalogue |
76
|
|
|
* @return MessageCatalogue |
77
|
|
|
*/ |
78
|
|
|
protected function getMessageCatalogue( |
79
|
|
|
TranslatorConfig $config, |
80
|
|
|
CatalogueManager $manager, |
81
|
|
|
CatalogueInterface $catalogue |
82
|
|
|
): MessageCatalogue { |
83
|
|
|
$messageCatalogue = new MessageCatalogue( |
84
|
|
|
$catalogue->getLocale(), |
85
|
|
|
$catalogue->getData() |
86
|
|
|
); |
87
|
|
|
|
88
|
|
|
if ($this->option('fallback')) { |
89
|
|
|
foreach ($manager->get($config->getFallbackLocale())->getData() as $domain => $messages) { |
90
|
|
|
foreach ($messages as $id => $message) { |
91
|
|
|
if (!$messageCatalogue->defines($id, $domain)) { |
92
|
|
|
$messageCatalogue->set($id, $message, $domain); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $messageCatalogue; |
99
|
|
|
} |
100
|
|
|
} |