|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the PHP Translation package. |
| 5 | + * |
| 6 | + * (c) PHP Translation team <tobias.nyholm@gmail.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Translation\Bundle\EventListener; |
| 13 | + |
| 14 | +use Translation\Common\Model\Message; |
| 15 | +use Symfony\Component\EventDispatcher\Event; |
| 16 | +use Symfony\Component\Translation\DataCollectorTranslator; |
| 17 | +use Translation\Bundle\Service\StorageService; |
| 18 | + |
| 19 | +/** |
| 20 | + * @author Tobias Nyholm <tobias.nyholm@gmail.com> |
| 21 | + */ |
| 22 | +class AutoAddMissingTranslations |
| 23 | +{ |
| 24 | + /** |
| 25 | + * @var DataCollectorTranslator |
| 26 | + */ |
| 27 | + private $dataCollector; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var StorageService |
| 31 | + */ |
| 32 | + private $storage; |
| 33 | + |
| 34 | + /** |
| 35 | + * @param DataCollectorTranslator $translator |
| 36 | + * @param StorageService $storage |
| 37 | + */ |
| 38 | + public function __construct(StorageService $storage, DataCollectorTranslator $translator = null) |
| 39 | + { |
| 40 | + $this->dataCollector = $translator; |
| 41 | + $this->storage = $storage; |
| 42 | + } |
| 43 | + |
| 44 | + public function onTerminate(Event $event) |
| 45 | + { |
| 46 | + if ($this->dataCollector === null) { |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + $messages = $this->dataCollector->getCollectedMessages(); |
| 51 | + foreach ($messages as $message) { |
| 52 | + if ($message['state'] === DataCollectorTranslator::MESSAGE_MISSING) { |
| 53 | + $m = new Message($message['id'], $message['domain'], $message['locale'], $message['translation']); |
| 54 | + $this->storage->create($m); |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | +} |
0 commit comments